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

Implementing the buildImageStream() and displayImage() methods - 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 you might have guessed from the concepts that I deployed in the previous section, completing the definition of this image generator PHP class is a process reduced to implementing the respective "buildImageStream()" and "displayImage()" methods that were declared earlier within the class structure.

Below I have again included the signature of the image generator class in question, including the implementation of the aforementioned class methods.

Having explained that, here's the complete version of this class, after implementing its corresponding "buildImageStream()" and "displayImage()" methods:

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




Certainly, if you take some time to study the signature of the above image generator class, then you'll agree with me that its business logic is quite simple to grasp. As you can see, the implementations for the methods I mentioned a few lines ago are also simple. In the first case, the "buildImageStream()" method performs some basic tasks, like creating the pertinent image stream, then allocating the appropriate foreground and background colors, and finally embedding into the stream the input text that was initially passed to the constructor. Quite easy to follow, right?

Now, concerning the implementation of the second "displayImage()" method, its functionality is limited to outputting to the browser the respective image stream built previously, in this case using the PNG format via the "imagepng()" function that comes bundled with the GD extension.

Finally the stream is removed from the web server's memory by calling the "imagedestroy()" function. This procedure should be quite familiar to you if you work with the GD library on a regular basis.

So far, so good, right? At this point, I have completed the development of the previous "ImageGenerator" class, which behaves like a simple wrapper for the most common functions included with the GD extension.

However, you probably want to see how this class can be used in the context of a practical example. Therefore, in the course of the last section of this tutorial I'll show you some friendly code samples to give you a better idea of how to put this image generator class to work for you.

To see how this brand new hands-on example will be developed, please click on the link 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: