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

Developing a basic noisy image generation system - 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 stated in the section that you just read, the best way to demonstrate how the two previously defined classes can be put to work together to implement a simple noisy image system is by developing an example where you can see how they function in tandem.

So first I will list the complete source code corresponding to the two classes. Here it is:

// define 'ImageGenerator' class

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

 }

}

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;

 }

}

Now, take a look at the following code sample, which shows how to embed dynamically several four-digit random strings into a predefined image stream:

  try{

// create new instance of 'RandomGenerator' class

  $rnd=new RandomGenerator();

// create new instance of 'ImageGenerator' class

  $imgGen=new ImageGenerator($rnd->getRandomValue());

// display image stream on the browser

  $imgGen->displayImageStream();

}

  catch(Exception $e){

   echo $e->getMessage();

  exit();

}

 

There you have it! By using the pair of PHP 5 classes that I built previously, it was feasible to develop a fully-functional noisy image application, which can be easily incorporated into any existing web site to prevent (at least partially) automated submissions of its web forms.

Final thoughts

Unfortunately, this series has come to and end. Hopefully the whole learning experience has been positive, since you learned how to utilize the functionality provided by some of the most important functions packaged with the GD extension to build an image generator class. As you have seen, such a class can be really handy for creating, among other useful things, a simple and efficient noisy image system.

See you in the next PHP development tutorial!



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