font = __DIR__ . '/Elephant.ttf';
$this->charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';
$this->codelen = 4;
$this->width = 150;
$this->height = 50;
$this->fontsize = 20;
$this->createCode();
}
//生成随机码
private function createCode()
{
$_len = strlen($this->charset) - 1;
for ($i = 0; $i < $this->codelen; ++$i) {
$this->code .= $this->charset[mt_rand(0, $_len)];
}
}
//生成背景
private function createBg()
{
$this->img = imagecreatetruecolor($this->width, $this->height);
$color = imagecolorallocate($this->img, mt_rand(157, 255), mt_rand(157, 255), mt_rand(157, 255));
imagefilledrectangle($this->img, 0, $this->height, $this->width, 0, $color);
}
//生成文字
private function createFont()
{
$_x = $this->width / $this->codelen;
for ($i = 0; $i < $this->codelen; ++$i) {
$this->fontcolor = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156));
imagettftext($this->img, $this->fontsize, mt_rand(-30, 30), $_x * $i + mt_rand(1, 5), $this->height / 1.4, $this->fontcolor, $this->font, $this->code[$i]);
}
}
//生成线条、雪花
private function createLine()
{
//线条
for ($i = 0; $i < 6; ++$i) {
$color = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156));
imageline($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $color);
}
//雪花
for ($i = 0; $i < 100; ++$i) {
$color = imagecolorallocate($this->img, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255));
imagestring($this->img, mt_rand(1, 5), mt_rand(0, $this->width), mt_rand(0, $this->height), '*', $color);
}
}
//对外生成
public function outImg()
{
$this->createBg();
$this->createLine();
$this->createFont();
ob_start();
// 输出图像
imagepng($this->img);
$content = ob_get_clean();
imagedestroy($this->img);
return response($content, 200, ['Content-Length' => strlen($content)])->contentType('image/png');
}
//返回验证码
public function getCode()
{
return $this->code;
}
public function get_captcha(){
//验证码唯一标识
$uniqid = uniqid((string)mt_rand(100000, 999999));
//说明:这里生成的链接其实就是指向验证码显示方法
$src = url('show','','html',true).'?uniqid='.$uniqid;
$data = [
'src' => $src,
'uniqid' => $uniqid,
];
//这里你们可以根据自己封装的api接口返回方法返回数据
$this->success('获取成功',$data);
}
//显示页面
public function show($uniqid)
{
//存缓存,时间短一点,减少压力
Cache::set('captcha_' . $uniqid, $this->getCode(), 1200);
//输出图像
return $this->outImg();
}
/**
* 验证
* @param $data
* @return bool
*/
public function check($data){
//从缓存中根据uniqid取验证码
$code = Cache::get('captcha_' . $data['uniqid']);
if (!$code) {//没取到说明验证码过期
$this->error('验证码过期,请刷新后重试');
}
//对比缓存中的验证码和表单传递过来的验证码是否相等(大小写不敏感)
if (strcasecmp($data['code'],$code)!=0) {
$this->error('验证码错误');
}
return true;
}
}
访问get_captcha方法获得下面的数据
{
"code": 1,
"msg": "获取成功",
"time": "1685612554",
"data": {
"src": "http://****.com/api/captcha/show.html?uniqid=8830546478680a5c69b",
"uniqid": "8830546478680a5c69b"
}
}
请求src中的url获取验证图片
验证
public function check_verify(){
$data['code'] = $this->request->post('code');
$data['uniqid'] = $this->request->post('uniqid');
$captcha = new Captcha();
$captcha->check($data);
}