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

Recalling the initial definition of the image generator class - 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

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
 

blog comments powered by Disqus
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 4 - Follow our Sitemap

Dev Shed Tutorial Topics: