composer require gregwar/captcha
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
//引用对应的命名空间
use Gregwar\Captcha\CaptchaBuilder;
use Illuminate\Support\Facades\Session;
class KitController extends Controller
{
/**
* Display a listing of the resource.
*
* @return Response
*/
public function captcha($tmp)
{
//生成验证码图片的Builder对象,配置相应属性
$builder = new CaptchaBuilder;
//可以设置图片宽高及字体
$builder->build($width = 100, $height = 40, $font = "captcha1.ttf");
//获取验证码的内容
$phrase = $builder->getPhrase();
//把内容存入session
Session::flash('milkcaptcha', $phrase);
//生成图片
header("Cache-Control: no-cache, must-revalidate");
header('Content-Type: image/jpeg');
ob_clean();
flush();
$builder->output();
}
public function check()
{
$code = Session::get("milkcaptcha");
dump($code);
$key = \Input::get("key");
dump($key);
if ($code == $key) {
//用户输入验证码正确
dump("success");
} else {
//用户输入验证码错误
dump("error");
}
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return View("kit.index");
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
<form action="/kit/check">
<input type="text" name="key">
<input type="hidden" name="_token" value="{{ csrf_token() }}" />
<img src="/kit/captcha/fdsaf" />
<input type="submit">
</form>
Route::get('/kit/captcha/{tmp}', 'KitController@captcha'); //显示验证码
Route::get('/kit/check', 'KitController@check'); //检查是否正确
Route::get('/kit/index', 'KitController@index'); //