php中动态生成不同字体颜色的png格式验证图片

轉載於 : http://dev.csdn.net/article/80/80154.shtm
<?
session_start();
//生成验证码图片
Header("Content-type: image/PNG");
$authnum=$_SESSION["authnum"];
srand((double)microtime()*1000000);
$len = strlen($authnum)*12+10;
$im = imagecreate($len,25);
$color[]= array ();
$color[1] = ImageColorAllocate($im, 0x00,0x00,0x00);
$color[2] = ImageColorAllocate($im, 0x00,0x00,0xff);
$color[3] = ImageColorAllocate($im, 0xff,0x33,0x00);
$color[4] = ImageColorAllocate($im, 0x00,0x00,0x99);
$color[5] = ImageColorAllocate($im, 0xff,0x00,0xff);
$color[6] = ImageColorAllocate($im, 0x99,0x66,0xff);
$color[7] = ImageColorAllocate($im, 0x00,0x99,0x99);
$color[8] = ImageColorAllocate($im, 0xff,0xff,0x00);

if($background)
{
 $r = substr($background, 0, 2);
 $g = substr($background, 2, 2);
 $b = substr($background, 4, 2);
 $bg = ImageColorAllocate($im, hexdec("0x".$r),hexdec("0x".$g),hexdec("0x".$b));
}
else
{
 $bg = ImageColorAllocate($im, 0xcc,0xcc,0xff);
}
imagefill($im, 0, 0, $bg);

for($i=0,$x=5;$i<strlen($authnum);$i++)
{
 imagestring($im, 5, $x, rand(2,8), $authnum[$i], $color[rand(1,8)]);
 $x+=12;
}
for($i=0;$i<100;$i++)   //加入干扰象素
{
    $randcolor = ImageColorallocate($im,rand(100,255),rand(100,255),rand(100,255));
    imagesetpixel($im, rand()%$len , rand()%30 , $randcolor);
}
ImagePNG($im);
ImageDestroy($im);
?>

你可能感兴趣的:(PHP,.net)