如何使用 PHP 中的 imagefilledpolygon() 函数绘制填充多边形?

phpserver side programmingprogramming

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
   // 设置多边形的点数组
   $values = array(
      40, 50, // 点 1 (x, y)
      20, 240, // 点 2 (x, y)
      60, 60, // 点 3 (x, y)
      240, 20, // 点 4 (x, y)
      50, 40, // 点 5 (x, y)
      10, 10 // 点 6 (x, y)
   );
   // 使用 imagecreatetruecolor 函数创建图像
   $img = imagecreatetruecolor(700, 350);

   // 分配蓝色和灰色
   $bg = imagecolorallocate($img, 122, 122, 122);
   $blue = imagecolorallocate($img, 0, 0, 255);

   // 填充背景
   imagefilledrectangle($img, 0, 0, 350, 350, $bg);

   // 绘制多边形
   imagefilledpolygon($img, $values, 6, $blue);

   // 刷新图像
   header('Content-type: image/png');
   imagepng($img);
   imagedestroy($img);
?>

输出

示例 2

<?php
   // 设置多边形的顶点
   $values = array(
      150, 50, // 点 1 (x, y)
      55, 119, // 点 2 (x, y)
      91, 231, // 点 3 (x, y)
      209, 231, // 点 4 (x, y)
      245, 119 // 点 5 (x, y)
   );
   // 创建图像或空白图像的大小。
   $img = imagecreatetruecolor(700, 350);

   // 设置灰色背景图像颜色
   $bg = imagecolorallocate($img, 122, 122, 122);

   // 设置红色图像颜色
   $red = imagecolorallocate($img, 255, 0, 0);

   // 填充背景
   imagefilledrectangle($img, 0, 0, 350, 350, $bg);

   // 绘制多边形图像
   imagefilledpolygon($img, $values, 5, $red);

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

输出


相关文章