eusonlito / captcha
一个简单的生成和验证验证码的包
1.1.1
2022-07-23 13:24 UTC
Requires
- php: >=5.3
Requires (Dev)
README
一个简单且易于实现的验证码包。
使用 Composer 安装
{ "require": { "eusonlito/captcha": "1.0.*" } }
演示
使用方法
模板
<?php use Eusonlito\Captcha\Captcha; ?> <div class="form-group"> <img src="<?= Captcha::source($LETTERS_COUNT, $WIDTH, $HEIGHT); ?>" class="img-responsive" /> <input type="text" name="<?= Captcha::sessionName(); ?>" value="" class="form-control" /> ... or ... <?= Captcha::img($LETTERS_COUNT, $WIDTH, $HEIGHT); ?> <input type="text" name="<?= Captcha::sessionName(); ?>" value="" class="form-control" /> ... or ... <?= Captcha::img($LETTERS_COUNT, $WIDTH, $HEIGHT, array('class' => 'img-responsive')); ?> <input type="text" name="<?= Captcha::sessionName(); ?>" value="" class="form-control" /> ... or ... <?= Captcha::img($LETTERS_COUNT, $WIDTH, $HEIGHT); ?> <?= Captcha::input(array('class' => 'form-control')); ?> ... or ... <?= Captcha::img(array($LETTERS_MIN, $LETTERS_MAX), $WIDTH, $HEIGHT); ?> <?= Captcha::input(array('class' => 'form-control')); ?> </div>
如果您在一个没有会话的环境中使用,您必须在任何 HTML 输出之前添加 Captcha::sessionStart()
(控制器)。
验证
<?php use Eusonlito\Captcha\Captcha; function validate() { if (!Captcha::check()) { throw new Exception('Captcha text is not correct'); } }
就这么多!
Laravel 使用方法
<?php # config/app.php return [ ... 'aliases' => [ ... 'Captcha' => 'Eusonlito\Captcha\Captcha', ... ] ];
现在您将在控制器和视图中有一个可用的 Captcha
类。
打印选项
<?php use Eusonlito\Captcha\Captcha; # Simple usage with fixed word length Captcha::source($LETTERS_COUNT, $WIDTH, $HEIGHT); # Print base64 source image code # Define min and max word length Captcha::source(array($LETTERS_MIN, $LETTERS_MAX), $WIDTH, $HEIGHT); # Print base64 source image code # Same using img tag Captcha::img($LETTERS_COUNT, $WIDTH, $HEIGHT); # Print img tag Captcha::img(array($LETTERS_MIN, $LETTERS_MAX), $WIDTH, $HEIGHT); # Print img tag # Img tag with parameters Captcha::img($LETTERS_COUNT, $WIDTH, $HEIGHT, array('class' => 'img-responsive')); # Print img tag with class attribute # Simple input tag print Captcha::input(); # Print input tag # Input tag with parameters Captcha::input(array('class' => 'form-control')); # Print input tag with class attribute
自定义配置
所有自定义设置将在调用 img
、source
或 check
方法之前定义。
<?php use Eusonlito\Captcha\Captcha; # Define a unique font to use (only .ttf) Captcha::setFont(__DIR__.'/../fonts/couture-bold.ttf'); # string or array # Add fonts to repository (only .ttf) Captcha::addFont(array( __DIR__.'/../fonts/couture-bold.ttf', __DIR__.'/../fonts/brush-lettering-one.ttf' )); # Set custom rgb background. Default is 255, 255, 255 Captcha::setBackground([120, 120, 120]); # Set custom hex background. Captcha::setBackground('#FFF000'); # Set transparent background. Captcha::setBackground('transparent'); # Set custom rgb font color. Default is 115, 115, 115 Captcha::setColor([50, 50, 50]); # Set custom hex color. Captcha::setColor('#000FFF'); # Set custom padding to captcha image (approximate). Default is 0.4 Captcha::setPadding(20); // Fixed value in pixels Captcha::setPadding(0.4); // Percent value # Set image noise. Default is without noise Captcha::setNoise($POINTS, $LINES); // Fixed points and lines noise Captcha::setNoise(array($POINTS_MIN, $POINTS_MAX), array($LINES_MIN, $LINES_MAX)); // Variable points and lines noise Captcha::setNoise(null, array($LINES_MIN, $LINES_MAX)); // Avoid points noise Captcha::setNoise(array($POINTS_MIN, $POINTS_MAX), null); // Avoid lines noise # Set custom available letters. Default are 'ABCDEFGHJKLMNPRSTUVWXYZ' Captcha::setLetters('ABCDE3456'); # Set custom session name captcha storage (captcha string is stored crypted). Default is 'captcha-string' Captcha::sessionName('my-captcha'); # Enable session before use on non session environments Captcha::sessionStart();
享受吧!