imagefilledpolygon

(PHP 3, PHP 4, PHP 5)

imagefilledpolygon -- 画一多边形并填充

说明

bool imagefilledpolygon ( resource image, array points, int num_points, int color )

imagefilledpolygon()image 图像中画一个填充了的多边形。

points 参数是一个按顺序包含有多边形各顶点的 xy 坐标的数组。

num_points 参数是顶点的总数,必须大于 3。

例子 1. imagefilledpolygon() 例子

<?php
// 建立多边形各顶点坐标的数组
$values = array(
            
40,  50,  // Point 1 (x, y)
            
20,  240, // Point 2 (x, y)
            
60,  60,  // Point 3 (x, y)
            
240, 20,  // Point 4 (x, y)
            
50,  40,  // Point 5 (x, y)
            
10,  10   // Point 6 (x, y)
            
);

// 创建图像
$image = imagecreatetruecolor(250, 250);

// 设定颜色
$bg   = imagecolorallocate($image, 200, 200, 200);
$blue = imagecolorallocate($image, 0, 0, 255);

// 画一个多边形
imagefilledpolygon($image, $values, 6, $blue);

// 输出图像
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
?>


add a note add a note User Contributed Notes
jylyn at hotmail dot com
17-Oct-2006 08:11
In spite of what it says about requiring more than 3 vertices, it is possible to draw a triangle with this function!
tatlar at yahoo dot com
12-Sep-2006 08:08
<?php function _makeFiveSidedStar( $x, $y, $radius, $shape='polygon', $spiky=NULL ) {
  
// $x, $y co-ords of origin (in pixels), $radius (in pixels), $shape - 'polygon' or 'star', $spikiness - ratio between 0 and 1
  
$point = 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 // 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' ) ;
// Put values into imagepolygon function. You need to define the $image and $color, and flush it out to an image type.?>
etnekor at tar dot hu
23-Jan-2006 05:18
There is a simple function to draw a filled point with a chosen radius and color.

<?php
function drawPoint($img, $radius, $origo_x, $origo_y, $pointColor)
{
  for (
$i=0;$i<=360;$i++)
  {
  
$pont[] = $origo_x + ($radius * sin(deg2rad($i)));
  
$pont[] = $origo_y - ($radius * cos(deg2rad($i)));
  }
 
reset($pont);
 
ImageFilledPolygon ($img, $pont, (sizeof($pont)/2), $pointColor);
}
?>