2024 年 PHP Conference Japan

imagepolygon

(PHP 4, PHP 5, PHP 7, PHP 8)

imagepolygon繪製多邊形

說明

PHP 8.0.0 起的簽章(不支援命名參數)

imagepolygon(GdImage $image, 陣列 $points, 整數 $color): 布林值

替代簽章(自 PHP 8.1.0 起棄用)

imagepolygon(
    GdImage $image,
    陣列 $points,
    整數 $num_points,
    整數 $color
): 布林值

imagepolygon() 在指定的 image 中建立一個多邊形。

參數

image

一個 GdImage 物件,由其中一個影像建立函式返回,例如 imagecreatetruecolor()

points

一個包含多邊形頂點的陣列,例如:

points[0] = x0
points[1] = y0
points[2] = x1
points[3] = y1

num_points

頂點總數,至少必須為 3。

如果根據第二個簽章省略此參數,points 必須具有偶數個元素,並且 num_points 假設為 count($points)/2
color

使用 imagecolorallocate() 建立的顏色識別碼。

返回值

成功時返回 true,失敗時返回 false

更新日誌

版本 說明
8.1.0 參數 num_points 已被棄用。
8.0.0 image 現在需要一個 GdImage 實例;先前需要一個有效的 gd 資源

範例

範例 #1 imagepolygon() 範例

<?php
// 建立空白影像
$image = imagecreatetruecolor(400, 300);

// 配置多邊形的顏色
$col_poly = imagecolorallocate($image, 255, 255, 255);

// 繪製多邊形
imagepolygon($image, array(
0, 0,
100, 200,
300, 200
),
3,
$col_poly);

// 將圖片輸出到瀏覽器
header('Content-type: image/png');

imagepng($image);
imagedestroy($image);
?>

上述範例將輸出類似以下的內容

Output of example : imagepolygon()

另請參閱

新增註解

使用者貢獻的註解 3 則註解

tatlar at yahoo dot com
18 年前
用於取得五邊形或五角星形狀座標的函式。
<?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' ) ;
?>
licson0729 at gmail dot com
12 年前
用於繪製 n 邊正多邊形的函式

<?php
$img
= imagecreatetruecolor(1360,768);

function
regularPolygon($img,$x,$y,$radius,$sides,$color)
{
$points = array();
for(
$a = 0;$a <= 360; $a += 360/$sides)
{
$points[] = $x + $radius * cos(deg2rad($a));
$points[] = $y + $radius * sin(deg2rad($a));
}
return
imagepolygon($img,$points,$sides,$color);
}

regularPolygon($img,1360/2,768/2,300,8,0xffffff);//測試繪製

header('Content-type: image/png');
imagepng($img);
imagedestroy($img);
?>
jsnell at networkninja dot com
23 年前
以下是一些用於多邊形旋轉和平移的簡便例程。也可以輕鬆地新增縮放功能。

<?php
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;
/* 數學公式:
[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;
}
?>
To Top