hpd/captcha

Laravel 验证码代码包

v1.0.0 2022-12-08 07:38 UTC

This package is auto-updated.

Last update: 2024-09-10 13:09:02 UTC


README

预览

preview

安装

使用 composer 安装此包

composer require hpd/captcha

使用

不需要在 config/app.php 的 providers 数组中添加 CaptchaServiceProvider。

配置

要使用自己的设置,首先发布 config/config.php。然后根据需要自定义配置属性。

$ php artisan vendor:publish config/config.php

return [
    'disable' => env('CAPTCHA_DISABLE', !str_contains(env('APP_ENV', 'local'), 'prod')),
    'characters'=>[
        'lowercase'=>'abxefghijklymcnopsqrtuvd',
        'uppercase'=>'AXOBEPFCYDGWSJKZHIRULMNQTV',
        'digits'=>'6302581497'
    ],
    'default' => [
        'length' => 5,
        'bgColor'=>'#FFFFFF',
        'color'=>'random',
        'flake'=>true,
        'flakeColor'=>'#BBC6C8',
        'sensitive'=>false,
        'digits'=>true,
        'uppercase'=>true,
        'lowercase'=>true,
        'alpha'=>10,
        'blur'=>true
    ],
    ...
];

属性

以下属性可以从发布的 config.php 文件中自定义。

    protected string $bgColor="#000000";
    protected string $color="#FFFFFF";
    protected string $flakeColor="#FFFFFF";
    protected bool $blur=false;
    protected int $alpha=0;
    protected bool $flake=false;
    protected bool $line=false;
    protected bool $difficult= false;
    protected int $length=5;
    protected int $width=110;
    protected int $height=40;
    protected int $expire=60;
    protected bool $sensitive=false;
    protected array $characters;
    protected bool $lowercase=true;
    protected bool $uppercase=false;
    protected bool $digits=false;
    protected string $fontDir='';
    protected string $font='libre.ttf';
    protected int $fontSize=24;

如何使用

您可以在项目中使用以下辅助函数来获取验证码图片。

    captcha(); // returns image
    
    captcha_get_src()// returns image source(URl)

    captcha_get_html()// returns img html element

使用期望配置

//If omitted configuration optional parameter, 'default' configuration will be used.

    captcha('default'); // returns image
    
    captcha_get_src('easy')// returns image source(URl)

    captcha_get_html('dark')// returns img html element

示例

获取验证码图片 src

    <img src="{{!! captcha_get_src()!!}}" titile="Captcha" alt="Captcha">

获取 img html 元素

    <div>
        {{!! captcha_get_html()!!}}
    </div>

验证

会话模式

    Route::post('captcha_check', function() {
            $validator = validator()->make(request()->all(), 
                ['captcha' => 'required|captcha'];
            );
            if ($validator->fails()) {
                echo '<p style="color: #ff0000;">Incorrect!</p>';
            } else {
                echo '<p style="color: #00ff00;">Matched </p>';
            }
   
    });

无状态模式

您可以从此 URL 获取图片和代码: https:///captcha/api/default 返回的结果

     [
        'code'=>$hash,
         'sensitive'=>$this->sensitive,
          'image'=>'data:image/png;base64,'.$this->createBase64FromImg($this->image)
          ]

然后,为了验证验证码,您应该将 'code' 发送到验证器。

将配置类型设置为与之前选择的类型相同。

    $validator = validator()->make(request()->all(),
        ['captcha' => 'required|captcha_api:'. request('code') . ',default'];
    );
    if ($validator->fails()) {
        return response()->json([
            'message' => 'invalid captcha',
        ]);

    } else {
        // continue
    }