Laravel5.2不带验证码类,使用第三方类

在laravel5中默认是没有提供验证码的,这里咱们须要使用第三方提供的库:gregwar/captchaphp

经过composer安装:laravel

在composer.json的require中加入"gregwar/captcha": "dev-master",具体代码以下git

"require": {
        "laravel/framework": "5.0.*",
        "gregwar/captcha": "dev-master"
    },

而后运行:php composer.phar update命令github

去github下载:json

下载后将包放至vendor下目录结构以下,以后在composer.json文件中加入自动加载:session

"autoload": {
        "classmap": [
            "database"
        ],
        "psr-4": {
            "WangDong\\": "app/",
            "Gregwar\\Captcha\\": "vendor/Captcha/"
        }
    },

而后运行composer的dump-autoload命令app

在项目中使用composer

<?php namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

//引用对应的命名空间
use Gregwar\Captcha\CaptchaBuilder;
use 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 = null);
        //获取验证码的内容
        $phrase = $builder->getPhrase();

        //把内容存入session
        Session::flash('milkcaptcha', $phrase);
        //生成图片
        header("Cache-Control: no-cache, must-revalidate");
        header('Content-Type: image/jpeg');
        $builder->output();
    }

}

API方法:字体

1.getPhrase() 取得验证码的值ui

2.build($width = 150, $height = 40, $font = null)

根据给定的大小和字体建立验证码

3.save($filename, $quality = 80)

验证码存入到一个给定的文件  

$builder->save('out.jpg');

4.output($quality = 80),输出验证码到页面

5.setBackgroundColor($r, $g, $b), 设置验证码背景颜色

6.setBackgroundImages(array($imagepath1, $imagePath2)) 设置验证码背景图片

相关文章
相关标签/搜索