<?php
class CaptchaTool
{
//声明私有的静态方法
public static function draw(){
//按照素材创建画布
$img=imagecreatefromjpeg(PUBLIC_PATH."Captcha/captcha_bg".mt_rand(1,5).".jpg");
//获取画布 的大小
$width=imagesx($img);
$height=imagesy($img);
//设置画布的颜色 白色
$white=imagecolorallocate($img,255,255,255);
//设置画布的颜色 黑色
$blank=imagecolorallocate($img,0,0,0);
//绘制矩形
imagerectangle($img,0,0,$width-1,$height-1,$white);
//获取随机文字 声明一个字符串 在打乱顺序
$str="1234567890QWERTYUIOPLKJHGFDSAZXCVBNM";
$str=str_shuffle($str);
//随机获取文字
$code=substr($str,-4);
//记录当前的验证码 记录在session中
session_start();
$_SESSION["code"]=$code;
//绘制w文字
imagestring($img,5,50,4,$code,mt_rand(0,1)==1?$white:$blank);
//绘制随机线条
for($i=1;$i<2;$i++){
imageline($img,mt_rand(0,$width),mt_rand(0,$height),mt_rand(0,$width),mt_rand(0,$height),mt_rand(0,1)==1?$white:$blank);
}
//设置网页的格式
header("Content-type:image/jpeg");
//绘制画布
imagejpeg($img);
//释放资源
imagedestroy($img);
}
}
如整合期间有遇到什么问题 可以加群 858507220 一起讨论哦。