Home arrow PHP arrow Page 4 - Adding Validation to an Image Generator Class with PHP 5

The improved signature of the ImageGenerator class - PHP

Among the plethora of applications that can be developed with PHP 5, building dynamic image streams is one of the easiest to tackle. It requires only an intermediate background in the functions that come packaged with the powerful GD extension. This series of articles will teach you how to use this graphic library to build a highly expansible image generator class that can be used to create noisy images (also known as captchas).

  1. Adding Validation to an Image Generator Class with PHP 5
  2. Recalling the initial definition of the image generator class
  3. Validating incoming parameters
  4. The improved signature of the ImageGenerator class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
October 02, 2007

print this article



In consonance with the concepts that I deployed in the section that you just read, the only thing that remains to be covered here is demonstrating a basic implementation of the image generator class, including the improvements that I introduced earlier to the corresponding constructor.

Below I listed the entire definition of the class in question, along with a basic practical example, which simply shows how to output a sample text string on the browser in PNG format.

The corresponding code sample is as follows:

// define 'ImageGenerator' class (improved version)

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 color.');









// 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 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();



After studying the improved definition for the above image generator class, you'll certainly agree with me that outputting different strings in a predetermined graphic format is actually a no-brainer process that can be performed with minor problems.

As usual, you're completely free to modify all the code samples shown here and add your own improvements. It's really fun, trust me!

Final thoughts

As demonstrated in this second article of the series, the image generator class is quite useful for displaying text strings in a predefined graphic format. However, maybe you're wondering at this very moment: what's the point in doing this? Well, as I explained before, the class on its own may seen rather useless, but when combined with other PHP applications, it can be utilized to embed plain texts into different image streams to generate dynamic banners, or the so-called noisy images.

However, the creation of noisy images with the previous image generator class will be covered in detail in the last part of the series, so you don't have any excuses 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: