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

Defining the core structure of a noisy image application - 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 expressed in the previous section, the next step involves improving the structure of the original image generator class, so it can be called repeatedly, without having to create different instances of it.

Here's the modified signature of the image generator class that you learned in the previous article:

// define 'ImageGenerator' class (final version)

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




As illustrated above, the definition of the image generator class now looks slightly more robust and efficient. It incorporates some important improvements, such as the declaration and implementation of some handy setting methods for assigning different values for its respective properties. In addition to these modifications, the class also allows you to specify a background image for the dynamic graphic stream to be created via its "setBgImage()" method, which is handy for including pre-built graphics into the image stream.

So far, so good, right? At this stage hopefully you realized how simple it was to improve the definition of the previous image generator class to make it slightly more flexible and efficient. So what's the next step?

Well, since my intention here is to demonstrate how to use the prior image generator class to implement a simple noisy image system, below I included the complete signature of a brand new class, called "RandomGenerator," for creating random strings.

The definition of this class is as follows:

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;



As you can see, the logic implemented by the above "RandomGenerator" class is indeed very simple. Basically, all that this class does is return to client code a randomized string, which has a predefined length (four characters is its default size).

Discussing how this class works doesn't make much sense here, so now it's time to demonstrate by the mean of a hands-n example how to couple the two classes defined earlier to implement an efficient noisy image generation system.

Want to learn more on how this will be achieved? Click on the link that appears below and keep reading.

>>> 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: