Codice PHP:
<?php
create_image();
<b>Conferma codice</b>
<input type='text' name='OPcaptcha' value='' maxlength='5'>
<span style='padding-left: 10px'> </span>
<img src=".$_SESSION['captcha2'].">
// va poi inserito il controllo sulla risposta
function create_image()
{
$testo = substr(random_string(),0,5);
$_SESSION['captcha1'] = $testo;
$_SESSION['captcha2'] = NS('captchapng').time().'.png';
$x = 100;
$y = 50;
$image = imagecreatetruecolor($x, $y);
$colore = '#FFFFCC';
$sfondo = imagecolorallocate($image,hexdec(substr($colore,1,2)),hexdec(substr($colore,3,2)),hexdec(substr($colore,5,2)));
$nero = imagecolorallocate($image,0,0,0);
imagefilledrectangle($image, 0, 0, $x, $y, $sfondo);
imagettftext
(
$image, //immagine
15, //dimensione carattere
rand(-5, 5), //angolo di rotazione
10+rand(-10,10), //offset sulla x
30, //offset sulla y
$nero, //colore
NS('arialttf'), //font
$testo //da stampare
);
$line_color = imagecolorallocate($image, 64, 64, 64 );
for ($i = 0; $i < 5; $i++)
imageline($image, 0, rand() % 50, 100, rand() % 50, $line_color);
$pixel_color = imagecolorallocate($image, 0, 0, 255);
for ($i = 0; $i < 1000; $i++)
imagesetpixel($image, rand() % 200, rand() % 50, $pixel_color);
$images = glob(NS('captchapng')."*.png");
foreach ($images as $image_to_delete) @unlink($image_to_delete);
imagepng($image, $_SESSION['captcha2']);
}
function random_string()
{
$str = 'ABCDEFGHKLMNOPQRSTWXYZabcdefghjkmnpqrstwxyz123456789';
$strl = strlen($str)-1;
$r = '';
$l = 8+rand(0,5);
for($i=0;$i<$l;$i++) $r .= $str{rand(0,$strl)};
return $r;
}
?>