| Package Data | |
|---|---|
| Maintainer Username: | simonetoo | 
| Maintainer Contact: | 521287718@qq.com (vicens) | 
| Package Create Date: | 2016-04-25 | 
| Package Last Update: | 2019-03-27 | 
| Home Page: | |
| Language: | PHP | 
| License: | MIT | 
| Last Refreshed: | 2025-10-25 03:01:07 | 
| Package Statistics | |
|---|---|
| Total Downloads: | 771 | 
| Monthly Downloads: | 1 | 
| Daily Downloads: | 0 | 
| Total Stars: | 13 | 
| Total Watchers: | 1 | 
| Total Forks: | 5 | 
| Total Open Issues: | 0 | 
一个简单的laravel5图形验证码扩展包
先通过composer安装扩展包到项目中
composer require vicens/captcha
在config/app.php 配置文件的providers数组中,注册服务提供者:
'providers' => [
    // Other service providers...
    \Vicens\Captcha\Providers\CaptchaServiceProvider::class
]
如果你使用Captcha别名的话,在aliases数组中注册别名:
'aliases' => [
    // Other facades...
    'Captcha' => \Vicens\Captcha\Facades\Captcha::class
]
如果你想使用自己的配置,你可以执行以下命令发布配置文件config/captcha.php:
php artisan vendor:publish --provider=\Vicens\Captcha\Providers\CaptchaServiceProvider
return array(
    /**
     * 调试模式(不验证验证码的正确性), 设置为null时, 取值为app.debug
     *
     * @var bool|null
     */
    'debug' => env('CAPTCHA_DEBUG', false),
    /**
     * 验证码的访问路径
     *
     * @var string
     */
    'route' => '/captcha',
    /**
     * 路由名
     *
     * @var string
     */
    'name' => 'captcha',
    /**
     * 中间件名,必须开启session
     *
     * @var string
     */
    'middleware' => 'web',
    /**
     * 默认验证码长度
     *
     * @var int
     */
    'length' => 4,
    /**
     * 验证码字符集
     *
     * @var string
     */
    'charset' => 'abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ123456789',
    /**
     * 是否开启严格模式(区分大小写)
     *
     * @var bool
     */
    'strict' => false,
    /**
     * 默认验证码宽度
     *
     * @var int
     */
    'width' => 150,
    /**
     * 默认验证码高度
     *
     * @var int
     */
    'height' => 40,
    /**
     * 指定文字颜色
     *
     * @var string
     */
    'textColor' => null,
    /**
     * 文字字体文件
     *
     * @var string
     */
    'textFont' => null,
    /**
     * 指定图片背景色
     *
     * @var string
     */
    'backgroundColor' => null,
    /**
     * 开启失真模式
     *
     * @var bool
     */
    'distortion' => true,
    /**
     * 最大前景线条数
     *
     * @var int
     */
    'maxFrontLines' => null,
    /**
     * 最大背景线条数
     *
     * @val int
     */
    'maxBehindLines' => null,
    /**
     * 文字最大角度
     *
     * @var int
     */
    'maxAngle' => 8,
    /**
     * 文字最大偏移量
     *
     * @var int
     */
    'maxOffset' => 5
);
use \Vicens\Captcha\Facades\Captcha;
$image = Captcha::make();
$image = Captcha::setConfig($config)->make();
$image = Captcha::width(100)->height(40)->make();
直接返回Response对象:
$image->response();
直接输出给浏览器:
$image->output();
输出img标签:
Captcha::html($width, $height);
返回base64编码:
$image->getBase64();
返回base64Url地址:
$image->getDataUrl();
返回图片二进制内容:
$image->getContent();
保存图片到服务器:
$image->save($filename);
仅测试验证码的正确性:
Captcha::test($input);
检测验证码的正确性,并且从缓存中删除验证码:
Captcha::check($input);
在路由上使用:
Route::post('login','LoginController@login')->middleware(\Vicens\Captcha\Middleware\CaptchaMiddleware::class);
在控制器中使用:
public function __constructor(){
   $this->middleware(\Vicens\Captcha\Middleware\CaptchaMiddleware::class)->only(['login', 'register']);
}
在控制器中使用:
$this->validation([
   'code' => 'captcha'
]);
在Request中使用:
public function rules()
{
   return [
       'code' => 'captcha'
   ];
}
返回验证码URL:
Captcha::url();
Captcha::src();
返回验证码img标签:
Captcha::image();
返回可点击切换验证码的img标签:
Captcha::clickableImage();