Home arrow PHP arrow Page 3 - Developing a Captcha Application with an Image Generator Class with PHP 5

Defining the core structure of a noisy image application - PHP

PHP is a powerful server-side scripting language that is recognized for its flexibility for developing a huge variety of web applications with relative ease. Speaking more specifically, its robust set of graphic processing functions, which has been packaged into the popular GD extension, makes it perfectly suitable for building image generating classes with minor hassles.

TABLE OF CONTENTS:
  1. Developing a Captcha Application with an Image Generator Class with PHP 5
  2. The complete definition of the image generator class
  3. Defining the core structure of a noisy image application
  4. Developing a basic noisy image generation system
By: Alejandro Gervasio
Rating: starstarstarstarstar / 9
October 03, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As I expressed in the previous section, the next step involves improving the structure of the original image generator class, so it can be called repeatedly, without having to create different instances of it.

Here's the modified signature of the image generator class that you learned in the previous article:

// define 'ImageGenerator' class (final version)

class ImageGenerator{

  private $width=400;

  private $height=300;

  private $bgColor='0,0,0';

  private $textColor='255,255,255';

  private $inputString='Default Input String';

  private $bgImage=NULL;

  private $img;

// initialize input arguments

public function __construct($inputString='Default Input String'){

  if(strlen($inputString>255)){

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

}

  $this->inputString=$inputString;

}

  public function setImageWidth($width=400){

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

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

}

  $this->width=$width;

}

  public function setImageHeight($height=300){

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

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

}

}

  public function setImageBgColor($bgColor='0,0,0'){

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

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

}

  $this->bgColor=$bgColor;

}

  public function setImageTextColor($textColor='255,255,255'){

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

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

}

  $this->textColor=$textColor;

}

  public function setBgImage($bgImage='default.gif'){

   if(!file_exists($bgImage)){

    throw new Exception('Invalid background image.');

}

  $this->bgImage=$bgImage;

}

  public function setImageFont($font){

   if(!file_exists($font)){

    throw new Exception('Invalid image font.');

}

  $this->font=$font;

}

// create image stream

  public function displayImageStream(){

$this->img=$this->bgImage!=NULL?imagecreatefromgif($this-
>bgImage):imagecreate($this->width,$this->height);

  $bgColor=explode(',',$this->bgColor);

  $textColor=explode(',',$this->textColor);

// allocate background color on image stream

imagecolorallocate($this->img,$bgColor[0],$bgColor[1],$bgColor
[2]);

// allocate text color on image stream

$textColor=imagecolorallocate($this->img,$textColor[0],$textColor
[1],$textColor[2]);

// load font

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

}

  header("Content-type: image/gif");

// display image

  imagegif($this->img);

// free up memory

  imagedestroy($this->img);

 }

}

As illustrated above, the definition of the image generator class now looks slightly more robust and efficient. It incorporates some important improvements, such as the declaration and implementation of some handy setting methods for assigning different values for its respective properties. In addition to these modifications, the class also allows you to specify a background image for the dynamic graphic stream to be created via its "setBgImage()" method, which is handy for including pre-built graphics into the image stream.

So far, so good, right? At this stage hopefully you realized how simple it was to improve the definition of the previous image generator class to make it slightly more flexible and efficient. So what's the next step?

Well, since my intention here is to demonstrate how to use the prior image generator class to implement a simple noisy image system, below I included the complete signature of a brand new class, called "RandomGenerator," for creating random strings.

The definition of this class is as follows:

class RandomGenerator{

  private $length;

  private $chars="ABCDEFGHIJKLMNPQRSTUVWXYZ123456789";

  public function __construct($length=4){

   if(!is_int($length)||$length<1){

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

}

  $this->length=$length;

}

  public function getRandomValue(){

   $rndstr='';

    $maxvalue=strlen($this->chars)-1;

   for($i=0;$i<$this->length;$i++){

    $rndstr.=substr($this->chars,rand(0,$maxvalue),1);

}

  return $rndstr;

 }

}

As you can see, the logic implemented by the above "RandomGenerator" class is indeed very simple. Basically, all that this class does is return to client code a randomized string, which has a predefined length (four characters is its default size).

Discussing how this class works doesn't make much sense here, so now it's time to demonstrate by the mean of a hands-n example how to couple the two classes defined earlier to implement an efficient noisy image generation system.

Want to learn more on how this will be achieved? Click on the link that appears 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: