1. Home
  2. Computing & Technology
  3. PHP / MySQL

Drawing a Polygon with PHP

By Angela Bradley, About.com

Using the imagepolygon() function: You can create a shape using coordinates with the imagepolygon () function. You need to include an imagecreate (), the coordinates for your shape in an array, the number of coordinates, and the line color you wish to use as variables in the function.
<?php
// create a blank image
$image = imagecreate(200, 110) ;

// background color
$bg = imagecolorallocate($image, 225, 0, 0) ;

// line color
$color = imagecolorallocate($image, 255, 255, 255) ;

// draw the shape
imagepolygon($image,
array (
0, 0,
100, 90,
180, 100,
100,10 ),
4,
$color);

// output the picture
header("Content-type: image/png") ;
imagepng($image) ;
?>
You can also cross coordinates to make more abstract shapes. In the example we are saving the file as a PNG type file but it could also be changed to make a JPG. Like other GD Library functions, drawing a polygon can be used with other functions to make more complex images.

Explore PHP / MySQL

More from About.com

  1. Home
  2. Computing & Technology
  3. PHP / MySQL
  4. Graphics- GD Library
  5. Image Polygon PHP - Imagepolygon FunctionPHP - Draw Shape with PHP

©2008 About.com, a part of The New York Times Company.

All rights reserved.