用php显示验证码

2025-10-26 13:09:05

1、//一、获取生成图像的数据,如验证码的四位字符(如何保障数据随机性的处理)。$authnum=random(4);

2、//二、新建一个图像(注意生成图像格式)。Header("Content-type: image/PNG");$im = imagecreate(55,18); //imagecreate() 新建图像,大小为 x_size 和 y_size 的空白图像。

3、//三、生成填充颜色$red = ImageColorAllocate($im, 153,51,0); //设置背景颜色$white = ImageColorAllocate($im, 255,204,0);//设置文字颜色$gray = ImageColorAllocate($im, 102,102,0); //设置杂点颜色

4、//四、填充背景颜色imagefill($im,55,18,$red);

5、//五、填充数据字符for ($i = 0; $i < strlen($authnum); $i++){    imagestring($im, 6, 13*$i+4, 1, substr($authnum,$i,1), $white); }

6、//六、填充杂色for($i=0;$i<100;$i++) imagesetpixel($im, rand()%55 , rand()%18 , $gray); //加入干扰象素

7、//七、输出图像ImagePNG($im); //以 PNG 格式将图像输出到浏览器或文件ImageDestroy($im);//销毁一图像

8、//产生随机数函数function random($length) {    $hash = '';    $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';    $max = strlen($chars) - 1;    mt_srand((double)microtime() * 1000000);    for($i = 0; $i < $length; $i++) {        $hash .= $chars[mt_rand(0, $max)];    }    return $hash;}

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢