Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Image Functions : imagepolygon

imagepolygon

Draws a polygon (PHP 4, PHP 5)
bool imagepolygon ( resource image, array points, int num_points, int color )

Example 1018. imagepolygon() example

<?php
// create a blank image
$image = imagecreatetruecolor(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);

?>

The above example will output something similar to:


Code Examples / Notes » imagepolygon

glowell

Something to be aware of, ImagePolygon appears to convert the array of points passed to it from whatever format they may have been in originally into integers. This means if you pass it an array of floats (after running a rotation routine for example) the floats will be changed to integers INSIDE THE ORIGINAL ARRAY.
An extreme example: if for some reason you had an unit-sized polygon pt-array ( -1<x|y<1 for easy scaling purpose for instance) and for some reason your code calls imagepolygon on it (why? it'd only be a dot anyway) the array would be unusable after that (all either 1s, 0s or -1s). Scaling a unit-sized array, drawing it and then scaling it again will also may have a different result than expected.
Obviously, if the array in its original state is important to your code, it should use a copy of the original array for this call. If your code draws the same polygon multiple times but resizes it for different cases, you should have each size be created off an original template rather than adjusting a single polygon array.


jsnell

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;
}


tatlar

Function to get 5-sided polygon (pentagon) or star (pentagram) co-ords.
<?php
function _makeFiveSidedStar( $x, $y, $radius, $shape='polygon', $spiky=NULL ) {
   $point = array() ; // new array                                                                                                                  
   $angle = 360 / 5 ;                                                                                                                    
   $point[0]['x'] = $x ;                                                                                                                
   $point[0]['y'] = $y - $radius ;                                                                                                      
   $point[2]['x'] = $x + ( $radius * cos( deg2rad( 90 - $angle ) ) ) ;
   $point[2]['y'] = $y - ( $radius * sin( deg2rad( 90 - $angle ) ) ) ;
   $point[4]['x'] = $x + ( $radius * sin( deg2rad( 180 - ( $angle*2 ) ) ) ) ;
   $point[4]['y'] = $y + ( $radius * cos( deg2rad( 180 - ( $angle*2 ) ) ) ) ;
   $point[6]['x'] = $x - ( $radius * sin( deg2rad( 180 - ( $angle*2 ) ) ) ) ;                                                            
   $point[6]['y'] = $y + ( $radius * cos( deg2rad( 180 - ( $angle*2 ) ) ) ) ;
   $point[8]['x'] = $x - ( $radius * cos( deg2rad( 90 - $angle ) ) ) ;                                                                  
   $point[8]['y'] = $y - ( $radius * sin( deg2rad( 90 - $angle ) ) ) ;
   if( $shape == 'star' ) {
       if( $spiky == NULL ) $spiky = 0.5 ;  // degree of spikiness, default to 0.5
       $indent = $radius * $spiky ;
       $point[1]['x'] = $x + ( $indent * cos( deg2rad( 90 - $angle/2 ) ) ) ;                                                            
       $point[1]['y'] = $y - ( $indent * sin( deg2rad( 90 - $angle/2 ) ) ) ;                                                    
       $point[3]['x'] = $x + ( $indent * sin( deg2rad( 180 - $angle ) ) ) ;                                                              
       $point[3]['y'] = $y - ( $indent * cos( deg2rad( 180 - $angle ) ) ) ;
       $point[5]['x'] = $x ;                                                                                                            
       $point[5]['y'] = $y + ( $indent * sin( deg2rad( 180 - $angle ) ) ) ;
       $point[7]['x'] = $x - ( $indent * sin( deg2rad( 180 - $angle ) ) ) ;                                                              
       $point[7]['y'] = $y - ( $indent * cos( deg2rad( 180 - $angle ) ) ) ;                                                              
       $point[9]['x'] = $x - ( $indent * cos( deg2rad( 90 - $angle/2 ) ) ) ;                                                            
       $point[9]['y'] = $y - ( $indent * sin( deg2rad( 90 - $angle/2 ) ) ) ;
   }
   ksort( $point ) ;
   $coords = array() ;  // new array                                                                                                                
   foreach( $point as $pKey=>$pVal ) {                                                                                                  
       if( is_array( $pVal ) ) {                                                                                                        
           foreach( $pVal as $pSubKey=>$pSubVal ) {                                                                                      
               if( !empty( $pSubVal ) ) array_push( $coords, $pSubVal ) ;                                                                
           }                                                                                                                            
       }                                                                                                                                
   }
   return $coords ;
}
$values = _makeFiveSidedStar( 100, 100, 50, 'star' ) ;
?>


Change Language


Follow Navioo On Twitter
gd_info
getimagesize
image_type_to_extension
image_type_to_mime_type
image2wbmp
imagealphablending
imageantialias
imagearc
imagechar
imagecharup
imagecolorallocate
imagecolorallocatealpha
imagecolorat
imagecolorclosest
imagecolorclosestalpha
imagecolorclosesthwb
imagecolordeallocate
imagecolorexact
imagecolorexactalpha
imagecolormatch
imagecolorresolve
imagecolorresolvealpha
imagecolorset
imagecolorsforindex
imagecolorstotal
imagecolortransparent
imageconvolution
imagecopy
imagecopymerge
imagecopymergegray
imagecopyresampled
imagecopyresized
imagecreate
imagecreatefromgd2
imagecreatefromgd2part
imagecreatefromgd
imagecreatefromgif
imagecreatefromjpeg
imagecreatefrompng
imagecreatefromstring
imagecreatefromwbmp
imagecreatefromxbm
imagecreatefromxpm
imagecreatetruecolor
imagedashedline
imagedestroy
imageellipse
imagefill
imagefilledarc
imagefilledellipse
imagefilledpolygon
imagefilledrectangle
imagefilltoborder
imagefilter
imagefontheight
imagefontwidth
imageftbbox
imagefttext
imagegammacorrect
imagegd2
imagegd
imagegif
imagegrabscreen
imagegrabwindow
imageinterlace
imageistruecolor
imagejpeg
imagelayereffect
imageline
imageloadfont
imagepalettecopy
imagepng
imagepolygon
imagepsbbox
imagepsencodefont
imagepsextendfont
imagepsfreefont
imagepsloadfont
imagepsslantfont
imagepstext
imagerectangle
imagerotate
imagesavealpha
imagesetbrush
imagesetpixel
imagesetstyle
imagesetthickness
imagesettile
imagestring
imagestringup
imagesx
imagesy
imagetruecolortopalette
imagettfbbox
imagettftext
imagetypes
imagewbmp
imagexbm
iptcembed
iptcparse
jpeg2wbmp
png2wbmp
eXTReMe Tracker