An advanced PHP Captcha Generator
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.
Supports 4 different captcha text sources
Input
Custom Generator Functions
You can define a custom function of your own that generates the text that should be in the captcha.
Random Text Generator
Generates a random string of Letters and Numbers.
Dictionary File
Can use any file containing space separated words as a dictionary file to choose words from.
Captcha Image Compression
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
$captcha = new omranjamal\RealCaptcha\RealCaptcha();
$captcha->generate()->output("jpg");
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.
$realCaptcha = new omranjamal\RealCaptcha\RealCaptcha();
$captcha = $realCaptcha->generate();
The generate()
method returns an Output object it has two methods and a buch of Properties.
file( path, format, quality)
: Writes Captcha to a fileoutput( format, quality)
: Sets appropriate image format headers and sends the Image data to browsertext
: Contains a string words that are present in the captcha image, seperated by spacesarray
: Contains an Array of the words present in the captcha image.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);
To save captcha to file, you can use the file( file_path, format, quality)
method.
$captcha = $realCaptcha->generate();
$captcha->file("file.jpg" ,"jpg", 90);
omranjamal\RealCaptcha\RealCaptcha::INPUT
: Requires you to pass the captcha text while calling generate()
methodomranjamal\RealCaptcha\RealCaptcha::RANDOM
: Generates a random string of letters and numbers.omranjamal\RealCaptcha\RealCaptcha::DICTIONARY
: Selects words at random from the dictionary file.omranjamal\RealCaptcha\RealCaptcha::uFUNCTION
: Requires you to set a custom made function that returns the captcha text.omranjamal\RealCaptcha\RealCaptcha::GREY_VARIABLE
: Randomly chooses a shade from white to light ash.array( int, int, int)
: A numerical array containing RGB values.$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");
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");
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.
$captcha = new omranjamal\RealCaptcha\RealCaptcha(array(
"source" => realCaptcha::uFUNCTION
));
$captcha->textFunction(function(){
return array("EXAMPLE","FUNCTION");
});
$captcha->generate()->output("jpg");
RealCaptcha is released under the MIT License. Visit the project Homepage for more information.