今天看見一個網站登錄頁面有個圖片驗證碼,想想自己以前好像真沒弄過這個玩意,正好現在有時間,准備用laravel來弄個圖片驗證碼出來,不多BB,直接上代碼
1、直接使用別人封裝好的,composer下載一個
composer require gregwar/captcha
2、直接設置各種屬性和輸出圖片
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Gregwar\Captcha\CaptchaBuilder; use Gregwar\Captcha\PhraseBuilder; class IndexController extends Controller { public function index() { return view('index'); } /** * 設置並輸出圖片 */ public function getCaptcha() { $phrase = new PhraseBuilder; // 設置驗證碼位數 $code = $phrase->build(5); // 生成驗證碼圖片的Builder對象,配置相應屬性 $builder = new CaptchaBuilder($code, $phrase); // 設置背景顏色25,25,112 $builder->setBackgroundColor(25, 25, 112); // 設置傾斜角度 $builder->setMaxAngle(25); // 設置驗證碼后面最大行數 $builder->setMaxBehindLines(10); // 設置驗證碼前面最大行數 $builder->setMaxFrontLines(10); // 設置驗證碼顏色 $builder->setTextColor(255, 255, 0); // 可以設置圖片寬高及字體 $builder->build($width = 150, $height = 40, $font = null); // 獲取驗證碼的內容 $phrase = $builder->getPhrase(); // 把內容存入session session()->put('CAPTCHA_IMG', $phrase); // 生成圖片 header('Cache-Control: no-cache, must-revalidate'); header('Content-Type:image/jpeg'); $builder->output(); } }
3、路由設置
4、視圖層
5、結果
以上就是本次內容的全部了