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

Defining the basic structure of a image generator class in PHP 5 - 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 explained in the beginning of this article, the initial structure of this image generator class will be rather simplistic. It will be composed of only two basic methods (except for the corresponding constructor), called "buildImageStream()" and "displayImage()" respectively.

Obviously, as these names suggest, the first one will be tasked with building dynamically an image stream, while the second one will output it on the browser using a specific graphic format.

Having said that, here's the basic signature of this image generator class. 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 String',$width=400,$height=300,$bgColor='0,0,0',








// create image stream

  private function buildImageStream(){

// implementation for this private method goes here

// display image stream on the browser

  public function displayImage(){

// implementation for this public method goes here



As you can see, the definition of the above "ImageGenerator" class is very easy to follow, even though it's still incomplete. First, you may notice that the constructor takes up a few simple incoming parameters. These include the corresponding input string that will be embedded in the graphic output, and then the dimensions of the image stream being generated, and finally the foreground and background colors.

Additionally, it's clear to see that the constructor also calls the private "buildImageStream()" method from inside its context. This will come in handy for generating the respective dynamic image stream on the fly, based upon the pertinent input arguments. However, for the sake of clarity, the implementation of this method and the "displayImage()" method will be covered in detail in the section to come.

Thus, if you're interested in learning how these brand new methods that belong to the image generator class will be defined, please jump ahead and read the next few lines. I'll be there, waiting for you.

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