PHP
  Home arrow PHP arrow Page 4 - 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 
Moblin 
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

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

    Adding Validation to an Image Generator Class with PHP 5 - The improved signature of the ImageGenerator class


    (Page 4 of 4 )

    In consonance with the concepts that I deployed in the section that you just read, the only thing that remains to be covered here is demonstrating a basic implementation of the image generator class, including the improvements that I introduced earlier to the corresponding constructor.

    Below I listed the entire definition of the class in question, along with a basic practical example, which simply shows how to output a sample text string on the browser in PNG format.

    The corresponding code sample is as follows:

    // define 'ImageGenerator' class (improved version)

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

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

      $font=imageloadfont($this->font);

    if(!imagestring($this->img,$this->font,$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 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();

    }

    After studying the improved definition for the above image generator class, you'll certainly agree with me that outputting different strings in a predetermined graphic format is actually a no-brainer process that can be performed with minor problems.

    As usual, you're completely free to modify all the code samples shown here and add your own improvements. It's really fun, trust me!

    Final thoughts

    As demonstrated in this second article of the series, the image generator class is quite useful for displaying text strings in a predefined graphic format. However, maybe you're wondering at this very moment: what's the point in doing this? Well, as I explained before, the class on its own may seen rather useless, but when combined with other PHP applications, it can be utilized to embed plain texts into different image streams to generate dynamic banners, or the so-called noisy images.

    However, the creation of noisy images with the previous image generator class will be covered in detail in the last part of the series, so you don't have any excuses 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.

       · 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

    - 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 6 hosted by Hostway