Custom PHP CAPTCHA generator

0 favourites
  • 3 posts
From the Asset Store
Paypal PHP Supports multiple platforms, Web/html5 Android, IOS. (Including storing transaction data)
  • 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:

    <img src="captchar.php" alt="CAPTCHA image" />

    Here's the CAPTCHA PHP code itself:

     *    CAPTCHA generator v0.01, 2013
     *    Written by ionux (
     *    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. :D

  • its possible to create captcha via events in C2?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • ionux

    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.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)