控制器
<?php
namespace App\Http\Controllers\Index;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Gregwar\Captcha\CaptchaBuilder;
use Illuminate\Support\Facades\Input;
use Session;
class RegisterController extends Controller
{
/*生成验证码*/
public function captcha()
{
//生成验证码图片的Builder对象,配置相应属性
$builder = new CaptchaBuilder;
//dd($builder);
//可以设置图片宽高及字体
$builder->build($width = 100, $height = 40, $font = null);
//获取验证码的内容
$phrase = $builder->getPhrase();
//把内容存入session
Session::put('milkcaptcha', $phrase);
//生成图片
header("Cache-Control: no-cache, must-revalidate");
header('Content-Type: image/jpeg');
$builder->output();
}
}
视图层
<div class="yzm">
<input type="text" name="yan" placeholder="验证码">
<span>
<img src="{{url('index/captcha/1')}}" alt="验证码" style="height: 100px; height: 40px;" class="yanzhengma" id="c2c98f0de5a04167a9e427d883690ff6">
</span>
</div>
<script>
/*生成验证码*/
$(function(){
$("input[name=captcha]").blur(function () {
var captcha=$(this).val();
if(captcha=='')
{
alert('验证码不能为空');
return;
}
})
$('.yanzhengma').click(function () {
var _this=$(this);
var url="{{url('index/captcha')}}";
var url=url+'/'+Math.random();
document.getElementById('c2c98f0de5a04167a9e427d883690ff6').src=url;
})
})
</script>