Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/Gregwar/Captcha
PHP Captcha library
https://github.com/Gregwar/Captcha
anti-bot anti-spam bots captcha php security
Last synced: 14 days ago
JSON representation
PHP Captcha library
- Host: GitHub
- URL: https://github.com/Gregwar/Captcha
- Owner: Gregwar
- License: mit
- Created: 2012-12-27T12:01:19.000Z (almost 12 years ago)
- Default Branch: master
- Last Pushed: 2024-05-14T15:58:26.000Z (6 months ago)
- Last Synced: 2024-10-15T04:41:12.883Z (24 days ago)
- Topics: anti-bot, anti-spam, bots, captcha, php, security
- Language: PHP
- Homepage:
- Size: 643 KB
- Stars: 1,714
- Watchers: 62
- Forks: 288
- Open Issues: 48
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-captcha - CGregwar/Captcha - Biblioteka PHP CAPTCHA. (Biblioteki)
- awesome-php-cn - Gregwar/Captcha - 图形验证码 (目录 / 图像 Imagery)
- php-awesome - Captcha - 验证码 (类库 / 图像处理)
README
Captcha
=======![Captchas examples](http://gregwar.com/captchas.png)
[![paypal](https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=YUXRLWHQSWS6L)Installation
============With composer :
``` json
{
...
"require": {
"gregwar/captcha": "1.*"
}
}
```Usage
=====You can create a captcha with the `CaptchaBuilder` :
```php
build();
```You can then save it to a file :
```php
save('out.jpg');
```Or output it directly :
```php
output();
```Or inline it directly in the HTML page:
```php
```You'll be able to get the code and compare it with a user input :
```php
getPhrase();
```You can compare the phrase with user input:
```php
if($builder->testPhrase($userInput)) {
// instructions if user phrase is good
}
else {
// user phrase is wrong
}
```API
===You can use theses 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 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
* **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 codeIf you want to change the number of character, you can call the phrase builder directly using
extra parameters:```php
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:
```php
// 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 submissionSymfony Bundle
================You can have a look at the following repository to enjoy the Symfony 2 bundle packaging this captcha generator :
https://github.com/Gregwar/CaptchaBundleYii2 Extension
===============You can use the following extension for integrating with Yii2 Framework :
https://github.com/juliardi/yii2-captchaLicense
=======This library is under MIT license, have a look to the `LICENSE` file