|
废话不多说直接上代码
1.PHP代码:
class verifyCode
{
public static function codeConfig(){
return [
'codeStr' => //验证码随机字符
['0', '1', '2', '3', '4', '5', '6',' 7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z','a',
'b','c','d','e','f','g','h','i','j','k','l',
'm','n','o','p','q','r','s','t','u','v','w',
'x','y','z'],
//设置画布大小
'imageWidth' => 100,
//设置画布高度
'imageHight'=>40,
//干扰点开启,true开启。false关闭
'setPoint' => true,
//干扰点个数
'setPointCount'=>200,
//干扰线开启,true开启。false关闭
'setLine' => true,
//干扰线条数
'setLineCount'=>3,
];
}
public static function craetCode(){
//配置项加载
$config = self::codeConfig();
$configStrCount = count($config['codeStr']);
//创建画布
$image = imagecreatetruecolor($config['imageWidth'], $config['imageHight']);
//1.创建画布背景色
$imageBgColor = imagecolorallocate($image, 250, 255, 255);
//2.背景色填充画布
imagefill($image, 0, 0, $imageBgColor);
//3.添加验证码
$strCode = '';
for($i=0;$i<4;$i++){
$strtmp = $config[&#39;codeStr&#39;][rand(0,$configStrCount-1)];
//4.设置字体大小和颜色
$fontColor = imagecolorallocate($image, rand(0,50),rand(0,50), rand(0,50));
imagestring($image, 20, $i*20+10, $i*1+10,$strtmp, $fontColor);
$strCode.= strtolower($strtmp);
}
//5.存储session
session()->put(&#39;code&#39;,$strCode);
//6.添加干扰点
if ($config[&#39;setPoint&#39;]) {
for($i=0;$i<$config[&#39;setPointCount&#39;];$i++){
$setPointColor = imagecolorallocate($image, rand(200,255), rand(200,255), rand(200,255));//干扰点颜色
imagesetpixel($image, rand(1,$config[&#39;imageWidth&#39;]), rand(1,$config[&#39;imageHight&#39;]), $setPointColor);
}
}
//7.添加干扰线
if ($config[&#39;setLine&#39;]) {
for($i=0;$i<$config[&#39;setLineCount&#39;];$i++){
$setLineColor = imagecolorallocate($image, rand(200,255), rand(200,255), rand(200,255));//干扰点颜色
imageline($image, rand(0,$config[&#39;imageWidth&#39;]), rand(0,$config[&#39;imageHight&#39;]), rand(0,$config[&#39;imageWidth&#39;]), rand(0,$config[&#39;imageHight&#39;]), $setLineColor);
} # code...
}
//8.输出图片验证码
header(&#34;Content-Type:image/png&#34;);
imagepng($image);
//9.销毁画布
imagedestroy($image);
}
}
2.效果图
 |
|