PHP code example of isszz / webman-scaptcha

1. Go to this page and download the library: Download isszz/webman-scaptcha library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

isszz / webman-scaptcha example snippets


return [
    'type' => null, // 单独的配置项
    'cache' => true, // 是否启用字形缓存
    'api' => false, // 是否是API模式
    // 设置为1时不管验证对错, 都会删除存储凭证,若验证失败则需要刷新一次验证码
    // 设置为0时, 直到验证输入正确时, 才删除存储凭证,也就是允许试错
    // 非API模式或者未配置token机制,则需要设置2才能让验证码为一次性
    'disposable' => 0,
    'width' => 150, // 宽度
    'height' => 50, // 高度
    'noise' => 5, // 干扰线条的数量
    'inverse' => false, // 反转颜色
    'color' => true, // 文字是否随机色
    'background' => '', // 验证码背景色
    'size' => 4, // 验证码字数
    'ignoreChars' => '', // 验证码字符中排除
    'fontSize' => 52, // 字体大小
    'char' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', // 预设随机字符
    'math' => '', // 运算模式:1=加法,2=减法,3=乘法,4=除法,或非1=4,则随机四种
    'mathMin' => 1, // 用于计算的最小值
    'mathMax' => 9, // 用于计算的最大值
    'font' => '', // 用于验证码的字体文件路径, 建议字体文件不超过3MB

    // API模式,使用token机制,使用这里的配置后API会携带一个token,在验证时需要携带token和输入的code进行验证
    /*'token' => [
        // 也可以自定义\app\common\libs\MyStore::class,
        // 自带可选:redis,session;建议使用redis
        'store' => 'cache', 
        'expire' => 300,
        'redis' => [
            'host' => '127.0.0.1',
            'port' => 6379,
            'password' => '',
            'select' => 1,
            'timeout' => 0,
        ],
    ],*/

    // 单独的配置, 会覆盖上面的配置
    'test' => [
        'noise' => 3,
        'color' => false,
        'char' => '0123456789',
        // 'token' => null,
    ],
];

{
    'code': 0,
    'msg': 'success',
    'token': '8SOy2KSfcSVIP7qTogFCLvLZb9tj5eTB', // API模式,使用token机制否则返回null
    'svg': 'data:image/svg+xml,.../%3E%3C/svg%3E',
}


[$token, $image] = scaptcha_api([
    'noise' => 3, // 3条干扰线
    'color' => false, // 灰色模式
    'char' => '0123456789', // 数字验证码
]);

// 或指定单独的配置,第二个参数用于选择生成的格式false=svg,true=base64
[$token, $image] = scaptcha_api('test', true);

use support\Request;
use isszz\captcha\Captcha;

class CaptchaController
{
    /**
     * 获取验证码, 用于api
    */
    public function index(Request $request)
    {
        return json([
            'code' => 0,
            'data' => scaptcha_api(),
            'msg' => 'success',
        ]);
    }

    /**
     * 直接显示svg验证码
    */
    public function svg()
    {   
        $content = scaptcha();
        
        return response($content, 200, [
            'Content-Type' => 'image/svg+xml',
            'Content-Length' => strlen($content),
        ]);
    }

    /**
     * 验证输入验证码是否正确|输出json
    */
    public function check($code, string|null $token = null)
    {
        $json = [
            'code' => 0,
            'msg' => 'success',
            'data' => null,
        ];

        if(!scaptcha_check($code, $token)) {
            $json['code'] = 1;
            $json['msg'] = 'error';
        }

        return json($json);
    }
}


if(!scaptcha_check($code, $token)) {
    // 验证失败
}
html
<!-- 配置项参考URL参数配置 -->

    $captchaSrc = scaptcha_src([
        'l' => 5,
    ]);