Create numbered captcha in php

September 17, 201883
Share Tweet Pin it

Creating CAPTCHA in php is very simple. I followed lot of tutorials through out the web. I found 100s of code lines in php but every body is following the hard structure which is very difficult in some cases to adopt the code line. Here I am giving a very simple code line where people can implementthis code and return the captcha easyly in jquery as well.

To create the captcha write the following code into an php file like captcha.php

        // initialise image with dimensions of 160 x 45 pixels
        $image = @imagecreatetruecolor(160, 45) or die('Cannot Initialize new GD image stream');

        // set background and allocate drawing colours
        $background = imagecolorallocate($image, 0x66, 0xCC, 0xFF);
        imagefill($image, 0, 0, $background);
        $linecolor = imagecolorallocate($image, 0x33, 0x99, 0xCC);
        $textcolor1 = imagecolorallocate($image, 0x00, 0x00, 0x00);
        $textcolor2 = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);

        // draw random lines on canvas
        for ($i = 0; $i < 8; ++$i) {
            imagesetthickness($image, rand(1, 3));
            imageline($image, rand(0, 160), 0, rand(0, 160), 45, $linecolor);


        // using a mixture of TTF fonts
        $fonts = [];
        $fonts[] = 'dejavu/DejaVuSerif-Bold.ttf';
        $fonts[] = 'dejavu/DejaVuSans-Bold.ttf';
        $fonts[] = 'dejavu/DejaVuSansMono-Bold.ttf';

        // add random digits to canvas using random black/white colour
        $digit = '';
        for ($x = 10; $x <= 130; $x += 30) {
            $textcolor = (rand() % 2) ? $textcolor1 : $textcolor2;
            $digit .= ($num = rand(0, 9));
            imagettftext($image, 20, rand(-30, 30), $x, rand(20, 42), $textcolor, $fonts[array_rand($fonts)], $num);

        // record digits in session variable
        $_SESSION['captchatext'] = $digit;

        // display image and clean up
        header('Content-type: image/png');

The above code will return you the captcha like following image

There is a small trick to call it in your page. Please follow the code below.

            url: ajaxurl_to_captcha_php_code_file,
            beforeSend: function (xhr) {
                xhr.overrideMimeType('text/plain; charset=x-user-defined');
            type: 'POST',
            data: data,
            dataType: "text",
            success: function(result, textStatus, jqXHR){
                if(result.length < 1){
                    alert("The thumbnail doesn't exist");
                    $("#thumbnail").attr("src", "data:image/png;base64,");
                var binary = "";
                var responseText = jqXHR.responseText;
                var responseTextLen = responseText.length;

                for ( i = 0; i < responseTextLen; i++ ) {
                    binary += String.fromCharCode(responseText.charCodeAt(i) & 255)
                $("#recaptcha-src").attr("src", "data:image/png;base64,"+btoa(binary));

the above jquery you have call on button click, which work like ajax call. Load the above function in page load as well to get load the captcha in pageload.

Hope this helps you developers. 

Thanks for be here.