<?php
header ("Content-type: image/png");
session_start();
$_SESSION[’validate_code’] = strtoupper(substr(md5(rand()),20,6));
$im = @imagecreate (130, 40)
or die ("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate ($im, 200, 200, 200);
//设置干扰像素,防止被OCR
for ($i=0;$i<=128;$i++)
{
$point_color = imagecolorallocate ($im, rand(0,255), rand(0,255), rand(0,255));
imagesetpixel($im,rand(2,128),rand(2,38),$point_color);
}
//逐个画上验证码字符
for ($i=0;$i<=5;$i++)
{
$text_color = imagecolorallocate ($im, rand(0,255), rand(0,128), rand(0,255));
$x = 10 + $i * 20;
$y = rand(5,20);
imagechar ($im, 5, $x, $y, $_SESSION[’validate_code’]{$i}, $text_color);
}
//输出PNG图像
imagepng ($im);
imagedestroy ($im);
?>