Home arrow PHP arrow Page 4 - Developing a Captcha Application with an Image Generator Class with PHP 5

Developing a basic noisy image generation system - PHP

PHP is a powerful server-side scripting language that is recognized for its flexibility for developing a huge variety of web applications with relative ease. Speaking more specifically, its robust set of graphic processing functions, which has been packaged into the popular GD extension, makes it perfectly suitable for building image generating classes with minor hassles.

  1. Developing a Captcha Application with an Image Generator Class with PHP 5
  2. The complete definition of the image generator class
  3. Defining the core structure of a noisy image application
  4. Developing a basic noisy image generation system
By: Alejandro Gervasio
Rating: starstarstarstarstar / 9
October 03, 2007

print this article



As I stated in the section that you just read, the best way to demonstrate how the two previously defined classes can be put to work together to implement a simple noisy image system is by developing an example where you can see how they function in tandem.

So first I will list the complete source code corresponding to the two classes. Here it is:

// define 'ImageGenerator' class

class ImageGenerator{

  private $width=400;

  private $height=300;

  private $bgColor='0,0,0';

  private $textColor='255,255,255';

  private $inputString='Default Input String';

  private $bgImage=NULL;

  private $img;

// initialize input arguments

public function __construct($inputString='Default Input String'){


   throw new Exception('Invalid length for input string.');




  public function setImageWidth($width=400){


    throw new Exception('Invalid width for image stream.');




  public function setImageHeight($height=300){


    throw new Exception('Invalid height for image stream.');



  public function setImageBgColor($bgColor='0,0,0'){


    throw new Exception('Invalid format for background color.');




  public function setImageTextColor($textColor='255,255,255'){


    throw new Exception('Invalid format for text color.');




  public function setBgImage($bgImage='default.gif'){


    throw new Exception('Invalid background image.');




  public function setImageFont($font){


    throw new Exception('Invalid image font.');




// create image stream

  public function displayImageStream(){




// allocate background color on image stream


// allocate text color on image stream


// load font


  throw new Exception('Error creating image text');


  header("Content-type: image/gif");

// display image


// free up memory




class RandomGenerator{

  private $length;

  private $chars="ABCDEFGHIJKLMNPQRSTUVWXYZ123456789";

  public function __construct($length=4){


    throw new Exception('Invalid length for random string.');




  public function getRandomValue(){






  return $rndstr;



Now, take a look at the following code sample, which shows how to embed dynamically several four-digit random strings into a predefined image stream:


// create new instance of 'RandomGenerator' class

  $rnd=new RandomGenerator();

// create new instance of 'ImageGenerator' class

  $imgGen=new ImageGenerator($rnd->getRandomValue());

// display image stream on the browser



  catch(Exception $e){

   echo $e->getMessage();




There you have it! By using the pair of PHP 5 classes that I built previously, it was feasible to develop a fully-functional noisy image application, which can be easily incorporated into any existing web site to prevent (at least partially) automated submissions of its web forms.

Final thoughts

Unfortunately, this series has come to and end. Hopefully the whole learning experience has been positive, since you learned how to utilize the functionality provided by some of the most important functions packaged with the GD extension to build an image generator class. As you have seen, such a class can be really handy for creating, among other useful things, a simple and efficient noisy image system.

See you in the next PHP development tutorial!

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: