法一:进入 https://packagist.org/ 组件网 搜索 gregwar/captcha
法二:用命令 composer require gregwar/captcha
或者:在项目中的composer.json 文件中加入
"require" :{
"gregwar/captcha": "1.*"
}
然后执行:composer update
使用:
添加路由 :
Route::get('/code/captcha/{tmp}','AdminLoginController@captcha');
控制器中(LoginController):
use GregwarCaptchaCaptchaBuilder;
use GregwarCaptchaPhraseBuilder;
方法:
public function captcha($tmp){
$phrase = new PhraseBuilder;
//设置验证吗位数
$code = $phrase->build(6);
//生成验证码图片build 对象 ,配置相应属性
$builder = new CaptchaBuilder($code,$phrase);
//设置背景颜色
$builder->setBackgroundColor(220,210,230);
$builder->setMaxAngle(25);
$builder->setMaxBehindLines(0);
$builder->setMaxFrontLines(0);
//可以设置图片宽度及字体
$builder->build($whth=100,$height=40,$font=null);
//获取验证码内容
$phrase = $builder->getPhrase();
//把内容存入session
Session::flash('code',$phrase);
//生成图片
header('Cache-Control:no-cache,must-revalidate');
header('Cotent-Type:image/jpeg');
$builder ->output();
}
模板中(login):
<input name="code" lay-verify="required" placeholder="验证码" type="text" class="layui-input" style="height: 40px; 150px;float: left;">
<a onclick="javascript:re_captcha()" style="margin-left: 20px">
<img src="{{URL('/code/captcha/1')}}" alt="" id="127ddf0de5a04167a9e427d883690ff6">
</a>
点击切换验证码的js
<script type="text/javascript">
function re_captcha() {
$url = "{{URL('/code/captcha')}}";
$url = $url+"/"+Math.random();
document.getElementById('127ddf0de5a04167a9e427d883690ff6').src = $url;
}
</script>