diversen / captcha
v2.0.1
2022-07-25 14:00 UTC
Requires
- php: >=5.3.0
- ext-gd: *
- ext-mbstring: *
- symfony/finder: *
Requires (Dev)
- phpunit/phpunit: ^9.5
README
注意。'Gregwar/Captcha' 已修复,因此无需使用此 REPO。
注意,这只是 https://github.com/Gregwar/Captcha 的一个分支。
制作此包是因为该包没有更新。
如果 Gregwar/Captcha 包被更新,我将删除此包。
安装
使用 composer
composer require diversen/captcha
用法
您可以使用 CaptchaBuilder
创建验证码
<?php use Gregwar\Captcha\CaptchaBuilder; $builder = new CaptchaBuilder; $builder->build();
然后将其保存到文件中
<?php $builder->save('out.jpg');
或直接输出到浏览器
<?php header('Content-type: image/jpeg'); $builder->output();
或在 HTML 页面中直接内联
<img src="<?php echo $builder->inline(); ?>" />
您将能够获取代码并与用户输入进行比较
<?php // Example: storing the phrase in the session to test for the user // input later $_SESSION['phrase'] = $builder->getPhrase();
您可以将短语与用户输入进行比较
if($builder->testPhrase($userInput)) { // instructions if user phrase is good } else { // user phrase is wrong }
API
您可以使用以下函数
- __construct($phrase = null),使用给定的短语构造构建器,如果短语为 null,则生成一个随机的短语
- getPhrase(),允许您获取短语内容
- setDistortion($distortion),启用或禁用扭曲,在
build()
之前调用 - isOCRReadable(),如果可以使用
ocrad
软件读取 OCR,则返回true
,您需要启用 shell_exec,安装 imagemagick 和 ocrad - buildAgainstOCR($width = 150, $height = 40, $font = null),构建代码,直到它无法被
ocrad
读取 - build($width = 150, $height = 40, $font = null),使用给定的 $width、$height 和 $font 构建代码。默认情况下,将使用库中的随机字体
- save($filename, $quality = 80),将验证码保存到 $filename 中的 jpeg 文件,并使用给定的质量
- get($quality = 80),返回 jpeg 数据
- output($quality = 80),直接将 jpeg 代码输出到浏览器
- setBackgroundColor($r, $g, $b),设置背景颜色强制使用(这将禁用许多效果,不建议使用)
- setBackgroundImages(array($imagepath1, $imagePath2)),设置用于验证码背景的自定义背景图片。建议在传递自定义背景图片时禁用图像效果(ignore_all_effects)。从提供的列表中随机选择一张图片,必须传递图片文件的完整路径。
- setInterpolation($interpolate),启用或禁用插值(默认启用),禁用它将更快,但图像将看起来更糟糕
- setIgnoreAllEffects($ignoreAllEffects),禁用验证码图像上的所有效果。在传递自定义背景图片时建议使用。
- testPhrase($phrase),如果给定的短语有效则返回 true
- setMaxBehindLines($lines),设置代码后面的最大行数
- setMaxFrontLines($lines),设置代码前面的最大行数
如果您想更改字符数,可以直接使用额外的参数调用短语构建器
use Gregwar\Captcha\CaptchaBuilder; use Gregwar\Captcha\PhraseBuilder; // Will build phrases of 3 characters $phraseBuilder = new PhraseBuilder(4); // Will build phrases of 5 characters, only digits $phraseBuilder = new PhraseBuilder(5, '0123456789'); // Pass it as first argument of CaptchaBuilder, passing it the phrase // builder $captcha = new CaptchaBuilder(null, $phraseBuilder);
您也可以直接将想要的短语传递给构建器
// Building a Captcha with the "hello" phrase $captcha = new CaptchaBuilder('hello');
完整示例
如果您想查看一个示例,可以查看 demo/form.php
,它使用 demo/session.php
来渲染验证码并在提交后进行检查。
Symfony Bundle
您可以查看以下仓库以享受使用此验证码生成器的 Symfony 2 包:[CaptchaBundle](https://github.com/Gregwar/CaptchaBundle "外部链接")
Yii2 扩展
您可以使用以下扩展与 Yii2 框架集成:[yii2-captcha](https://github.com/juliardi/yii2-captcha "外部链接")
许可证
此库受 MIT 许可证保护,请查看 LICENSE
文件。