PHP
  Home arrow PHP arrow Page 3 - Developing a Captcha Application with an Image Generator Class with PHP 5
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Developing a Captcha Application with an Image Generator Class with PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 8
    2007-10-03


    Table of Contents:
  • Developing a Captcha Application with an Image Generator Class with PHP 5
  • The complete definition of the image generator class
  • Defining the core structure of a noisy image application
  • Developing a basic noisy image generation system

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Developing a Captcha Application with an Image Generator Class with PHP 5 - Defining the core structure of a noisy image application
    ( Page 3 of 4 )

    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
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek