PHP
  Home arrow PHP arrow Page 3 - Building an Image Generator Class with...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
Moblin 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
IBM developerWorks
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Building an Image Generator Class with PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 8
    2007-10-01

    Table of Contents:
  • Building an Image Generator Class with PHP 5
  • Defining the basic structure of a image generator class in PHP 5
  • Implementing the buildImageStream() and displayImage() methods
  • Testing the ImageGenerator class

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!

    Building an Image Generator Class with PHP 5 - Implementing the buildImageStream() and displayImage() methods


    (Page 3 of 4 )

    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
    String',$width=400,$height=300,$bgColor='0,0,0',
    $textColor='255,255,255'){

      $this->inputString=$inputString;

       $this->width=$width;

       $this->height=$height;

       $this->bgColor=explode(',',$bgColor);

       $this->textColor=explode(',',$textColor);

       $this->buildImageStream();

    }

    // create image stream

     private function buildImageStream(){

      if(!$this->img=imagecreate($this->width,$this->height)){

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

    }

    // allocate background color on image stream

    imagecolorallocate($this->img,$this->bgColor[0],$this->bgColor
    [1],$this->bgColor[2]);

    // allocate text color on image stream

    $textColor=imagecolorallocate($this->img,$this->textColor
    [0],$this->textColor[1],$this->textColor[2]);

    if(!imagestring($this->img,5,$this->width/2-strlen($this-
    >inputString)*5,$this->height/2-5,$this->inputString,$textColor)){

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

     }

    }

    // display image stream on the browser

     public function displayImage(){

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

    // display image

      imagepng($this->img);

    // free up memory

      imagedestroy($this->img);

     }

    }

    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


       · As you’ll probably know, PHP 5 really shines when it comes to building...
     

       

    PHP ARTICLES

    - Handling Attachments in MIME Email with PHP
    - Completing the Project Management Application
    - Sending MIME Email with PHP
    - Handling Files for a Project Management Appl...
    - Viewing and Editing Tasks for a Project Mana...
    - More on Private Methods with PHP 5 Member Vi...
    - Adding Tasks to a Project Management Applica...
    - Utilizing Private Methods with PHP 5 and Mem...
    - Making Changes in a Project Management Appli...
    - Defining Public and Protected Methods with M...
    - HTML for a Project Management Application
    - Using Subclasses and Accessors with Member V...
    - Implementing Internet Protocols with PHP
    - Project Management: The Application
    - Working with Private Properties to Protect P...




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway