Creating simple captcha images with PHP

Written by pelister. Posted in PHP

PHP is not only a html generation script, but also an excellent image generation script. You can create, manipulate, merge images and do various complex operations as you do in an image editing software, by merely writing a piece of code in PHP.  PHP comes with Graphics Draw ( GD ) library which is what does the magic with images. PHP GD Library

CAPTCHA ( Completely Automated Public Turing test to tell Computers and Humans Apart )

Every day captcha images are becoming harder and harder, sometimes we have to spend few minutes to read them. Do not worry we are just going to generate a simple captcha image and this article is to introduce you to PHP GD library. First the code…

<?php
ob_start();

function create_captcha( ) {

   $md5 = md5( rand( 0, 999 ) );
   $pass = substr( $md5, 10, 5 );

   $width = 100;
   $height = 20;

   $image = imagecreate( $width, $height );

  $white = imagecolorallocate( $image, 255, 255, 255 );
  $black = imagecolorallocate( $image, 0, 0, 0 );
  $grey = imagecolorallocate( $image, 204, 204, 204 );

  imagefill( $image, 0, 0, $black );
  imagestring( $image, 3, 30, 3, $pass, $white );
  imagerectangle( $image,0,0,$width-1,$height-1,$grey );

  header("Content-Type: image/jpeg");
  imagejpeg( $image );
  imagedestroy( $image );
}

create_captcha( );
ob_flush( );
exit( );

Now code break down:

We have actually written a function called create_captcha which utilizes the PHP GD functions to generate the image. The first function that we call from GD library is imagecreate with 2 arguments that specifies the width and height of the image. Actually there are two functions to create image,

  1.  imagecreate –  generates an 8 bit – 256 color image.
  2.  imagecreatetruecolor – generates 24bit – 16,777,216 color image.

So for our simple captcha 256 color image is enough.

 $image = imagecreate( $width, $height );

imagecreate function returns an image identifier which will be used in further operations.

  $white = imagecolorallocate( $image, 255, 255, 255 );
  $black = imagecolorallocate( $image, 0, 0, 0 );
  $grey = imagecolorallocate( $image, 204, 204, 204 );

the above function imagecolorallocate generates the colors to be used for text, background and the border

imagefill( $image, 0, 0, $black );
imagestring( $image, 3, 30, 3, $pass, $white );
imagerectangle( $image,0,0,$width-1,$height-1,$grey );

the above three functions are self descriptive – imagefill fills the image with the given color, imagestring embeds the given string on the image and image rectangle draws a rectangle with the given color, all the above functions are supplied with image identifier that we got from imagecreate, x and y position and the color.

header("Content-Type: image/jpeg");
imagejpeg( $image );

finally we use header to output the image directly to the browser. but if you need to store it in a file remove the header and replace the line imagejpeg( $image ) with imagejpeg( $image, ‘yourimage.jpg’ );

Next time we will see more complex captcha generation. Hope you like it.

1 Star2 Stars3 Stars4 Stars5 Stars (6 votes, average: 4.83 out of 5)
Loading...
pelister
3D modeler and Web designer. Founder of Techlister. Love to build sites using WordPress and Joomla. Interested in Astronomy, project member of SETI (Search for Extra Terrestrial Intelligence).

Tags: , , , , ,

Leave a comment

Techlister© - 2012