PHP
  Home arrow PHP arrow Page 3 - Developing a Captcha Application with ...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
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? 
PHP

Developing a Captcha Application with an Image Generator Class with PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


       · In this final installment of the series, the image generator class built in previous...
       · In the ImageGenerator constructor, the following...
       · Thank you commenting on my PHP article and for pointing out that small bug on the...
     

       

    PHP ARTICLES

    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview
    - Handling Attachments in MIME Email with PHP
    - Completing the Project Management Application
    - Sending MIME Email with PHP
    - Handling Files for a Project Management Appl...
    - Viewing and Editing Tasks for a Project Mana...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway