• support@answerspoint.com

Converting Text to Image using php script for captcha code concept


PHP script to write email address into a image

I was after a php script that dynamically converts text to image and finally i decided to code on my own. It also does convert small length text to image like names, emails etc..

This script automatically calculates the width of text into pixels inserts the PNG or gif image

  • I am dealing with non-English language and I want to display that text as image so that it will be supported even in the platforms that do not support this language. The problem is I am able to display text properly in text format but when it comes to image. It does not display image at all.
	header ("Content-type: image/png");
	$string = $_GET['txt'];                                            
	$font   = 4;
	$width  = ImageFontWidth($font) * strlen($string);
	$height = ImageFontHeight($font);
	$im = @imagecreate ($width,$height);
	$background_color = imagecolorallocate ($im, 255, 255, 255); //white background
	$text_color = imagecolorallocate ($im, 0, 0,0);//black text
	imagestring ($im, $font, 0, 0,  $string, $text_color);
	imagepng ($im);


  • PHP

  • asked 7 years ago
  • Sandy Hook



The following code needs to be saved as a stand-alone PHP file (we call it captcha.php). This file creates a PNG image containing a series of five digits. It also stores these digits in a session variable so that other scripts can know what the correct code is and validate that it's been entered correctly.


  // Adapted for The Art of Web: www.the-art-of-web.com

  // Please acknowledge use of this code by including this header.

  // initialise image with dimensions of 120 x 30 pixels

  $image = @imagecreatetruecolor(120, 30) or die("Cannot Initialize new GD image stream");

  // set background to white and allocate drawing colours

  $background = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);

  imagefill($image, 0, 0, $background);

  $linecolor = imagecolorallocate($image, 0xCC, 0xCC, 0xCC);

  $textcolor = imagecolorallocate($image, 0x33, 0x33, 0x33);

  // draw random lines on canvas

  for($i=0; $i < 6; $i++) {

    imagesetthickness($image, rand(1,3));

    imageline($image, 0, rand(0,30), 120, rand(0,30), $linecolor);



  // add random digits to canvas

  $digit = '';

  for($x = 15; $x <= 95; $x += 20) {

    $digit .= ($num = rand(0, 9));

    imagechar($image, rand(3, 5), $x, rand(2, 14), $num, $textcolor);


  // record digits in session variable

  $_SESSION['digit'] = $digit;

  // display image and clean up

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




The output of this script appears as follows (reload to see it change):


This image is meant to be difficult for 'robots' to read, but simple for humans (the Turing test). You can make it more difficult for them by addition of colours or textures, or by using different fonts and a bit of rotation.

We've simplified the script presented above as much as possible so that you can easily customise it for your site and add more complexity as necessary. Further down the page you can find examples that use colours, rotation and different fonts, but the basic concept is the same.

  • answered 7 years ago
  • Gul Hafiz

Your Answer

    Facebook Share        
  • asked 7 years ago
  • viewed 3200 times
  • active 7 years ago

Best Rated Questions