![]() |
|
首页 │ Apache │ Linux│ Java│ MySQL│ 注册│帮助 | |||
[PHP]
<?php
session_start();
$authnum=rand(1000,9999);//生成四位随机整型验证码
setcookie('auth',$authnum);
$_SESSION['auth']=$authnum;
//生成验证码图片
Header("Content-type: image/PNG");
$im = imagecreate($wid=11*strlen($authnum),20);
imagefill($im,68,30,ImageColor($im,'#FFFFFF'));
//将四位整数验证码绘入图片
//位置交错
for ($i = 0; $i < strlen($authnum); ++$i)
{
$i%2 == 0?$top = 1
top = 5;
imagestring($im, 5, 10*$i+6, $top, substr($authnum,$i,1),ImageColor($im,'#FF0000'));
}
for($i=0;$i<150;$i++) //加入干扰象素
{
imagesetpixel($im, rand()%90 , rand()%30 , ImageColor($im,'#FF9900'));
}
ImagePNG($im);
ImageDestroy($im);
function ImageColor($im,$color)
{
preg_match_all("/([0-f]){2,2}/i",$color,$out);
if(count($out[0])!=3)$out[0]=array_pad ($out[0],3,0);
return ImageColorAllocate($im, hexdec($out[0][0]),hexdec($out[0][1]),hexdec($out[0][2]));
}
?>[/PHP]
function ImageColor($im,$color)
{
preg_match_all("/([0-f]){2,2}/i",$color,$out);
if(count($out[0])!=3)$out[0]=array_pad ($out[0],3,0);
return ImageColorAllocate($im, hexdec($out[0][0]),hexdec($out[0][1]),hexdec($out[0][2]));
}
ImageColor($im,'#FF9900');
函数中最后一句
$out[0][0]=F $out[0][1]=F $out[0][3]=0
对吧?
最后在ImageColorAllocate中,将他们转为十进制,对吧?为何要这样做?这样颜色是无法随机变化的,不是多此一举吗?
PS:代码是由纯粹误会提供,谢谢他了!

