vicens / captcha by vicens

A simple captcha component for laravel5 applications.
749
13
2
Package Data
Maintainer Username: vicens
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: 2024-04-24 03:04:41
Package Statistics
Total Downloads: 749
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 13
Total Watchers: 2
Total Forks: 5
Total Open Issues: 0

Build Status Scrutinizer Code Quality Latest Stable Version Total Downloads License

简介

一个简单的laravel5图形验证码扩展包

环境要求

  1. PHP >= 5.5
  2. php-gd
  3. illuminate\support ^5.1
  4. symfony/http-foundation >= 2.0

安装

使用composer安装扩展包

先通过composer安装扩展包到项目中

composer require vicens/captcha

注册服务提供者和别名(Laravel5.5无需手动注册)

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();

开源协议

MIT license.