HomePHP Page 2 - Drawing More Complex Shapes with the GD Library in PHP
Displaying filled images and arcs - 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 stated in the beginning of this series, the GD extension is useful for creating dynamic image streams from scratch, and offers a remarkable capacity for preprocessing existing graphics. Now we are going to see how well it fills up different types of basic shapes before displaying them on the browser.
This is the purpose of the brand new "imagefill()" and "imagefilledarc()" functions. As their names suggest, they perform a filling operation on a given image stream. The function called "imagefill()" performs a flood fill on a specified image; its use is shown in the code sample below:
// example of 'imagefill()' function
try{ if(!$img=imagecreatetruecolor(300,200)){ throw new Exception('Error creating image'); } // allocate blue color $blue=imagecolorallocate($img,0,0,255); imagefill($img,0,0,$blue); header("Content-type: image/gif"); imagegif($img);
As you can see, the "imagefill()" function is really handy for flood filling a specified image stream with a predefined foreground color. It starts this operation at the X, Y coordinates, which are naturally specified as part of the corresponding input parameters.
So far, so good, right? Now that you hopefully learned how the "imagefill()" GD function works, let me show you another example. In this case, the "imagefilledarc()" is used, not surprisingly, to fill in a basic arc with a specific foreground color.
That being explained, here's the corresponding code sample that shows a simple implementation for this function:
// example of 'imagefilledarc()' function try{ if(!$img=imagecreatetruecolor(300,200)){ throw new Exception('Error creating image'); } // allocate blue color $blue=imagecolorallocate($img,0,0,255); // draw filled arc imagefilledarc($img,50,50,100,50,0,90,$blue,IMG_ARC_PIE); header("Content-type: image/gif"); imagegif($img);
In this case, the "imagefilledarc()" function that comes bundled with the GD library is used to display a filled arc with a predetermined foreground color on the browser. Also, the referenced function takes the starting and ending coordinates of the arc as its input parameters, along with the image stream used to display the filled shape.
Given that, the image created by the previous hands-on example is similar to the one shown below:
Certainly, after you analyze in detail the previous practical example, you'll agree with me that drawing and flood filling basic shapes by using the specific functions provided by the GD extension is truly a no-brainer process that can be performed with minor difficulties. Thus, having covered these two handy functions of this library, it's time to explore others that are also part of the library.
In the following section I'll teach you how to use a few additional functions that come packaged with the GD extension to display some other filled shapes on the browser, like basic polygons.
Want to see how this will be achieved? Click on the link that appears below and keep reading.