PHP
  Home arrow PHP arrow Page 2 - Adding Validation to an Image Generato...
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

Adding Validation to an Image Generator Class with PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2007-10-02

    Table of Contents:
  • Adding Validation to an Image Generator Class with PHP 5
  • Recalling the initial definition of the image generator class
  • Validating incoming parameters
  • The improved signature of 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


    Adding Validation to an Image Generator Class with PHP 5 - Recalling the initial definition of the image generator class


    (Page 2 of 4 )

    Before I introduce the modifications that I mentioned in the beginning of this article, I'd like to list the complete source code for the image generator class as it was initially defined in the first tutorial of the series. I believe this will give you a much better idea of how these improvements will affect the functionality of the class in question.

    Thus, having clarified this important point, here's the full signature of the "ImageGenerator" class as it was built in the previous installment of this series:

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

     }

    }

    Okay, now that you remember how the signature of the above image generator class originally looked, please examine the following pair of code samples. They demonstrate a simple utilization of this class. Here they are:

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

    }

    Having demonstrated how to use the "ImageGenerator" class to display on the browser some primitive input strings in PNG format, it's time to analyze in detail its pitfalls. First, as you can see, the structure of the class is pretty flexible, but definitely doesn't allow you to perform a decent validation on its incoming parameters.

    Naturally, this crucial issue should be fixed quickly to make the class slightly more efficient. Thus, in the course of the next section, I'll be introducing some important modifications to the constructor of the class, so it can perform an adequate validation on all of its input arguments.

    To see how these improvements will be added to the image generator class, please click on the link that appears below and keep reading.

    More PHP Articles
    More By Alejandro Gervasio


       · Over the course of this second part of the series, the basic definition of the image...
       · Hello there,I copied the code with minimun changes, and tried to call on the...
       · Thank you for the comments about my PHP article and I’m glad to know you’re getting...
       · Thanks for your quick reply... I really thought that it would not matter if you...
       · Thank you again for your post. So, it seems the problem comes up from your web...
       · Instead of:if(strlen($inputString>255)){Should...
       · Thank you for pointing out to me that little bug in the class code.Regards.
     

       

    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