Custom PHP CAPTCHA generator

Discussion of tools and resources for game developers

Post » Sat Jan 18, 2014 2:45 pm

Hey guys, I wrote my own custom CAPTCHA image generator using PHP & the GD library and wanted to share it with you, in case you need something like this on your own sites or apps. It's a really simple implementation which you can see an example output here:

This PHP script generates a 150x80 PNG image consisting of a few random geometric shapes along with a CAPTCHA text string of 5 characters consisting of the numbers 0-9 and letters a-f. If you needed more characters or different colors, you could easily extend this script. The correct CAPTCHA value is stored in a session variable named "captchaval". The font I'm using is called "aescrawl.ttf" which has a handwriting feel to it. Substitute whatever font you'd like but it will need to be in the same directory as this script.

Implementation: Think of this file as just any other image that you'd insert into your HTML as an img src. You would include this script inside that tag like this:
[code]<img src="captchar.php" alt="CAPTCHA image" />[/code]

Here's the CAPTCHA PHP code itself:

*    CAPTCHA generator v0.01, 2013
*    Written by ionux ([email protected])
*    This is free software released into the
*    public domain. No warranty is implied or provided.


$string = substr(md5(mt_rand(0,55)),5,5);
$spook = ':::::::::::';
$shape1_x = rand(0,145);
$shape1_y = rand(0,75);
$shape2_x = rand(0,145);
$shape2_y = rand(0,75);

$_SESSION['captchaval'] = $string;

header('Content-Type: image/png');

$im = @imagecreate(150, 80) or die('Cannot Initialize new GD image stream');

$background_color = imagecolorallocate($im, 0, 0, 0);
$text_color = imagecolorallocate($im, 233, 233, 233);
$strange_color = imagecolorallocate($im, 101, 101, 101);
$shape1_color = imagecolorallocate($im, 191, 77, 224);
$shape2_color = imagecolorallocate($im, 200, 111, 43);

imagestring($im, 5, rand(3,100), rand(10,75), $spook, $strange_color);
imagestring($im, 5, rand(3,100), rand(10,75), $spook, $strange_color);
imageellipse($im, $shape1_x, $shape1_y, rand(46,100), rand(30,100), $shape1_color);
imageellipse($im, $shape2_x, $shape2_y, rand(46,100), rand(30,100), $shape2_color);
imagettftext($im, 34, rand(0,15), 10, 75, $text_color, 'aescrawl.ttf', $string);


If this help anyone else, great! I appreciate all the help you guys have given me with C2 so far. :Dionux2014-01-18 14:48:44
Posts: 24
Reputation: 647

Post » Mon Jan 20, 2014 12:37 am

its possible to create captcha via events in C2?
Posts: 1,896
Reputation: 39,569

Post » Mon Jan 20, 2014 5:04 pm


EDIT: nvm I realised I scripted forwarding my session values to cookies for some debugging.


ofc, you could easily add some effect to distort a text.
The idea is that image recognition should fail to recognise the captcha characters. As long as potential abusers cant find the value in the source code or directly by some recognition tool, then your set.

lennaert2014-01-20 17:16:03
Who dares wins
Posts: 1,880
Reputation: 19,628

Return to Tools and Resources

Who is online

Users browsing this forum: No registered users and 0 guests