Confirmation code generation for preventing automated registrations on a website.
Function arguments are:
$code - the code that you shall random generate
$location - relative location of the image that shall be generated
$fonts_dir - relative location for the GDF fonts directory
This function will create an image with the code given by you and will save it in the directory specified with the name formed by MD5 hash of the code.
You may insert as many font types in the fonts directory as you wish, with random names.
<?php
function generate_image($code, $location, $fonts_dir)
{
$image = imagecreate(150, 60);
imagecolorallocate($image, rand(0, 100), rand(100, 150), rand(150, 250));
$fonts = scandir($fonts_dir);
$max = count($fonts) - 2;
$width = 10;
for ($i = 0; $i <= strlen($code); $i++)
{
$textcolor = imagecolorallocate($image, 255, 255, 255);
$rand = rand(2, $max);
$font = imageloadfont($fonts_dir."/".$fonts[$rand]);
$fh = imagefontheight($font);
$fw = imagefontwidth($font);
imagechar($image, $font, $width, rand(10, 50 - $fh), $code[$i], $textcolor);
$width = $width + $fw;
}
imagejpeg($image, $location."/".md5($code).".jpg", 100);
imagedestroy($image);
return $code;
}
?>