Generating a captcha code using PHP

This script generates images (known as “Captcha’s”) which contain security codes used for protecting a form from spam bots. By encoding a ‘password’ inside an image and asking the user to re-enter what they see you can verify the user is a human and not automated software submitting your form.

code for captcha script


/** File: CaptchaSecurityImages.php*/

class CaptchaSecurityImages {

var $font = ‘monofont.ttf’;

function generateCode($characters) {
/* list all possible characters, similar looking characters and vowels have been removed */
$possible = ‘23456789bcdfghjkmnpqrstvwxyz’;
$code = ”;
$i = 0;
while ($i < $characters) {
$code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
return $code;

function CaptchaSecurityImages($width=’120′,$height=’40’,$characters=’6′) {
$code = $this->generateCode($characters);
/* font size will be 75% of the image height */
$font_size = $height * 0.75;
$image = imagecreate($width, $height) or die(‘Cannot initialize new GD image stream’);
/* set the colours */
$background_color = imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 20, 40, 100);
$noise_color = imagecolorallocate($image, 100, 120, 180);
/* generate random dots in background */
for( $i=0; $i<($width*$height)/3; $i++ ) {
imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
/* generate random lines in background */
for( $i=0; $i<($width*$height)/150; $i++ ) {
imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
/* create textbox and add text */
$textbox = imagettfbbox($font_size, 0, $this->font, $code) or die(‘Error in imagettfbbox function’);
$x = ($width – $textbox[4])/2;
$y = ($height – $textbox[5])/2;
imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font , $code) or die(‘Error in imagettftext function’);
/* output captcha image to browser */
header(‘Content-Type: image/jpeg’);
$_SESSION[‘security_code’] = $code;


$width = isset($_GET[‘width’]) && $_GET[‘width’] < 600 ? $_GET[‘width’] : ‘120’;
$height = isset($_GET[‘height’]) && $_GET[‘height’] < 200 ? $_GET[‘height’] : ’40’;
$characters = isset($_GET[‘characters’]) && $_GET[‘characters’] > 2 ? $_GET[‘characters’] : ‘6’;

$captcha = new CaptchaSecurityImages($width,$height,$characters);


save the above script as CaptchaSecurityImages.php
then create one html file for get the value of the captcha

<form method=”post” action=”captchacheck.php”>
<img src=”CaptchaSecurityImages.php” />
Security Code:
<input name=”security_code” />
<input tyep=”submit” value=”signin” name=”signin”/>

You can also specify certain options for the image by passing them as variables to CaptchaSecurityImages.php. The options available are the width and height of the image and the number of characters like the following

<img src=”CaptchaSecurityImages.php?width=100&height=40&characters=5″ alt=”captcha” />

after enter the string the action will take into the captchacheck.php

if(($_SESSION[‘security_code’] == $_POST[‘security_code’]) && (!empty($_SESSION[‘security_code’])) ) {
// Insert you code for processing the form here, e.g emailing the submission, entering it into a database.
} else {
// Insert your code for showing an error message here

After submitting the form we have to clear the session to avoid spam will enter the code by seeing once the image

Extra parameters to change

If you would rather your <img alt=”” /> tag links to a jpg rather than a php file you can use mod_rewrite. By inserting the following in your .htaccess file you can use <img src=”captcha.jpg” alt=”” /> instead.
RewriteEngine on
RewriteRule captcha.jpg /CaptchaSecurityImages.php

You may wish to change the colour of the captcha image, this can be done by editing the background_colour, text_colour and noise_colour variables. The imagecolorallocate() function constucts a colour from the given RGB (red, green and blue) values, each of these is a number between 0-255. Another idea you might want to try is using the mt_rand function to randomize the colour each time a captcha is generated
Posted in Php Tagged with: ,