PHP code example of wpjscc / captcha

1. Go to this page and download the library: Download wpjscc/captcha 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/ */

    

wpjscc / captcha example snippets


use Captcha\Captcha;

$captcha = Captcha::createWithLocalDriver([
    'storageDirectory' => '/tmp/captcha-storage',
]);

// 生成验证码
$response = $captcha->create();

// 输出图片
header('Content-Type: image/jpeg');
echo $response->imageBinary;

// 校验(会话模式)
$valid = $captcha->check($_POST['captcha']);

use Captcha\Captcha;
use Captcha\Drivers\LocalFileDriver;
use Captcha\Drivers\LocalStorage;

$fileDriver = new LocalFileDriver();
$storage = new LocalStorage($fileDriver, '/tmp/captcha-storage');

$captcha = new Captcha(
    fileDriver: $fileDriver,
    config: [
        'fontsDirectory' => '/path/to/fonts',
        'bgsDirectory' => '/path/to/backgrounds',
    ],
    storage: $storage,
);

$captcha = Captcha::createWithLocalDriver();

$response = $captcha->create();

// 返回 HTML img 标签
echo $captcha->img();

// 返回图片 URL(需自行实现对应路由)
$url = $captcha->src(baseUrl: 'https://example.com/captcha');

// 提交后校验
if ($captcha->check($input)) {
    // 验证通过
}

// 进程启动时实例化一次
$captcha = Captcha::createWithLocalDriver([
    'storageDirectory' => '/tmp/captcha-storage',
]);

// 每个请求传入用户唯一标识(如 PHP Session ID、JWT jti 等)
$sessionId = $request->sessionId;

$response = $captcha->create(sessionId: $sessionId);
$valid = $captcha->check($input, sessionId: $sessionId);

// 生成
$api = $captcha->create('math', api: true);
// [
//     'sensitive' => false,
//     'key'       => '...',
//     'img'       => 'data:image/jpeg;base64,...',
// ]

// 校验(对应 Laravel 的 captcha_api 规则)
$valid = $captcha->checkApi(
    value: $_POST['captcha'],
    key: $_POST['key'],
    config: 'math',
);

$captcha->create('default');  // 默认
$captcha->create('flat');     // 扁平风格
$captcha->create('mini');     // 迷你尺寸
$captcha->create('inverse');  // 反色风格
$captcha->create('math');     // 算术验证码

$captcha = Captcha::createWithLocalDriver([
    'disable'          => false,          // 设为 true 时 check/checkApi 直接返回 true
    'fontsDirectory'   => '/path/fonts',
    'bgsDirectory'     => '/path/backgrounds',
    'storageDirectory' => '/tmp/captcha',
    'encryptionKey'    => 'your-secret-key',
    'default' => [
        'length'  => 6,
        'width'   => 345,
        'height'  => 65,
        'expire'  => 60,    // 过期时间(秒)
        'math'    => false,
        'encrypt' => false,
    ],
]);

use Captcha\Contracts\FileDriverInterface;

class OssFileDriver implements FileDriverInterface
{
    public function files(string $directory): array { /* 返回目录下所有文件绝对路径 */ }
    public function exists(string $path): bool { /* ... */ }
    public function read(string $path): string { /* ... */ }
    public function write(string $path, string $contents): bool { /* ... */ }
    public function delete(string $path): bool { /* ... */ }
    public function makeDirectory(string $path, int $mode = 0755): bool { /* ... */ }
}

use Captcha\Contracts\StorageInterface;

class RedisStorage implements StorageInterface
{
    public function putSession(string $sessionId, array $data): void { /* ... */ }
    public function getSession(string $sessionId): ?array { /* ... */ }
    public function removeSession(string $sessionId): void { /* ... */ }
    public function hasSession(string $sessionId): bool { /* ... */ }
    public function putCache(string $key, string $value, int $ttl): void { /* ... */ }
    public function pullCache(string $key): ?string { /* ... */ }
}

captcha/
├── assets/
│   ├── fonts/              # 字体文件
│   └── backgrounds/        # 背景图片
├── config/captcha.php      # 默认配置
├── src/
│   ├── Captcha.php         # 核心类
│   ├── CaptchaResponse.php # 响应对象
│   ├── ImageGenerator.php  # GD 图片生成
│   ├── Contracts/
│   │   ├── FileDriverInterface.php
│   │   └── StorageInterface.php
│   └── Drivers/
│       ├── LocalFileDriver.php
│       └── LocalStorage.php
└── tests/