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.
Next: Listing the complete source code of the noisy image script >>
More DHTML Articles
More By Alejandro Gervasio