PHP
  Home arrow PHP arrow Page 3 - 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 
Actuate Whitepapers 
VeriSign Whitepapers 
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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Adding Validation to an Image Generator Class with PHP 5 - Validating incoming parameters


    (Page 3 of 4 )

    As I stated in the prior section, the original signature of the "ImageGenerator" class lacked an important feature, the ability to perform an adequate validation on all of its incoming parameters. Since this capacity should be properly incorporated into the class, I will modify the initial implementation of the constructor to provide it with the capacity to check the validity of the different parameters taken by the class.

    Given that, here's how the "ImageGenerator" class now looks after modifying the definition of its constructor method:

    // define 'ImageGenerator' class (improved version - performs a
    basic validation on its incoming parameters)

    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'){

      if(strlen($inputString>255)){

       throw new Exception('Invalid length for input string.');

    }

      if(!is_int($width)&&$width>800){

       throw new Exception('Invalid width for image stream.');

    }

      if(!is_int($width)&&$height>600){

       throw new Exception('Invalid height for image stream.');

    }

    if(!preg_match("/^d{1,3},d{1,3},d{1,3}$/",$bgColor)||!preg_match
    ("/^d{1,3},d{1,3},d{1,3}$/",$textColor)){

    throw new Exception('Invalid format for background or text color.');

    }

      $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(){

    // implementation of this method goes here

    }

     

    // display image stream on the browser

      public function displayImage(){

    // implementation of this method goes here

     }

    }

    As you can see, the constructor of the above "ImageGenerator" class incorporates a few additional lines of code for validating individually each of its incoming parameters, before assigning them as class properties. Of course, this checking process could be improved, particularly if you need to perform a strict validation on the respective input arguments, but I think that the current checking process is more than enough.

    So far, so good. At this stage I already showed you how to improve the implementation of the constructor that belongs to the "ImageGenerator" class, with the purpose of making it slightly more efficient and robust. So, what's the next step?

    Well, since the signature of the previous image generator class now looks more compact and solid, I believe that it's time to reassemble its different parts, and develop a concrete example where its functionality can be tested, at least at a very basic level.

    Considering this scenario, in the upcoming section of this tutorial I'll be showing you the complete (and improved) source code of this image generator class so you can see how it can be used to display several text strings in PNG format.

    As I said before, this will be done in the next few lines, so click on the link shown 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

    - 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...
    - Protecting PHP 5 Class Data with Member Visi...
    - Setting Up a Web-based Image Hosting Service
    - Comparing Files and Databases with PHP Bench...
    - Setting Up a Web-Based Image Gallery





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