PHP
  Home arrow PHP arrow Page 2 - 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 
Actuate Whitepapers 
VeriSign Whitepapers 
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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Developing a Captcha Application with an Image Generator Class with PHP 5 - The complete definition of the image generator class


    (Page 2 of 4 )

    Before building the noisy image application that I mentioned in the beginning of this article, I will list the complete signature corresponding to the image generator class that I created in the preceding article of the series. Doing so should give you a much better idea of how this class can be easily "plugged" to a string randomizer system to create a noisy image generation mechanism.

    All right, having said that, here's the full definition of the aforementioned image generator class, as it was originally built in the previous article of the series. Have a look at it, please:

    // define 'ImageGenerator' class

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

     }

    }

    As you can see, the logic that drives the above image generator class is very simple to follow. The class takes up a few input parameters, including the foreground and background colors of the image stream being generated, and the text string that will be embedded into the stream.

    These basic tasks are carried out by the two primary methods of the class, called "buildImageStream()" and "displayImage()" respectively.

    However, if you study in detail the signature of the previous class, you'll probably see that it seems pretty inflexible, since it actually builds the corresponding image stream whenever the class is instantiated. This prohibits the creation of other, different streams during the execution of given script.

    Therefore, in the section to come I'll introduce some important modifications to the original structure of the image generator class to solve this issue. I'll also build a simple text randomizer, which will be extremely useful for creating an efficient noisy image application.

    To see how all of these interesting tasks will be carried out, please jump forward and read the next few lines. They're just one click away.

    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

    - Viewing and Editing Tasks for a Project Mana...
    - More on Private Methods with PHP 5 Member Vi...
    - Adding Tasks to a Project Management Applica...
    - Utilizing Private Methods with PHP 5 and Mem...
    - Making Changes in a Project Management Appli...
    - Defining Public and Protected Methods with M...
    - HTML for a Project Management Application
    - Using Subclasses and Accessors with Member V...
    - Implementing Internet Protocols with PHP
    - Project Management: The Application
    - Working with Private Properties to Protect P...
    - Protecting PHP 5 Class Data with Member Visi...
    - Setting Up a Web-based Image Hosting Service
    - Comparing Files and Databases with PHP Bench...
    - Setting Up a Web-Based Image Gallery





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