Convert Figma logo to code with AI

Gregwar logoCaptcha

PHP Captcha library

1,704
288
1,704
48

Top Related Projects

No CAPTCHA reCAPTCHA For Laravel.

Quick Overview

Gregwar/Captcha is a PHP library for generating CAPTCHAs (Completely Automated Public Turing test to tell Computers and Humans Apart). It provides a simple and customizable way to create image-based CAPTCHAs for use in web applications to prevent automated bot submissions.

Pros

  • Easy to integrate and use in PHP projects
  • Highly customizable with various options for fonts, colors, and distortions
  • Supports multiple output formats (PNG, JPEG, GIF)
  • Includes built-in session handling for CAPTCHA validation

Cons

  • Limited to image-based CAPTCHAs, which may not be accessible for visually impaired users
  • Requires GD library support in PHP, which may not be available on all hosting environments
  • May be vulnerable to advanced OCR (Optical Character Recognition) techniques
  • Regular updates may be needed to stay ahead of CAPTCHA-solving algorithms

Code Examples

  1. Basic CAPTCHA generation:
use Gregwar\Captcha\CaptchaBuilder;

$builder = new CaptchaBuilder;
$builder->build();
$builder->output();
  1. Customizing CAPTCHA appearance:
$builder = new CaptchaBuilder;
$builder->setBackgroundColor(220, 230, 255);
$builder->setMaxBehindLines(5);
$builder->setMaxFrontLines(5);
$builder->build(150, 40);
$builder->output();
  1. Validating user input:
$userInput = $_POST['captcha'];
if($builder->testPhrase($userInput)) {
    echo "CAPTCHA is valid!";
} else {
    echo "CAPTCHA is invalid.";
}

Getting Started

  1. Install the library using Composer:
composer require gregwar/captcha
  1. Use the library in your PHP code:
<?php
require_once 'vendor/autoload.php';

use Gregwar\Captcha\CaptchaBuilder;

$builder = new CaptchaBuilder;
$builder->build();

// Display the CAPTCHA image
header('Content-type: image/jpeg');
$builder->output();

// Store the phrase in the session for later verification
$_SESSION['phrase'] = $builder->getPhrase();
  1. In your form processing script, validate the user input:
if($builder->testPhrase($_POST['captcha'])) {
    // CAPTCHA is valid, process the form
} else {
    // CAPTCHA is invalid, show an error message
}

Competitor Comparisons

No CAPTCHA reCAPTCHA For Laravel.

Pros of no-captcha

  • Implements Google's reCAPTCHA v2 and v3, offering more modern and user-friendly CAPTCHA options
  • Easier integration with Laravel framework, including built-in validation rules
  • Supports multiple languages out of the box

Cons of no-captcha

  • Limited to Google's reCAPTCHA, while Captcha offers more customization options
  • Requires an internet connection and relies on Google's services, potentially impacting privacy
  • Less control over CAPTCHA generation and appearance compared to Captcha

Code Comparison

no-captcha implementation:

{!! NoCaptcha::renderJs() !!}
{!! NoCaptcha::display() !!}

Captcha implementation:

$captcha = new Gregwar\Captcha\CaptchaBuilder;
$captcha->build();
echo $captcha->inline();

Both repositories provide CAPTCHA functionality for PHP applications, but they differ in their approach and features. no-captcha focuses on integrating Google's reCAPTCHA service, making it easier to implement modern CAPTCHA solutions, especially in Laravel projects. Captcha, on the other hand, offers more customization and control over CAPTCHA generation, without relying on external services. The choice between the two depends on specific project requirements, such as the desired level of customization, integration needs, and privacy considerations.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Captcha

Captchas examples paypal

Installation

With composer :

{
    ...
    "require": {
        "gregwar/captcha": "1.*"
    }
}

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

License

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