كود PHP:
<?
session_start();
make_captcha();
function make_captcha($lx=80,$ly=25,$nb_noise=25,$noise=0) {
$font_file=getcwd()."/comic.ttf";
$private_key = $_SESSION['authkey']= rand(100, 999);
$_SESSION['authkey_expire']= time()+60*2;
$long=strlen($private_key);
$image = imagecreatetruecolor($lx,$ly);
$back=ImageColorAllocate($image,intval(rand(224,255)),intval(rand(224,255)),intval(rand(224,255)));
ImageFilledRectangle($image,0,0,$lx,$ly,$back);
if ($noise) { // rand characters in background with random position, angle, color
for ($i=0;$i<$nb_noise;$i++) {
$size=intval(rand(6,14));
$angle=intval(rand(0,360));
$x=intval(rand(10,$lx-10));
$y=intval(rand(0,$ly-5));
$color=imagecolorallocate($image,intval(rand(160,224)),intval(rand(160,224)),intval(rand(160,224)));
$text=chr(intval(rand(45,250)));
ImageTTFText ($image,$size,$angle,$x,$y,$color,$font_file,$text);
}
}
else { // random grid color
for ($i=0;$i<$lx;$i+=10) {
$color=imagecolorallocate($image,intval(rand(160,224)),intval(rand(160,224)),intval(rand(160,224)));
imageline($image,$i,0,$i,$ly,$color);
}
for ($i=0;$i<$ly;$i+=10) {
$color=imagecolorallocate($image,intval(rand(160,224)),intval(rand(160,224)),intval(rand(160,224)));
imageline($image,0,$i,$lx,$i,$color);
}
}
// private text to read
for ($i=0,$x=15; $i<$long;$i++) {
$r=intval(rand(0,128));
$g=intval(rand(0,128));
$b=intval(rand(0,128));
$color = ImageColorAllocate($image, $r,$g,$b);
$shadow= ImageColorAllocate($image, $r+128, $g+128, $b+128);
$size=intval(rand(12,17));
$angle=intval(rand(-30,30));
$text=strtoupper(substr($private_key,$i,1));
if(!function_exists('ImageTTFText'))
{
imagestring($image,7,$x+2,5,$text,$shadow);
imagestring($image,7,$x,3,$text,$color);
}
else
{
@ImageTTFText($image,$size,$angle,$x+2,22,$shadow,$font_file,$text);
@ImageTTFText($image,$size,$angle,$x,20,$color,$font_file,$text);
}
$x+=$size+6;
}
if ($imagetype=="jpg")
imagejpeg($image);
else
imagepng($image);
ImageDestroy($image);
}
?>