Home arrow PHP arrow 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.

  1. Drawing More Complex Shapes with the GD Library in PHP
  2. Displaying filled images and arcs
  3. Drawing filled polygons with the GD library
  4. Retrieving information about a given image stream
By: Alejandro Gervasio
Rating: starstarstarstarstar / 6
August 22, 2007

print this article



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

     throw new Exception('Error creating image');
   // allocate some basic colors
     throw new Exception('Error allocating color');
   // draw a white rectangle
   // display image to the browser
   header("Content-type: image/gif");
   // free memory

catch(Exception $e){
   echo $e->getMessage();

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

     throw new Exception('Error creating image');
   // define polygon coordinates
   $coord= array(10,50,50,100,100,30,30,10);
   // allocate blue color
   // draw filled polygon
   header("Content-type: image/gif");

catch(Exception $e){
   echo $e->getMessage();

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.

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: