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

The improved signature of the ImageGenerator 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

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!



 
 
>>> 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: