In consonance with the concepts that I deployed in the section that you just read, the only thing that remains to be covered here is demonstrating a basic implementation of the image generator class, including the improvements that I introduced earlier to the corresponding constructor. Below I listed the entire definition of the class in question, along with a basic practical example, which simply shows how to output a sample text string on the browser in PNG format. The corresponding code sample is as follows: // define 'ImageGenerator' class (improved version) 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(){ 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 $font=imageloadfont($this->font); if(!imagestring($this->img,$this->font,$this->width/2-strlen 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); } } // 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(); } After studying the improved definition for the above image generator class, you'll certainly agree with me that outputting different strings in a predetermined graphic format is actually a no-brainer process that can be performed with minor problems. As usual, you're completely free to modify all the code samples shown here and add your own improvements. It's really fun, trust me! Final thoughts As demonstrated in this second article of the series, the image generator class is quite useful for displaying text strings in a predefined graphic format. However, maybe you're wondering at this very moment: what's the point in doing this? Well, as I explained before, the class on its own may seen rather useless, but when combined with other PHP applications, it can be utilized to embed plain texts into different image streams to generate dynamic banners, or the so-called noisy images. However, the creation of noisy images with the previous image generator class will be covered in detail in the last part of the series, so you don't have any excuses to miss it!
blog comments powered by Disqus |
|
|
|
|
|
|
|