18.10 Drawing Polygons


You want to draw a polygon, which is any figure with more than one side.

Technique

Use the ImagePolygon() and the ImageFilledPolygon() functions to draw polygons.

Example 1 ”A simple polygon (not filled):

 <?php header("Content-type: image/gif"); $im = ImageCreate(100, 20); $white = ImageColorAllocate($im, 255, 255, 255); $light_blue = ImageColorAllocate($im, 20, 93, 233); $points = array(12, 10, 15, 20, 50, 17, 70, 10); ImagePolygon($im, $points, 4, $light_blue); ImageGif($im); ImageDestroy($im); ?> 

Example 2 ”A simple, filled polygon:

 <?php header("Content-type: image/gif"); $im = ImageCreate(100, 20); $white = ImageColorAllocate($im, 255, 255, 255); $light_blue = ImageColorAllocate($im, 20, 93, 233); $points = array(12, 10, 15, 20, 50, 17, 70, 10); ImageFilledPolygon($im, $points, 4, $light_blue); ImageGif($im); ImageDestroy($im); ?> 

Comments

In example 1, we draw a polygon on the image stream, $im , with vertices of (12, 10), (15, 20), (50, 17), and (70, 10) with a border color of light blue. We achieve this by using the ImagePolygon() function which takes an image stream, $im , as its first argument. Its second argument is an array of points (in pairs), followed by the number of vertices there are to be on the polygon, and then finally the color to draw the border of the polygon.

In the example 2, we draw the same polygon as in example 1, but this time we use the ImageFilledPolygon() function which fills in the polygon with the color given by the last argument. The argument order and content are the same for both ImagePolygon() and ImageFilledPolygon() .



PHP Developer's Cookbook
PHP Developers Cookbook (2nd Edition)
ISBN: 0672323257
EAN: 2147483647
Year: 2000
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net