//将随机数存入session中
session_start();
$_SESSION['authnum']=substr(md5(time()),rand(0,25),5);
//绘制图片
Header("Content-type: image/PNG");
$width = 100;
$height = 40;
$im = imagecreate($width,$height); //制定图片背景大小
//第一次对 imagecolorallocate() 的调用会填充背景色
$gray = ImageColorAllocate($im, 220,220,220); //背景为灰色
$black = ImageColorAllocate($im, 0,0,0);
//array imagettftext ( resource image, float size, float angle, int x, int y, int color, string fontfile, string text )
imagettftext($im,20,-10,10,23,$black,'C:\WINDOWS\Fonts\arial.ttf',$_SESSION['authnum']);
//加入干扰象素
for($i=0;$i<100;$i++){
$randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));
imagesetpixel($im, rand(0,$width) , rand(0,$height) , $randcolor);
}
imageline($im,rand(0,$width) , rand(0,$height) ,rand(0,$width) , rand(0,$height) ,$randcolor);
//图片输出
ImagePNG($im);
ImageDestroy($im);
?>



