PHP
  Home arrow PHP arrow Page 2 - Adding Validation to an Image Generator Class with PHP 5
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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: starstarstarstarstar / 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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT