Home arrow PHP arrow Page 4 - Building an Image Generator Class with PHP 5

Testing the ImageGenerator class - PHP

In this article, you will learn how to build an image generator class with PHP 5, using the functions bundled with the GD library. Such a class has a wide variety of uses, but for the purposes of this three-part series, we will focus on creating a function for displaying noisy images (also known as captchas) on the browser.

  1. Building an Image Generator Class with PHP 5
  2. Defining the basic structure of a image generator class in PHP 5
  3. Implementing the buildImageStream() and displayImage() methods
  4. Testing the ImageGenerator class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 13
October 01, 2007

print this article



As I stated during the section that you just read, one of the best ways to demonstrate the functionality provided by the previously defined "ImageGenerator" class consists of developing a practical example, where you can see very clearly how it works.

Please take a look at the following code sample, which illustrates how to use the class in question, first for displaying on the browser a default input string, and finally for showing another primitive text.

Given that, the corresponding code sample looks like this, including first the complete definition of the "ImageGenerator" class that you learned previously:

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








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




/* display default input string


// create new instance of 'ImageGenerator' class

  $imgGen=new ImageGenerator();

// display image stream on the browser



  catch(Exception $e){

   echo $e->getMessage();



// display sample input string


// create new instance of 'ImageGenerator' class

  $imgGen=new ImageGenerator('This is a sample string');

// display image stream on the browser



  catch(Exception $e){

   echo $e->getMessage();



As shown above, the image generator class in its current incarnation is quite useful for displaying input strings in the format of image streams. The procedure is reduced to creating an instance of the mentioned class with the input text to be outputted, and then calling its respective "displayImage()" method.

In addition, below I included two screen shots, which demonstrate the outputs generated by the previous code samples:

Of course, the functionality of the class analyzed in an isolated context is rather limited. However, it can be quite useful when utilized in conjunction with other applications, for instance, to build noise images. Now, things are getting slightly more interesting, right?

For the moment, I suggest that you test the source code of this image generator class by using several input texts, to see how it works in each particular case.

Final thoughts

That's all for now. Hopefully, after reading this article, you'll have a much better idea of how to build a PHP 5-based class that displays an input string in a predefined graphic format.

Nonetheless, the class in its current version has some important limitations, since it doesn't perform an appropriate validation on all of its input arguments. Therefore, considering this relevant issue, in the next part of the series I'll fix this issue, thus improving the overall structure of the class.

Now that you know what the next article will be about, you won't want to miss it!

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