laravel 图片验证码

  今天看见一个网站登录页面有个图片验证码,想想自己以前好像真没弄过这个玩意,正好现在有时间,准备用laravel来弄个图片验证码出来,不多BB,直接上代码

1、直接使用别人封装好的,composer下载一个

composer require gregwar/captcha

2、直接设置各种属性和输出图片

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;
use GregwarCaptchaCaptchaBuilder;
use GregwarCaptchaPhraseBuilder;

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、结果

以上就是本次内容的全部了

原文地址:https://www.cnblogs.com/chenhaoyu/p/10371263.html