/* 验证码长度 */
$letters = mb_strlen($word);
$im = imagecreate(100,100); // 画一张指定宽高的图片
$back = ImageColorAllocate($im, 245,245,245); // 定义背景颜色
imagefill($im,0,0,$back); //把背景颜色填充到刚刚画出来的图片中
$vcodes = "";
srand((double)microtime()*1000000);
$wordArr = str_split($word);
for($i=0;$i<$letters;$i++){
$font = ImageColorAllocate($im, rand(100,255),rand(0,100),rand(100,255)); // 生成随机颜色
imagestring($im, 5, (($this->width - (imagefontwidth(5) * $letters)) / 2)+$i*10, (($this->height - imagefontheight(5)) / 2), $wordArr[$i], $font);
}
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); // 画像素点函数
}
header('Expires: Thu, 01 Jan 1970 00:00:00 GMT');
// HTTP/1.1
header('Cache-Control: private, no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0, max-age=0', false);
// HTTP/1.0
header('Pragma: no-cache');
Header("Content-type: image/PNG");
ImagePNG($im);
ImageDestroy($im);