eprofos/captcha
最新稳定版本:1.0.0
Composer 安装命令:
composer require eprofos/captcha
包简介
A professional PHP 8+ library for generating visually sophisticated captchas with advanced security features and accessibility options
README 文档
README
A professional PHP 8+ library for generating visually sophisticated captchas.
Features
- Generation of visually sophisticated captchas
- Complete customization (dimensions, fonts, complexity levels, etc.)
- Distortion and wave effects
- Background noise and pattern options
- Color schemes and gradient options
- Character set and length configuration
- Session integration capabilities
- Implementation with modern PHP 8 features (attributes, typed properties, union types)
Installation
composer require eprofos/captcha
Requirements
- PHP 8.0 or higher
- GD extension
- mbstring extension
Basic Usage
<?php use Eprofos\Captcha\Captcha; use Eprofos\Captcha\Config\CaptchaConfig; // Create a captcha configuration $config = new CaptchaConfig(); $config->setWidth(200) ->setHeight(80) ->setLength(6) ->setComplexity(CaptchaConfig::COMPLEXITY_MEDIUM); // Create a captcha instance $captcha = new Captcha($config); // Generate and display the captcha $captcha->generate(); $captcha->output(); // Store the code in the session $_SESSION['captcha_code'] = $captcha->getCode();
Captcha Verification
<?php // Verify the submitted captcha code $userInput = $_POST['captcha'] ?? ''; $storedCode = $_SESSION['captcha_code'] ?? ''; if (strtolower($userInput) === strtolower($storedCode)) { echo "Captcha validated successfully!"; } else { echo "Incorrect captcha code!"; }
Advanced Customization
<?php use Eprofos\Captcha\Captcha; use Eprofos\Captcha\Config\CaptchaConfig; use Eprofos\Captcha\Config\ColorScheme; // Create a custom configuration $config = new CaptchaConfig(); $config->setWidth(300) ->setHeight(100) ->setLength(8) ->setComplexity(CaptchaConfig::COMPLEXITY_HIGH) ->setFont('montserrat.ttf') ->setBackgroundNoise(true) ->setWaveDistortion(true) ->setCharacterSet('ABCDEFGHJKLMNPQRSTUVWXYZ23456789') ->setColorScheme(new ColorScheme('#3498db', '#2c3e50')); // Create and generate the captcha $captcha = new Captcha($config); $captcha->generate(); // Save the image $captcha->save('/path/to/captcha.png'); // Or send the image as an HTTP response $captcha->output();
Using with Built-in Session Storage
<?php use Eprofos\Captcha\Captcha; use Eprofos\Captcha\Config\CaptchaConfig; use Eprofos\Captcha\Storage\SessionStorage; // Start the session session_start(); // Create a configuration $config = new CaptchaConfig(); // Create a captcha with session storage $storage = new SessionStorage('my_captcha_key'); $captcha = new Captcha($config, $storage); // Generate and display the captcha $captcha->generate(); $captcha->output(); // Verify the code (in another script) if ($captcha->verify($_POST['captcha'] ?? '')) { echo "Captcha validated successfully!"; } else { echo "Incorrect captcha code!"; }
License
MIT
统计信息
- 总下载量: 1
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-03-08