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

The complete definition of the image generator class - 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



Before building the noisy image application that I mentioned in the beginning of this article, I will list the complete signature corresponding to the image generator class that I created in the preceding article of the series. Doing so should give you a much better idea of how this class can be easily "plugged" to a string randomizer system to create a noisy image generation mechanism.

All right, having said that, here's the full definition of the aforementioned image generator class, as it was originally built in the previous article of the series. Have a look at it, please:

// define 'ImageGenerator' class

class ImageGenerator{

  private $width;

  private $height;

  private $bgColor;

  private $textColor;

  private $inputString;

  private $img;

// initialize input arguments

public function __construct($inputString='Default Input


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



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



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



throw new Exception('Invalid format for background or text









// create image stream

  private function buildImageStream(){


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


// allocate background color on image stream


// allocate text color on image stream




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



// display image stream on the browser

  public function displayImage(){

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

// display image


// free up memory




As you can see, the logic that drives the above image generator class is very simple to follow. The class takes up a few input parameters, including the foreground and background colors of the image stream being generated, and the text string that will be embedded into the stream.

These basic tasks are carried out by the two primary methods of the class, called "buildImageStream()" and "displayImage()" respectively.

However, if you study in detail the signature of the previous class, you'll probably see that it seems pretty inflexible, since it actually builds the corresponding image stream whenever the class is instantiated. This prohibits the creation of other, different streams during the execution of given script.

Therefore, in the section to come I'll introduce some important modifications to the original structure of the image generator class to solve this issue. I'll also build a simple text randomizer, which will be extremely useful for creating an efficient noisy image application.

To see how all of these interesting tasks will be carried out, please jump forward and read the next few lines. They're just one click away.

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