Home arrow PHP arrow Page 3 - Adding Validation to an Image Generator Class with PHP 5

Validating incoming parameters - PHP

Among the plethora of applications that can be developed with PHP 5, building dynamic image streams is one of the easiest to tackle. It requires only an intermediate background in the functions that come packaged with the powerful GD extension. This series of articles will teach you how to use this graphic library to build a highly expansible image generator class that can be used to create noisy images (also known as captchas).

TABLE OF CONTENTS:
  1. Adding Validation to an Image Generator Class with PHP 5
  2. Recalling the initial definition of the image generator class
  3. Validating incoming parameters
  4. The improved signature of the ImageGenerator class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
October 02, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: