belomaxorka/captcha
最新稳定版本:v1.2.4
Composer 安装命令:
composer require belomaxorka/captcha
包简介
Captcha generator
关键字:
README 文档
README
This repository is now archived and no longer maintained.
This fork was originally created to provide bug fixes and PHP compatibility updates that were not being merged into the upstream repository. However, as of version v1.3.0, the original repository has incorporated all necessary fixes and updates.
🔄 Migration Notice
Please migrate to the original repository: gregwar/captcha
The upstream repository is now actively maintained and includes all the improvements that were previously only available in this fork.
Why was this fork created?
This fork served as a temporary solution when:
- Critical bug fixes were not being merged upstream
- PHP version compatibility issues needed to be addressed
- The original repository appeared to be inactive
Current Status
✅ Original repository is now active and maintained
✅ All fixes have been merged upstream
❌ This fork is no longer necessary
For new projects: Use the original repository
For existing projects: Please plan migration to the upstream repository
Thank you to everyone who used this fork during the transition period!
Captcha (Based on gregwar/Captcha)
Requirements
- PHP Versions:
5.3and above (up to8.4). - PHP Extensions:
gd,mbstring,fileinfo(enabled by default).
Installation
It's available for Composer:
composer require belomaxorka/captcha
How to migrate
Just replace:
{
...
"require": {
"gregwar/captcha": "1.*"
}
}
To:
{
...
"require": {
"belomaxorka/captcha": "1.*"
}
}
Usage
You can create a captcha with the CaptchaBuilder:
<?php use Gregwar\Captcha\CaptchaBuilder; use Gregwar\Captcha\PhraseBuilder; $builder = new CaptchaBuilder; $builder->build();
You can then save it to a file:
<?php $builder->save('out.jpg');
Or output it directly:
<?php header('Content-type: image/jpeg'); $builder->output();
Or inline it directly in the HTML page:
<img src="<?php echo $builder->inline(); ?>" />
You'll be able to get the code and compare it with a user input:
<?php // Create session to store $_SESSION['phrase'] session_start(); // Example: storing the phrase in the session to test for the user // input later $_SESSION['phrase'] = $builder->getPhrase();
You can compare the phrase with user input:
if ($builder->testPhrase($userInput)) { // instructions if user phrase is good } else { // user phrase is wrong }
Or with using PhraseBuilder:
if (PhraseBuilder::comparePhrases($_SESSION['phrase'], $userInput)) { // instructions if user phrase is good } else { // user phrase is wrong }
API
You can use these functions:
- __construct($phrase = null), constructs the builder with the given phrase, if the phrase is null, a random one will be generated
- getPhrase(), allow you to get the phrase contents
- setDistortion($distortion), enable or disable the distortion, call it before
build() - isOCRReadable(), returns
trueif the OCR can be read using theocradsoftware, you'll need to have shell_exec enabled, imagemagick and ocrad installed - buildAgainstOCR($width = 150, $height = 40, $font = null), builds a code until it is not readable by
ocrad - build($width = 150, $height = 40, $font = null), builds a code with the given $width, $height and $font. By default, a random font will be used from the library
- save($filename, $quality = 80), saves the captcha into a jpeg in the $filename, with the given quality
- get($quality = 80), returns the jpeg data
- output($quality = 80), directly outputs the jpeg code to a browser
- setBackgroundColor($r, $g, $b), sets the background color to force it (this will disable many effects and is not recommended)
- setBackgroundImages(array($imagepath1, $imagePath2)), Sets custom background images to be used as captcha background. It is recommended to disable image effects when passing custom images for background (ignore_all_effects). A random image is selected from the list passed, the full paths to the image files must be passed.
- setInterpolation($interpolate), enable or disable the interpolation (enabled by default), disabling it will be quicker but the images will look uglier
- setScatterEffect($scatterEffect), enable or disable the scatter effect (enabled by default). Note: Requires PHP 7.4 or higher.
- setIgnoreAllEffects($ignoreAllEffects), disable all effects on the captcha image. Recommended to use when passing custom background images for the captcha.
- setIgnorePostEffects($ignorePostEffects), disable post effects on the captcha image (edge, negate, contrast, colorize). Note: setIgnoreAllEffects also disables post effects.
- testPhrase($phrase), returns true if the given phrase is good
- setMaxBehindLines($lines), sets the maximum number of lines behind the code
- setMaxFrontLines($lines), sets the maximum number of lines on the front of the code
If you want to change the number of character, you can call the phrase builder directly using extra parameters:
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);
You can also pass directly the wanted phrase to the builder:
// Building a Captcha with the "hello" phrase $captcha = new CaptchaBuilder('hello');
Complete example
If you want to see an example you can have a look at the demo/form.php, which uses demo/session.php to
render a captcha and check it after the submission
Symfony Bundle
You can have a look at the following repository to enjoy the Symfony 2 bundle packaging this captcha generator: https://github.com/Gregwar/CaptchaBundle
Yii2 Extension
You can use the following extension for integrating with Yii2 Framework: https://github.com/juliardi/yii2-captcha
Examples
Have a look at the examples inside the demo folder, which should give you an impression of what's possible - or simply whip up a PHP development server:
# Change directory cd demo # Launch server php -S localhost:3000 # All examples are now available in your browser, eg 'http://localhost:3000/form.php'
License
This library is under MIT license, have a look to the LICENSE file
belomaxorka/captcha 适用场景与选型建议
belomaxorka/captcha 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.7k 次下载、GitHub Stars 达 7, 最近一次更新时间为 2025 年 03 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「security」 「spam」 「captcha」 「anti-spam」 「obfuscation」 「anti-bot」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 belomaxorka/captcha 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 belomaxorka/captcha 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 belomaxorka/captcha 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Simple captcha generator
reCAPTCHA library
Client library for reCAPTCHA with proxy support, a free service that protects websites from spam and abuse.
Diese Contao 4 Erweiterung stellt Google reCAPTCHA V2 in Form eines neuen Formularfeldes im Formulargenerator bereit. This extension provides Google reCAPTCHA V2 in the form of a new form field in the form generator of Contao Open Source CMS.
Provide a way to secure accesses to all routes of an symfony application.
Two Captcha
统计信息
- 总下载量: 2.7k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-03-08