
Originalmente inviato da
penelopesito
...
Ma non c'è verso di vedere l'immagine con il testo >_<'''
Non capisco questo tuo suggerimento:
- Poi per il form sostituisci i checkbox coi radiobutton (la scelta per ogni gruppo è unica).
Devo usare i radio button perché ho inserito scelte su più cose?
...
Controlli di sicurezza a che scopo?
Grazie infinite per la pazienza, scusate ma io sono autodidatta e php mi fa impazzire^^
Puoi trovare un esempio funzionante (è solo il tuo codice) qui.
la pagina col form è:
Codice PHP:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Ante card</title>
<style type="text/css">
body {background-color: #faf6db;
text-align:center;
font-family : "Tahoma";
color : #4c412c;
font-size : 14px
}
table.base{
border:0px;
text-align:center;
font-family : "Tahoma";
color : #4c412c;
font-size : 12px;
margin-left:auto;
margin-right:auto
}
td.bordo{
border:1px dotted #4c412c;
text-align:center;
font-family : "Tahoma";
color : #4c412c;
font-size : 12px;
}
td.left{
border: 0px;
text-align:left;
font-family : "Tahoma";
color : #4c412c;
font-size : 12px;
}
</style>
<script type="text/javascript">
function checkform(f) {
// Controllo dei radiobutton
var boolRadio = false;
for (var i = 0; i < f.elements['card'].length; i++) {
boolRadio = boolRadio || f.elements['card'][i].checked;
}
if (!boolRadio) {
alert("Seleziona una card");
return false;
}
}
</script>
</head>
<body>
<table class="base" cellpadding="3" cellspacing="3" width="1000">
<tr>
<td><form name="modulo" action="card.php" method="get" onsubmit="return checkform(this)">
<table class="base" cellpadding="3" cellspacing="3" width="100%">
<tr>
<td class="bordo"><img border="0" src="http://forum.it.altervista.org/images/card_01.jpg" width="310" height="310" alt=""><br>
<input name="card" type="checkbox" value="http://forum.it.altervista.org/images/card_01.jpg"></td>
<td class="bordo"><img border="0" src="http://forum.it.altervista.org/images/card_02.jpg" width="310" height="310" alt=""><br>
<input name="card" type="checkbox" value="http://forum.it.altervista.org/images/card_02.jpg"></td>
<td class="bordo"><img border="0" src="http://forum.it.altervista.org/images/card_03.jpg" width="310" height="310" alt=""><br>
<input name="card" type="checkbox" value="http://forum.it.altervista.org/images/card_03.jpg"></td>
</tr>
<tr>
<td colspan="3"> </td>
</tr>
<tr>
<td class="left" colspan="3">
- Colore testo<br>
Nero <input type="checkbox" name="colore_testo" checked value="0,0,0">
Blu <input type="checkbox" name="colore_testo" value="0,51,153">
Rosso <input type="checkbox" name="colore_testo" value="204,51,102"><br><br>
- Font<br>
Arial <input type="checkbox" name="font" checked value="font/arial.ttf">
Tahoma <input type="checkbox" name="font" value="font/tahoma.ttf"><br><br>
- Posizione testo<br>
Top: 10 <input type="checkbox" name="valore_x" checked value="10"> 15 <input type="checkbox" name="valore_x" value="15"> 20 <input type="checkbox" name="valore_x" value="15"><br>
Lato: 10 <input type="checkbox" name="valore_y" checked value="10"> 15 <input type="checkbox" name="valore_y" value="15"> 20 <input type="checkbox" name="valore_y" value="15"><br><br>
<textarea name="testo" rows="10" cols="35"></textarea><br>
<input type="submit" name="invia" value="Invia"></td>
</tr>
</table></form></td>
</tr>
</table>
</body>
</html>
mentre la pagina php è:
Codice PHP:
<?php
// Recupero i vari dati dal form
$img = $_GET['card'];
$txt = $_GET['testo'];
$font = $_GET['font'];
$colore_testo = $_GET['colore_testo'];
$x = $_GET['valore_x'];
$y = $_GET['valore_y'];
// Scelgo l'immagine
$mia_img = imagecreatefromjpeg($img);
// Scelgo il colore del testo
//$colore_testo = imagecolorallocate($mia_img,255,255,255);
// Creo una variabile con il testo da inserire
$testo = $txt;
// Inserisco il testo nell'immagine, scelgo il colore, la font e la posizione del testo sull'immagine
imagefttext ($mia_img, 10, 0, $x, $y, $colore_testo, $font, $testo);
// Definisco il tipo di immagine
header("Content-type: image/jpeg");
// Visualizzo l'immagine
imagejpeg($mia_img);
// Elimino tutto
imagedestroy($mia_img);
?>
e come vedi funziona.
Riguardo l'uso di checkbox e/o radiobutton: Con i checkbox dai la possibilità di selezionare o meno una determinata opzione, mentre con i radio button dai la possibilità di selezionare uno ed un solo elemento da un gruppo.
Nel tuo caso, tu permetti scegliere anche tutte le caselle (tutte le immagini, tutti i font, ...) mentre è più corretto stabilire che per ogni gruppo ci sia la possibilità di selezionare una sola opzione.
Riguardo la sicurezza, chi ti garantisce che il tuo script verrà usato solo per come lo hai pensato tu? E se qualcuno comincia a scrivere del codice ad hoc per bucarti il sito? Mai prendere i parametri passati così come sono senza controllarli, e non dare mai notizie in più di quelle strettamente necessarie (tipo i percorsi delle varie cartelle ...)
EDIT:
qui puoi trovare una versione modificata.
questo il file col form:
Codice PHP:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Ante card</title>
<style type="text/css">
body {background-color: #faf6db;
text-align:center;
font-family : "Tahoma";
color : #4c412c;
font-size : 14px
}
table.base{
border:0px;
text-align:center;
font-family : "Tahoma";
color : #4c412c;
font-size : 12px;
margin-left:auto;
margin-right:auto
}
td.bordo{
border:1px dotted #4c412c;
text-align:center;
font-family : "Tahoma";
color : #4c412c;
font-size : 12px;
}
td.left{
border: 0px;
text-align:left;
font-family : "Tahoma";
color : #4c412c;
font-size : 12px;
}
</style>
<script type="text/javascript">
function checkform(f) {
// Controllo dei radiobutton
var boolRadio = false;
for (var i = 0; i < f.elements['card'].length; i++) {
boolRadio = boolRadio || f.elements['card'][i].checked;
}
if (!boolRadio) {
alert("Seleziona una card");
return false;
}
}
</script>
</head>
<body>
<form name="modulo" action="card2.php" method="post" onsubmit="return checkform(this)">
<table class="base" cellpadding="3" cellspacing="3" width="1000">
<tr>
<td>
<table class="base" cellpadding="3" cellspacing="3" width="100%">
<tr>
<td class="bordo">
<img border="0" src="images/card_01.jpg" width="310" height="310" alt=""><br>
<input name="card" type="radio" value="01">
</td>
<td class="bordo">
<img border="0" src="images/card_02.jpg" width="310" height="310" alt=""><br>
<input name="card" type="radio" value="02">
</td>
<td class="bordo">
<img border="0" src="images/card_03.jpg" width="310" height="310" alt=""><br>
<input name="card" type="radio" value="03">
</td>
</tr>
<tr>
<td colspan="3"> </td>
</tr>
<tr>
<td class="left" colspan="3">
<b>Colore testo:</b>
<select name="colore_testo">
<option value="nero" selected>Nero</option>
<option value="blu">Blu</option>
<option value="rosso">Rosso</option>
</select>
<br><br>
<b>Font:</b><br>
<input type="radio" name="font" checked value="arial"> Arial <br>
<input type="radio" name="font" value="tahoma"> Tahoma<br><br>
<b>Posizione testo:</b><br>
Top:
<select name="valore_x" size="3">
<option value="10" selected>10</option>
<option value="15">15</option>
<option value="20">20</option>
</select>
Sinistra:
<select name="valore_y" size="3">
<option value="10" selected>10</option>
<option value="15">15</option>
<option value="20">20</option>
</select>
<br><br>
<textarea name="testo" rows="10" cols="35"></textarea><br>
<input type="submit" name="invia" value="Invia">
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</body>
</html>
e questo il file per la generazione dell'immagine.
Codice PHP:
<?php
// Recupero i vari dati dal form
$img = isset($_POST['card']) ? $_POST['card'] : '';
$txt = isset($_POST['testo']) ? $_POST['testo'] : '';
$f = isset($_POST['font']) ? $_POST['font'] : '';
$c = isset($_POST['colore_testo']) ? $_POST['colore_testo'] : '';
$x = isset($_POST['valore_x']) ? (int)$_POST['valore_x'] : '';
$y = isset($_POST['valore_y']) ? (int)$_POST['valore_y'] : '';
//Verifico che sia stata scelta una immagine
if(!$img) {
echo "<h1>Non è stata selezionata alcuna immagine.</h1>";
echo "<a href=\"index2.php\">Indietro</a>";
exit;
}
switch($f){
case "tahoma":
$font = "./font/tahoma.ttf";
break;
default:
$font = "./font/arial.ttf";
break;
}
// Scelgo l'immagine
$mia_img = imagecreatefromjpeg('./images/card_' . $img . '.jpg');
//Imposto i valori per il colore
switch($c) {
case "blu":
$color = imagecolorallocate($mia_img, 0,51,153);
break;
case "rosso":
$color = imagecolorallocate($mia_img, 204,51,102);
break;
default:
$color = imagecolorallocate($mia_img, 0,0,0);
break;
}
// Inserisco il testo nell'immagine, scelgo il colore, la font e la posizione del testo sull'immagine
imagefttext ($mia_img, 10, 0, $x, $y, $color, $font, $txt);
// Definisco il tipo di immagine
header("Content-type: image/jpeg");
// Visualizzo l'immagine
imagejpeg($mia_img);
// Elimino tutto
imagedestroy($mia_img);
?>