onnov/captcha

拥有最灵活设置的计算验证码。

v1.0.1 2019-08-25 19:44 UTC

This package is auto-updated.

Last update: 2024-09-17 03:00:55 UTC


README

Build Status Latest Stable Version License

captcha

Captchas examples

验证码演示

具有最灵活设置的验证码。正在改变

  • 字体
  • 大小
  • 颜色
  • 效果

安装

(推荐)使用 Composer 从 Packagist 安装此库:onnov/captcha Composer

从您的项目目录运行以下命令以添加依赖项

composer require onnov/captcha

或者,直接将依赖项添加到您的 composer.json 文件中

"require": {
    "onnov/captcha": "^1.0"
}

项目中的类按照 PSR-4 标准 structured,因此您也可以使用自己的自动加载器或在代码中直接 require 需要的文件。

用法

SYMFONY

如果您使用 SYMFONY 框架

在 services.yaml 文件中

services:
    Onnov\Captcha\Captcha:
        autowire: true

在控制器或服务中

use Onnov\Captcha\Captcha;
    /** @var Captcha */
    protected $captcha;

    public function __construct(Captcha $captcha) {
        $this->captcha = $captcha;
    }

无框架

use Onnov\Captcha\Captcha;

$captcha = new Captcha();

获取默认设置的验证码

/**
 * Returns CaptchaReturn Object
 *
 * @var CaptchaReturn $result
 */
$result = $captcha->getCaptcha();

// Save the value of the captcha in the session
$_SESSION['CaptchaKeyString'] = $result->getKeyString();

// Send the desired headers
foreach ($result->getHeaders() as $k => $v) {
    header($k.': '.$v);
}

// Send captcha to browser
echo $result->getImg();

自定义设置

默认字体是 ActionJackson。您可以使用 monsterShadow 或 baveuse3d 字体。

use Onnov\Captcha\Font\MonsterShadowFont;
use Onnov\Captcha\CaptchaConfig;

$captchaConfig = (new CaptchaConfig())->setFonts([new MonsterShadowFont()]);
$result = $captcha
            ->setConfig($captchaConfig)
            ->getCaptcha();

您可以使用多个字体,它们将以随机顺序使用。

use Onnov\Captcha\Font\ActionJacksonFont;
use Onnov\Captcha\Font\MonsterShadowFont;
use Onnov\Captcha\Font\Baveuse3dFont;
use Onnov\Captcha\CaptchaConfig;

$captchaConfig = (new CaptchaConfig())
            ->setFonts(
                [
                    new ActionJacksonFont(),
                    new MonsterShadowFont(),
                    new Baveuse3dFont(),
                ]
            );
$result = $captcha
            ->setConfig($captchaConfig)
            ->getCaptcha();

您可以使用任何 TTF 字体,指定路径和两个宽度和高度参数(您将需要实验性地选择参数)

use Onnov\Captcha\Font\ModelFont;
use Onnov\Captcha\CaptchaConfig;

$font = (new ModelFont())
            ->setFontPath(__DIR__.'/SignboardCpsNr.ttf')
            ->setCharWidth(25)
            ->setCharHeight(30);
$captchaConfig = (new CaptchaConfig())->setFonts([$font]);
$result = $captcha
            ->setConfig($captchaConfig)
            ->getCaptcha();

扭曲效果

默认情况下,使用两种效果来扭曲验证码中的图像

您可以将两种效果都关闭。

use Onnov\Captcha\CaptchaConfig;

$captchaConfig = (new CaptchaConfig())->setEffects([]);
$result = $captcha
            ->setConfig($captchaConfig)
            ->getCaptcha();

您只能使用一种效果。

use Onnov\Captcha\CaptchaConfig;
use Onnov\Captcha\Effect\WaveDistortionEffect;

$captchaConfig = (new CaptchaConfig())->setEffects([new WaveDistortionEffect()]);
$result = $captcha
            ->setConfig($captchaConfig)
            ->getCaptcha();

您可以更改应用顺序(默认情况下,首先干扰然后波浪扭曲)

use Onnov\Captcha\CaptchaConfig;
use Onnov\Captcha\Effect\WaveDistortionEffect;
use Onnov\Captcha\Effect\InterferenceEffect;

$captchaConfig = (new CaptchaConfig())
            ->setEffects(
                [
                    new WaveDistortionEffect(),
                    new InterferenceEffect(),
                ]
            );
$result = $captcha
            ->setConfig($captchaConfig)
            ->getCaptcha();

每个效果都可以配置。

use Onnov\Captcha\CaptchaConfig;
use Onnov\Captcha\Effect\InterferenceConfig;
use Onnov\Captcha\Effect\InterferenceEffect;
use Onnov\Captcha\Effect\WaveDistortionConfig;
use Onnov\Captcha\Effect\WaveDistortionEffect;

$waveDistortionConfig = (new WaveDistortionConfig())
            ->setAmplitudeStart(300)
            ->setAmplitudeEnd(500)
            ->setAmplitudeDivider(120);

$interferenceConfig = (new InterferenceConfig())
            ->setInterferenceMin(25)
            ->setInterferenceMax(35)
            ->setInterferenceSymbols(':#~');
        
$captchaConfig = (new CaptchaConfig())
            ->setEffects(
                [
                    new WaveDistortionEffect($waveDistortionConfig),
                    new InterferenceEffect($interferenceConfig),
                ]
            );

$result = $captcha
            ->setConfig($captchaConfig)
            ->getCaptcha();

基本验证码设置

  • setWidth(120) // 图像宽度 px
  • setHeight(50) // 图像高度 px
  • setForegroundColor([0,0,0]) // 前景色数组 [R, G, B]
  • setBackgroundColor([255,255,255]) // 背景颜色数组 [R, G, B]
  • setAllowedSymbols('23456789') // 用于绘制验证码的符号
  • 验证码中的字符数
    • setLengthMin(4) // 最小字符串长度
    • setLengthMax(5) // 最大字符串长度
  • 符号之间的大小间隙
    • setGapMin(0) // 符号之间的最小间隙
    • setGapMax(10) // 符号之间的最大间隙
  • setFluctuationAmplitude(5) // 符号的垂直波动幅度
  • setPadding(5) // 从图像边缘到文字的缩进
  • setCharRotate(5) // 字符旋转角度(以度为单位)
  • setJpegQuality(70) // 验证码图像的 JPEG 质量
  • 脚本尝试按顺序首先以 GIF 格式返回图像,然后是 jpg,最后是 PNG
    • setMaybeReturnGif(false) // 防止返回 GIF 图像
    • setMaybeReturnJpg(false) // 防止返回 JPG 图像
    • setMaybeReturnPng(false) // 防止返回 PNG 图像

配置示例

use Onnov\Captcha\CaptchaConfig;

$captchaConfig = (new CaptchaConfig())
            ->setWidth(120)
            ->setHeight(70)
            ->setPadding(5)
            ->setBackgroundColor([255,255,220])
            ->setForegroundColor([0,100,100]);

$result = $captcha
            ->setConfig($captchaConfig)
            ->getCaptcha();