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- $bgColor=explode(',',$this->bgColor); $textColor=explode(',',$this->textColor); // allocate background color on image stream imagecolorallocate($this->img,$bgColor[0],$bgColor[1],$bgColor // allocate text color on image stream $textColor=imagecolorallocate($this->img,$textColor[0],$textColor // load font if(!imagestring($this->img,5,$this->width/2-strlen($this- 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.
blog comments powered by Disqus |
|
|
|
|
|
|
|