Home arrow DHTML arrow Page 3 - An Object-Based Approach to Building Noisy Images
DHTML

An Object-Based Approach to Building Noisy Images


Noisy images can be a useful technique for preventing automated submissions of online forms. If you want to learn how to put this approach to work for you, this article provides you with distilled material on the topic. Welcome to the second installment of the series "Building Noisy Images." Over the course of this educational journey, you'll see how to build simple - yet effective -- noisy images, by using a combination of basic structural markup, CSS styles and a tiny bit of PHP.

Author Info:
By: Alejandro Gervasio
Rating: 5 stars5 stars5 stars5 stars5 stars / 3
May 15, 2007
TABLE OF CONTENTS:
  1. · An Object-Based Approach to Building Noisy Images
  2. · Creating noisy images with a procedural approach
  3. · Building a noisy image with an object-based approach
  4. · Listing the complete source code of the noisy image script

print this article
SEARCH DEVARTICLES

An Object-Based Approach to Building Noisy Images - Building a noisy image with an object-based approach
(Page 3 of 4 )

In consonance with the concepts that I deployed in the previous section, my purpose here is to demonstrate how a simple noisy image can be created by using a PHP class instead of using a procedural function. Therefore, below I included the definition of a brand new PHP class, called "RandomGenerator." As its name implies, it is tasked with generating the four-digit random values contained within the corresponding noisy image.

Please take some time to examine this class, shown below: 

class RandomGenerator{
   var $length;
   var $chars="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
   function RandomGenerator($length=4){
     if(!is_int($length)||$length<1||$length>strlen($this-
>chars)){
       trigger_error('Invalid length for random
string',E_USER_ERROR);
     }
     $this->length=$length;
   }
   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,  regardless of the simple definition of the previous class, its functionality is quite remarkable. In plain terms, the class in question is comprised of two basic methods (including the corresponding constructor, logically), and its main purpose is to return to client code the random strings required to create the pertinent noisy image.

I have to admit that the prior "RandomGenerator" class is a simple wrapper for the procedural function that was defined in the first article of the series. However, if you feel inclined to work with object-based web applications, this class should be very appropriate to your programming needs.

So far, so good. At this point I hopefully demonstrated in a friendly fashion how to use a basic PHP class to generate the random strings required to make the noisy images work properly.

So, what's the next step concerning the implementation of the noisy images? Well, since I wish you to see how this brand new class can be neatly linked to the same PHP file that was defined in the preceding article of the series, in the section to come I'm going to list the complete source code of this file, logically including the "RandomGenerator" class.

To read the next chapter of this article, please click on the link below and keep reading.


blog comments powered by Disqus
DHTML ARTICLES

- Text-Justify, Volume, and Other Style Sheet ...
- Ruby-Position, Size, and Other Style Sheet P...
- Padding, Pages, and More Style Sheet Propert...
- Marks, Orphans, and More Style Sheet Propert...
- Layouts, Margins, and Other Style Sheet Prop...
- Floats, Fonts, and Other Style Sheet Propert...
- Color, Filters, and Other Style Sheet Proper...
- Borders and More with Style Sheets
- Learning Style Sheet Properties
- Style Sheet Property Reference
- Completing a Noisy Image Application
- An Object-Based Approach to Building Noisy I...
- A Basic Method for Building Noisy Images
- Adding More Features to Sliders with the Scr...
- Using Sliders with the Scriptaculous Framewo...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials