php算数验证码

2015-01-04 15:08
<?php
session_start();
//生成验证码图片
Header("Content-type: image/PNG");
$im = imagecreate(74,28);
$back = ImageColorAllocate($im, 255,255,255);
imagefill($im,0,0,$back); //背景
$red = ImageColorAllocate($im, 255,0,0); //字的颜色

srand((double)microtime()*1000000);
//生成4位数字

/*
for($i=0;$i<4;$i++){
$red = ImageColorAllocate($im, 255,0,0);
$authnum=rand(1,9);
$vcodes.=$authnum;
imagestring($im, 5, 2+$i*10, 1, $authnum, $red);
}
*/

$aya = rand(11,99);
$ayb = rand(1,99);
$vcodes = $aya + $ayb;
$authnum = $aya."+".$ayb."=?";

imagestring($im, 5, 10, 3, $authnum, $red);


for($i=0;$i<100;$i++) //加入干扰象素
{
$randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));
imagesetpixel($im, rand()%70 , rand()%30 , $randcolor);
}

ImagePNG($im);
ImageDestroy($im);

$_SESSION['authcode'] = md5($vcodes);
?>
^