<?php
$im=imagecreatetruecolor(200,50);//验证码区域大小
//创建背景色
$color=imagecolorallocate($im,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
//填充画布
imagefill($im,0,0,$color);
//随机生成验证码字符
$str='123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
$captcha='';
for($i=0;$i<5;$i++)
{
$captcha .=$str[mt_rand(0,strlen($str)-1)];
}
//设置文字颜色
$str_color=imagecolorallocate($im,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));
//将文字写入图片
imagestring($im,5,80,20,$captcha,$str_color);
//增加干扰线(要在字上层)
for($j=0;$j<5;$j++)
{
$line_color=imagecolorallocate($im,mt_rand(100,200),mt_rand(100,200),mt_rand(100,200));
//划线
imageline($im,mt_rand(0,200),mt_rand(0,50),mt_rand(0,200),mt_rand(0,50),$line_color);
}
//画点
for($i=0;$i<300;$i++)
{
$line_pixel=imagecolorallocate($im,mt_rand(100,200),mt_rand(100,200),mt_rand(100,200));
imagesetpixel($im,mt_rand(0,200),mt_rand(0,50),$line_pixel);
}
header('Content-type:image/png');
imagepng($im);
php简易验证码
这篇博客介绍了如何使用PHP创建一个带有干扰元素的随机验证码。代码首先定义了验证码区域大小,然后生成了背景色和随机字符串。接着,设置了文字颜色并将字符串写入图片。为了增加干扰,代码绘制了随机线条和像素点。最后,设置了HTTP头并输出为PNG图像。
摘要由CSDN通过智能技术生成