<?PHP
session_start();$_SESSION['authnum']=mt_rand(1000,9999); //获取一个四位随即数//生成验证码图片 
Header("Content-type: image/PNG");  $im = imagecreate(62,20); 
$black = ImageColorAllocate($im, 0,0,0); 
$white = ImageColorAllocate($im, 255,255,255); 
$gray = ImageColorAllocate($im, 200,200,200); 
imagefill($im,68,30,$gray); //将四位随机验证码绘入图片
$authnum=$_SESSION['authnum'];
imagestring($im, 5, 14, 2,$authnum, $white);for($i=0;$i<200;$i++)   //加入干扰象素 

    $randcolor = ImageColorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
    imagesetpixel($im, mt_rand()%70 , mt_rand()%30 , $randcolor); //在image图像中用$randcolor颜色在 x,y 坐标上画一个点

ImagePNG($im); 
ImageDestroy($im); 
?>