As I stated in the prior section, the original signature of the "ImageGenerator" class lacked an important feature, the ability to perform an adequate validation on all of its incoming parameters. Since this capacity should be properly incorporated into the class, I will modify the initial implementation of the constructor to provide it with the capacity to check the validity of the different parameters taken by the class. Given that, here's how the "ImageGenerator" class now looks after modifying the definition of its constructor method: // define 'ImageGenerator' class (improved version - performs a class ImageGenerator{ private $width; private $height; private $bgColor; private $textColor; private $inputString; private $img; // initialize input arguments public function __construct($inputString='Default Input 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 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(){ // implementation of this method goes here }
// display image stream on the browser public function displayImage(){ // implementation of this method goes here } } As you can see, the constructor of the above "ImageGenerator" class incorporates a few additional lines of code for validating individually each of its incoming parameters, before assigning them as class properties. Of course, this checking process could be improved, particularly if you need to perform a strict validation on the respective input arguments, but I think that the current checking process is more than enough. So far, so good. At this stage I already showed you how to improve the implementation of the constructor that belongs to the "ImageGenerator" class, with the purpose of making it slightly more efficient and robust. So, what's the next step? Well, since the signature of the previous image generator class now looks more compact and solid, I believe that it's time to reassemble its different parts, and develop a concrete example where its functionality can be tested, at least at a very basic level. Considering this scenario, in the upcoming section of this tutorial I'll be showing you the complete (and improved) source code of this image generator class so you can see how it can be used to display several text strings in PNG format. As I said before, this will be done in the next few lines, so click on the link shown below and keep reading.
blog comments powered by Disqus |
|
|
|
|
|
|
|