PHP
  Home arrow PHP arrow Page 4 - 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 
Moblin 
JMSL Numerical Library 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 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


    Building an Image Generator Class with PHP 5 - Testing the ImageGenerator class


    (Page 4 of 4 )

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

     }

    }

    /* display default input string

      try{

    // create new instance of 'ImageGenerator' class

      $imgGen=new ImageGenerator();

    // display image stream on the browser

      $imgGen->displayImage();

    }

      catch(Exception $e){

       echo $e->getMessage();

      exit();

    }

    // display sample input string

      try{

    // create new instance of 'ImageGenerator' class

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

    // display image stream on the browser

      $imgGen->displayImage();

    }

      catch(Exception $e){

       echo $e->getMessage();

      exit();

    }

    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!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

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

       

    PHP ARTICLES

    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview
    - Handling Attachments in MIME Email with PHP
    - Completing the Project Management Application





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