
Laravel安装验证码有很多方式,一种是mews/captcha,还有一种是gregwar/captcha,前者参考这篇文章 /adetail/275.html
本文主要介绍gregwar/captcha安装和使用方法
步骤一:使用composer安装
composer require gregwar/captcha

步骤二、使用方法
创建验证码
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\CheckCaptcha;
use Gregwar\Captcha\CaptchaBuilder;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
//创建代码
public function captcha(Request $request){
$builder = new CaptchaBuilder();
$builder->build(150,47);
// 设置背景颜色
$builder->setBackgroundColor(220, 210, 230);
$builder->setMaxAngle(25);
$builder->setMaxBehindLines(0);
$builder->setMaxFrontLines(0);
//可以设置图片宽高及字体
$builder->build($width = 100, $height = 50, $font = null);
//获取验证码内容
$phrase = $builder->getPhrase();
//把内容存入session 存储验证码
$request->session()->flash('captchaSession', $phrase);
//清除缓存
ob_clean();
//把验证码数据以jpeg图片的格式输出
return response($builder->output())->header('Content-type','image/jpeg');
}
?>判断是否正确
<?php
Public function login(){
$code = $request->input("yzm"); //验证码
if (strtolower(Session::get('phrase')) ==strtolower($code)) {
Session::forget('phrase');
return '验证码正确';
} else {
//用户输入验证码错误
return $this->jsonData(ApiErrDesc::ERROR_YZM[0],ApiErrDesc::ERROR_YZM[1]);
}
}
?>