Before I introduce the modifications that I mentioned in the beginning of this article, I'd like to list the complete source code for the image generator class as it was initially defined in the first tutorial of the series. I believe this will give you a much better idea of how these improvements will affect the functionality of the class in question. Thus, having clarified this important point, here's the full signature of the "ImageGenerator" class as it was built in the previous installment of this series: // 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 $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 // allocate text color on image stream $textColor=imagecolorallocate($this->img,$this->textColor if(!imagestring($this->img,5,$this->width/2-strlen($this- 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); } } Okay, now that you remember how the signature of the above image generator class originally looked, please examine the following pair of code samples. They demonstrate a simple utilization of this class. Here they are: // display default input string try{ // create new instance of 'ImageGenerator' class $imgGen=new ImageGenerator(); // display image stream on the browser $imgGen->displayImage(); } catch(Exception $e){ echo $e->getMessage(); exit(); } // display sample input string try{ // create new instance of 'ImageGenerator' class $imgGen=new ImageGenerator('This is a sample string'); // display image stream on the browser $imgGen->displayImage(); } catch(Exception $e){ echo $e->getMessage(); exit(); } Having demonstrated how to use the "ImageGenerator" class to display on the browser some primitive input strings in PNG format, it's time to analyze in detail its pitfalls. First, as you can see, the structure of the class is pretty flexible, but definitely doesn't allow you to perform a decent validation on its incoming parameters. Naturally, this crucial issue should be fixed quickly to make the class slightly more efficient. Thus, in the course of the next section, I'll be introducing some important modifications to the constructor of the class, so it can perform an adequate validation on all of its input arguments. To see how these improvements will be added to the image generator class, please click on the link that appears below and keep reading.
blog comments powered by Disqus |
|
|
|
|
|
|
|