RealCaptcha

An advanced PHP Captcha Generator

View the Project on GitHub omranjamal/RealCaptcha

RealCaptcha

A PHP Library that uses the PHP-GD Extension to generate Captchas with only PHP.
RealCaptcha is released under the MIT License. Visit the project Homepage for more information.

Note: This code is still in its Beta stage so it maybe prone to many bugs...
Note: The Fonts included here are not created or owned by the author of the code, but none the less, their liscenses are open-source, commercial-friendly and permit redistribution, Check them out yourself.

Example Image

Features

Installation

The simplest method is using Composer. Just require it in your composer.json file as such:

{
    "require":{
        "omranjamal/real-captcha":"dev-master"
    }
}

To incluude it in your project, include the composer autoloader and just create new objects of the RealCaptcha class under the omranjamal\RealCaptcha namespace as such:

include 'vendor/autoload.php';
$captcha = new omranjamal\RealCaptcha\RealCaptcha();

Alternatively you could manually clone or download this reppository and directly include the Class file in and create an object of omranjamal\RealCaptcha\RealCaptcha

Basic Usage

Basic

$captcha = new omranjamal\RealCaptcha\RealCaptcha();
$captcha->generate()->output("jpg");

Configuring

All three of these code output similar Captchas to the browser in JPG Format.

While Initializing

$realCaptcha = new omranjamal\RealCaptcha\RealCaptcha(array(
    "height" = 200,
    "width" = 500,
    "number_of_words" = 2
));

$captcha = $realCaptcha->generate();
$captcha->output("jpg");

The Settings you provide at Initialization will be used every time the generate() method is called.

Using the set() method

$realCaptcha = new omranjamal\RealCaptcha\RealCaptcha();

$realCaptcha->set(array(
    "height" = 200,
    "width" = 500,
    "number_of_words" = 2
));

$captcha = $realCaptcha->generate();
$captcha->output("jpg");

The settings you define with the set() method will overwrite the settings you provided at initialization and will be used every time the generate() method is called.

While Generating

$realCaptcha = new omranjamal\RealCaptcha\RealCaptcha();

$captcha = $realCaptcha->generate(array(
    "height" = 200,
    "width" = 500,
    "number_of_words" = 2
));
$captcha->output("jpg");

The generate() method will give high priority to the settings passed to it as an argument. These setting will be forgotten as soon as the captcha is finished generating.

Dealing with output

$realCaptcha = new omranjamal\RealCaptcha\RealCaptcha();
$captcha = $realCaptcha->generate();

The generate() method returns an Output object it has two methods and a buch of Properties.

The Methods

The Properties

Example

This example generates a captcha and stores the captcha text in a session variable so that it can be matched later and output the captcha image to browser and also daves the image to a file named example.jpg

session_start();
$realCaptcha = new omranjamal\RealCaptcha\RealCaptcha();

$captcha = $realCaptcha->generate();
$_SESSION["captcha_text"] = $captcha->text;
$captcha->output("jpg",100);
$captcha->file("example.jpg","jpg",100);

Saving Captcha to file

To save captcha to file, you can use the file( file_path, format, quality) method.

$captcha = $realCaptcha->generate();
$captcha->file("file.jpg" ,"jpg", 90);

Full List of settings

Advanced Usage

Setting Background and Text color

Colored Example

$captcha = new omranjamal\RealCaptcha\RealCaptcha(array(
    "background_color" => array(255,0,0), //Bright Red
    "text_color" => array(255,255,255) //White
));

$captcha->generate()->output("jpg");

Using direct Input

Both these codes are correct but work in different ways and the settings persist for different fractions of the run cycle.

Setting at Initialization

$captcha = new omranjamal\RealCaptcha\RealCaptcha(array(
    "source" => realCaptcha::INPUT
));

$captcha->generate("Text")->output("jpg");

Setting at Generator

$captcha = new omranjamal\RealCaptcha\RealCaptcha();
$captcha->generate("Text", array("source" => realCaptcha::INPUT))->output("jpg");

It is Completely ok to pass String or an Array as input into the generator method, thus both the following teo lines of code are valid

$captcha->generate("Text")->output("jpg");
$captcha->generate(array("example","text"))->output("jpg");

Using Custom Text Generator Function

Unlike Direct Input , Custom Functions cannot be set and declared at the generator method, custom function usage has to be declared in the initialization settings or through the set() method and the function has to be define through the textFunction() method as the first argument. All prior to calling the generate() method in which you intend to use the Custom function.

Example Function

Example

$captcha = new omranjamal\RealCaptcha\RealCaptcha(array(
    "source" => realCaptcha::uFUNCTION
));

$captcha->textFunction(function(){
    return array("EXAMPLE","FUNCTION");
});

$captcha->generate()->output("jpg");

Fonts

Liscense

RealCaptcha is released under the MIT License. Visit the project Homepage for more information.