验证码
验证码:captcha,全自动区分人与计算机的图灵测试。
回顾:生成验证码需要经过:画画布、生成干扰线、生成噪点、生成验证码、生成验证码存入session、输出图片。
常见的验证码类型:字符验证码、短信验证码、电话验证码、12306类型验证码、拖拽验证码等。
1、验证码依赖安装
官方手册:链接
环境要求:php>=7.2,需要开启GD库,同时需要开启fileinfo和mbstring扩展\
1.1.Composer方法安装代码依赖包:
composer require mews/captcha
1.2修改配置文件:
配置:修改配置文件: config/app.php
配置provider信息, 添加一行信息: Mews\Captcha\CaptchaServiceProvider::class,
'providers'=>[
//......
Mews\Captcha\CaptchaServiceProvider::class,
]

1.3.配置别名aliases键,添加一个别名记录
‘Captcha’ => Mews\Captcha\Facades\Captcha::class,
'aliases' => [
// ...
'Captcha' => Mews\Captcha\Facades\Captcha::class,
]
如果(可选)需要定义自己的配置,则需要生成配置文件:
php artisan vendor:publish
//如果希望定制验证码样式,就需要调用如下示例代码,
//此命令生成验证码配置文件
php artisan vendor:publish
//注意 :选12
发布之后会在config目录下找到对应的配置文件:
如果需要自定义配置(如长度、宽高等),可以修改配置文件config/captcha.php文件(当前默认是9个长度),可以去进行修改。:
2、案例
2.1.需要在页面上显示出来
视图中:
//1: 显示图片 本质是生成 img标签
{!!captcha_img()!!}
//2:可以进行点击刷新验证码 获取验证码的url
<img src="{{captcha_src()}}" alt="" onclick="this.src=this.src+'?rand='+Math.random()">
2.2 验证码验证操作 captcha
官方手册链接
注意:验证码有效性验证规则,手册里是没有的,如果使用mews验证码包的话,其验证码验证规则就是 captcha
public function yzmAction(Request $request)
{
$request->validate([
'number'=>['required','captcha'],
]);
}
2.3 翻译问题(参考laravel基础->自动验证部分)
补充:自定义验证码类文件
<?php
namespace App\Libs;
/**
* 生成验证码
* Class VerificationCode
*/
class VerificationCode {
protected $image;
protected $width;
protected $height;
protected $data;
protected $num;
protected $path;
protected $strvalue="";
/**
* @param int $width
* @param int $height
* @param int $num
* @param int $font_size
* @param int $model
* @param int $point_num
* @param int $line_num
*/
public function __construct($width,$height,$point_num,$line_num,$num=4,$font_size=6,$model=1){
$this->width = $width;
$this->height = $height;
$this->num = $num;
//创建白色底图
$this->image = imagecreatetruecolor($this->width,$this->height);
$bjcolor = imagecolorallocate($this->image,255,255,255);
imagefill($this->image,0,0,$bjcolor);
//填入验证码
for($i = 0;$i<$this->num;$i++){
$content = $this->CreateRandData($this->DataModel($model));
$fontcolor = imagecolorallocate($this->image,rand(0,120),rand(0,120),rand(0,120));
$x = rand(5,10)+$i*$this->width/$this->num;
$y = rand(5,10);
imagestring($this->image,$font_size,$x,$y,$content,$fontcolor);
}
//产生干扰
$this->point($point_num);
$this->line($line_num);
}
/**
* 验证码产生数据模式
* @param int $model
* @return string
*/
protected function dataModel($model){
$str = "";
switch ($model){
//纯数字模式
case 1:$str = "0123456789";
break;
//纯小写字母模式
case 2:$str = "qwertyuiopasdfghjklzxcvbnm";
break;
//大小写随机模式
case 3:$str = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
break;
//数字字母随机模式
case 4:$str = "0123456789qwertyuiopasdfghjklzxcvbnm";
break;
//数字大小写字母随机模式
case 5:$str = "0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
break;
//数字字母安全识别模式
case 6:$str = "23456789qwertyupasdfghjkzxcvbnm";
break;
}
return $str;
}
protected function createRandData($str){
$s = substr($str,rand(0,strlen($str))-1,1);
$this->strvalue.=$s;
return $s;
}
/**
* 用于输出图片
*/
public function showImage(){
$newName = md5(time().$this->strvalue).".png";
$this->path = 'temp/'.$newName;
header('content-type:image/jpeg');
imagejpeg($this->image,$this->path);
return url($this->path);//在laravel框架中才有url这个方法,其他框架请替换这部分
}
/**
* 用于
* @return string
*/
public function value(){
return $this->strvalue;
}
/**
* 产生点干扰
* @param int $point_num 干扰点数目
*/
protected function point($point_num=0){
for($i=0;$i<$point_num;$i++){
$pointcolor = imagecolorallocate($this->image,rand(50,200),rand(50,200),rand(50,200));
imagesetpixel($this->image,rand(1,$this->width-1),rand(1,$this->height),$pointcolor);
}
}
/**
* 产生线干扰
* @param int $line_num 干扰线数目
*/
protected function line($line_num=0){
for($i=0;$i<$line_num;$i++){
$linecolor = imagecolorallocate($this->image,rand(80,220),rand(80,220),rand(80,220));
imageline($this->image,rand(1,$this->width-1),rand(1,$this->height-1),rand(1,$this->width-1),rand(1,$this->height-1),$linecolor);
}
}
/**
* 析构函数,销毁图片
*/
public function __destruct(){
imagedestroy($this->image);
}
}
public function getVerCode(){
$image = new VerificationCode(80,32,200,5,4,4,6);
session(["code"=>$image->value()]);
return $image->showImage();
}
