search for in the  
<imagepngimagepsbbox>
Last updated: Thu, 19 May 2005

imagepolygon

(PHP 3, PHP 4, PHP 5)

imagepolygon -- Draw a polygon

Description

int imagepolygon ( resource image, array points, int num_points, int color )

imagepolygon() creates a polygon in image id. points is a PHP array containing the polygon's vertices, i.e. points[0] = x0, points[1] = y0, points[2] = x1, points[3] = y1, etc. num_points is the total number of points (vertices).

Example 1. imagepolygon() example

<?php
// create a blank image
$image = imagecreate(400, 300);

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

// choose a color for the polygon
$col_poly = imagecolorallocate($image, 255, 255, 255);

// draw the polygon
imagepolygon($image,
             array (
                  
0, 0,
                  
100, 200,
                  
300, 200
            
),
            
3,
            
$col_poly);

// output the picture
header("Content-type: image/png");
imagepng($image);

?>

See also imagecreate() and imagecreatetruecolor().



User Contributed Notes
imagepolygon
jsnell at networkninja dot com
17-Feb-2001 09:07
Here are some handy routines for rotation and translation of polygons.  Scaling could be added easily as well.

function translate_point(&$x,&$y,$angle,$about_x,$about_y,$shift_x,$shift_y)
{
   $x -= $about_x;
   $y -= $about_y;
   $angle = ($angle / 180) * M_PI;
/* math:
[x2,y2] = [x,  *  [[cos(a),-sin(a)],
           y]      [sin(a),cos(a)]]
==>
x = x * cos(a) + y*sin(a)
y = x*-sin(a) + y*cos(a)
*/

   $new_x = $x * cos($angle) - $y * sin($angle);
   $new_y = $x * sin($angle) + $y * cos($angle);
   $x = $new_x+ $about_x + $shift_x ;
   $y = $new_y + $about_y + $shift_y;
}

function translate_poly($point_array, $angle, $about_x, $about_y,$shift_x,$shift_y)
{
   $translated_poly = Array();
   while(count($point_array) > 1)
   {
       $temp_x = array_shift($point_array);
       $temp_y = array_shift($point_array);
       translate_point($temp_x, $temp_y, $angle, $about_x, $about_y,$shift_x, $shift_y);
       array_push($translated_poly, $temp_x);
       array_push($translated_poly, $temp_y);
   }
   return $translated_poly;
}

<imagepngimagepsbbox>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT