1.composer执行下面命令
composer require gregwar/captcha
2.后端代码调用
<?php namespace App\Http\Controllers; use Gregwar\Captcha\CaptchaBuilder; use Session; class UserController extends Controller { //验证码 public function VCode() { //生成验证码图片的Builder对象,配置相应属性 $builder = new CaptchaBuilder; //可以设置图片宽高及字体 $builder->build($width = 100, $height = 40, $font = null); //获取验证码的文本内容 $phrase = $builder->getPhrase(); //把内容存入session // Session::flash('milkcaptcha', $phrase); //生成图片 return $builder->output(); } }
3.在routeslweb.php配置路由
Route::get('user/VCode', 'UserController@VCode');
4.前端调用
<img src="{{ URL('user/VCode') }}" alt="验证码" title="刷新图片" width="100" height="40" onclick="this.src=this.src+'?k='+Math.random();">