如何使用 PHP 中的 imagefilledpolygon() 函式繪製填充多邊形?


imagefilledpolygon() 是一個內建的 PHP 函式,用於繪製填充多邊形。

語法

bool imagefilledpolygon($image, $points, $num_points, $color)

引數

imagefilledpolygon() 接受四個不同的引數 - $image、$points、$num_points 和 $color。

  • $image - 使用 imagecreatetruecolor() 函式建立特定大小的空白影像。

  • $points - 包含多邊形的連續頂點。

  • $num_points - 包含多邊形中的頂點總數。要建立一個多邊形,點/頂點的總數必須至少為三個。

  • $color - 使用 imagecolorallocate() 函式包含填充顏色的識別符號。

返回值

成功時返回 True,失敗時返回 False。

示例 1

<?php
   // set up array of points for a polygon
   $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)
   );
   // create the image using imagecreatetruecolor function
   $img = imagecreatetruecolor(700, 350);

   // allocated the blue and gray colors
   $bg = imagecolorallocate($img, 122, 122, 122);
   $blue = imagecolorallocate($img, 0, 0, 255);

   // filled the background
   imagefilledrectangle($img, 0, 0, 350, 350, $bg);

   // draw a polygon
   imagefilledpolygon($img, $values, 6, $blue);

   // flush image
   header('Content-type: image/png');
   imagepng($img);
   imagedestroy($img);
?>

輸出

示例 2

<?php
   // Set the vertices of the polygon
   $values = array(
      150, 50, // Point 1 (x, y)
      55, 119, // Point 2 (x, y)
      91, 231, // Point 3 (x, y)
      209, 231, // Point 4 (x, y)
      245, 119 // Point 5 (x, y)
   );
   // It creates the size of the image or blank image.
   $img = imagecreatetruecolor(700, 350);

   // Set the gray background image color
   $bg = imagecolorallocate($img, 122, 122, 122);

   // Set the red image color
   $red = imagecolorallocate($img, 255, 0, 0);

   // fill the background
   imagefilledrectangle($img, 0, 0, 350, 350, $bg);

   // Draw the polygon image
   imagefilledpolygon($img, $values, 5, $red);

   // Output of the image.
   header('Content-type: image/png');
   imagepng($img);
   imagedestroy($img);
?>

輸出

更新於: 2021-08-09

310 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告