Thursday, April 12, 2007

Create Random String Image

<?

$string = stringGenerator();
//setcookie("ivstrcookie", $string, time()+3600);

function imageGenerator($string) {
header("Content-type: image/png");

$x = 100;
$y = 30;
$im = imagecreatetruecolor($x,$y);
$putih = imagecolorallocate($im, 255, 255, 255);
$orange = imagecolorallocate($im, 220, 210, 60);
$hitam = imagecolorallocate($im, 0, 0, 0);
$abu2 = imagecolorallocate($im, 200, 200, 200);
imagefill($im,0,0,$putih);
// Border
imagerectangle($im,0,0,$x-1,$y-1,$hitam);

imagestring($im, 6, $x - 78, $y - 24, $string, $hitam);

imagepng($im);
imagedestroy($im);
}

function stringGenerator() {
$strlen = 6;
$string = '';
for ($i=1;$i<=$strlen;$i++) {
$rand2 = round(rand (0, 2)) + 1;

if ($rand2 == 1) {
$bottomlimit = 48;
$toplimit = 57;
}
elseif ($rand2 == 2) {
$bottomlimit = 65;
$toplimit = 90;
}
else {
$bottomlimit = 97;
$toplimit = 122;
}

$rand1 = round(rand (0,$toplimit-$bottomlimit)) + $bottomlimit;
$string .= chr($rand1);
}

return $string;
}

imageGenerator($string);
?>

Save as image.php. Example available at http://muntenari.com/imageverification.php

No comments: