HomePHP Page 3 - Drawing More Complex Shapes with the GD Library in PHP
Drawing filled polygons with the GD library - PHP
The GD library that comes bundled with PHP 4 and PHP 5 provides web developers with a robust set of intuitive functions for generating and handling dynamic graphics with minor hassles. If you're interested in mastering the numerous features integrated with this PHP extension, this series of instructive tutorials will make the whole learning experience a truly painless process.
As I explained in the previous section, the GD library comes bundled with functions that display different types of polygons on the browser. I'm going to show you a couple of new functions for drawing filled rectangles, along with polygons that have more than four sides.
That being said, please take a look at the following code sample. It demonstrates how to use the "imagefilledrectangle" GD function. This function is tasked with displaying different flood filled rectangles on the browser. Here is how the example in question looks:
// example of 'imagefilledrectangle()' function - draws a white rectangle
try{ if(!$image=imagecreatetruecolor(300,200)){ throw new Exception('Error creating image'); } // allocate some basic colors if(!$whiteColor=imagecolorallocate($img,255,255,255)){ throw new Exception('Error allocating color'); } // draw a white rectangle imagefilledrectangle($image,10,10,100,100,$whiteColor); // display image to the browser header("Content-type: image/gif"); imagegif($image); // free memory imagedestroy($image);
As shown above, the handy "imagefilledectangle()" function can be useful for displaying quickly a rectangle which has been previously filled with a given foreground color. In this case, the example displays a basic white rectangle positioned at the X,Y coordinates specified by the respective input parameters. This generates an image similar to the one shown below:
All right, now that you hopefully learned how the previous "imagefilledrectangle()" function works, please pay close attention to the following example. It shows a simple implementation for another handy drawing function, called "imagefilledpolygon()". As you might have guessed, this one draws a predefined polygon, filled with a given foreground color. The way that this operation is carried out is demonstrated by the script below:
// example of 'imagefilledpolygon()' function
try{ if(!$img=imagecreatetruecolor(300,200)){ throw new Exception('Error creating image'); } // define polygon coordinates $coord= array(10,50,50,100,100,30,30,10); // allocate blue color $blue=imagecolorallocate($img,0,0,255); // draw filled polygon imagefilledpolygon($img,$coord,4,$blue); header("Content-type: image/gif"); imagegif($img);
Certainly, the "imagefilledpolygon()" function is very convenient for displaying on the browser different types of polygons which have been previously filled with a specified foreground color. The final image created by the previous script looks similar to this one:
So far, so good. At this point, I have shown you a considerable number of functions provided by the GD extension that can be used to perform all sorts of flood filling tasks on distinct shapes. This feature makes this library quite helpful for generating more complex graphics from primitive forms.
Thus, considering that the way all of these functions work is simple to grasp, I'll dedicate the last section of this tutorial to covering a few more handy functions packaged with this graphical library. These functions can be utilized for retrieving specific information about a given image stream, quickly and easily.
As you'll see in a moment, these functions are really useful, so jump ahead and read the next few lines. I'll be there, waiting for you.