랭킹으로 돌아가기

Gregwar/Captcha

PHP

PHP Captcha library

captchaphpsecuritybotsanti-botanti-spam
스타 성장
스타
1.8k
포크
309
주간 성장
이슈
1
5001k1.5k
2013년 1월2017년 7월2022년 1월2026년 7월
아티팩트Packagistcomposer require gregwar/captcha
README

Captcha

Captchas examples

Installation

With composer : composer require gregwar/captcha

Usage

You can create a captcha with the CaptchaBuilder :

<?php

use Gregwar\Captcha\CaptchaBuilder;

$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

// 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
}

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 true if the OCR can be read using the ocrad software, 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 in the $filename, with the given quality
  • get($quality = 80), returns the image data
  • output($quality = 80), directly outputs the image
  • inline($quality = 80), returns the jpeg code directly in the HTML page
  • setBackgroundColor($r, $g, $b), sets the background color to force it (this will disable many effects and is not recommended)
  • setBackgroundAlpha($alpha), sets the background transparency 0 is fully opaque, 127 is fully transparent
  • setBackgroundImages([$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
  • setIgnoreAllEffects($ignoreAllEffects), disable all effects on the captcha image. Recommended to use when passing custom background images for the captcha.
  • 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
  • setImageType($type), sets the image output type to "jpeg", "png" or "gif"
  • setScatterEffect($bool), enable or disable scatter effect (fuzzyness)

If you want to change the number of characters, 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

Filament Extension

You can use the following extension for integrating with Laravel Filament:

https://github.com/marcogermani87/filament-captcha

License

This library is under MIT license, have a look to the LICENSE file

관련 저장소
chaitin/SafeLine

SafeLine is a self-hosted WAF(Web Application Firewall) / reverse proxy to protect your web apps from attacks and exploits.

GoGo ModulesGNU General Public License v3.0firewallhttp-flood
ly.safepoint.cloud/fUxS0GW
21.9k1.4k
sml2h3/ddddocr

带带弟弟 通用验证码识别OCR pypi版

PythonPyPIMIT Licenseocrddddocr
ddddocr.com
14.5k2.3k
ultrafunkamsterdam/undetected-chromedriver

Custom Selenium Chromedriver | Zero-Config | Passes ALL bot mitigation systems (like Distil / Imperva/ Datadadome / CloudFlare IUAM)

PythonPyPIGNU General Public License v3.0chromedriverselenium
github.com/UltrafunkAmsterdam/undetected-chromedriver
12.8k1.3k
NopeCHALLC/nopecha-extension

Automated CAPTCHA solver for your browser. Works with Selenium, Puppeteer, Playwright, and more.

MIT Licenseaws-waf-captchacaptcha
nopecha.com
10.5k192
dessant/buster

Captcha solver extension for humans, available for Chrome, Edge and Firefox

JavaScriptnpmGNU General Public License v3.0browser-extensionchrome-extension
chrome.google.com/webstore/detail/buster-captcha-solver-for/mpbjkejclgfgadiemmefgebjfooflfhl
9.2k686
lining0806/PythonSpiderNotes

Python入门网络爬虫之精华版

PythonPyPIpythonzhihu
7.5k2.2k
tiagozip/cap

Free, open-source and self-hosted CAPTCHA alternative to reCAPTCHA. Privacy-first and powered by proof-of-work and instrumentation challenges.

JavaScriptnpmOthercaptchajavascript
trycap.dev
7.3k508
zhaipro/easy12306

使用机器学习算法完成对12306验证码的自动识别

PythonPyPIArtistic License 2.0deep-learningmathematics
2.9k725
ypwhs/captcha_break

验证码识别

Jupyter NotebookMIT Licensecaptcha-breakingkeras-tutorials
2.8k668
altcha-org/altcha

GDPR, WCAG 2.2 AA, and EAA compliant, self-hosted CAPTCHA alternative with PoW mechanism.

TypeScriptnpmMIT Licenseantispamcaptcha
altcha.org
2.6k126
mewebstudio/captcha

Captcha for Laravel 5+

PHPPackagistMIT Licenselaravelphp
2.6k465
mCaptcha/mCaptcha

A no-nonsense CAPTCHA system with seamless UX | Backend component

Rustcrates.ioGNU Affero General Public License v3.0mcaptchacaptcha
demo.mcaptcha.org
2.5k94