Codice PHP:
function random_string($len)
{
$string = "";
$chars = array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
"l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
"w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
"H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
"S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",
"3", "4", "5", "6", "7", "8", "9");
for($i = 0; $i < $len; ++$i)
{
shuffle($chars);
$string .= $chars[0];
}
return $string;
}
header("Content-type: image/png");
//header(”Content-type: image/png”);
$x = 200;
$y = 75;
$code = random_string(6);//substr($_GET['code'],0,9);
$space = $x / (strlen($code)+1);// Calcoliamo lo spazio che deve esserci tra un carattere e il successivo
$img = imagecreatetruecolor($x,$y);
$bg = imagecolorallocate($img,0,0,0);
$border = imagecolorallocate($img,0,0,0);
$colors[] = imagecolorallocate($img,128,64,192);
$colors[] = imagecolorallocate($img,216,147,54);
$colors[] = imagecolorallocate($img,108,192,64);
imagefilledrectangle($img,1,1,$x-2,$y-2,$bg);
imagerectangle($img,0,0,$x-1,$y-2,$border);
for ($i=0; $i< strlen($code); $i++)
{
$color = $colors[$i % count($colors)];
imagettftext($img,28+rand(0,8),-20+rand(0,40),($i+0.3)*$space,50+rand(0,10),$color,'xxx.otf',$code{$i});
}
for($i=0;$i<280;$i++)
{
$x1 = rand(3,$x-3);
$y1 = rand(3,$y-3);
$x2 = $x1-2-rand(0,8);
$y2 = $y1-2-rand(0,8);
imageline($img,$x1,$y1,$x2,$y2,$colors[rand(0,count($colors)-1)]);
}
?>
<?php imagepng($img); ?>
Poi il file che dovrebbe farlo vedere: