如何使用 PHP 中的 imageopenpolygon() 函数绘制开放多边形?

phpserver side programmingprogramming

imageopenpolygon() 是 PHP 中的内置函数,用于在给定图像上绘制开放多边形。

语法

bool imageopenpolygon(resource $image,array $points,int $num_points,int $color)

参数

imageopenpolygon() 有四个不同的参数:$image、$points、$num_points 和$color。 

  • $image − 指定要处理的图像资源。

  • $image − 指定要处理的图像资源。

  • $points − 指定多边形的点。

  • $num_points − 指定点的数量。(顶点)点的总数必须至少为三个。

  • $color −此参数指定多边形的颜色。

返回值

imageopenpolygon() 成功时返回 True,失败时返回 False。

示例 1

<?php
   // 使用 imagecreatetruecolor() 函数创建空白图像。
   $img = imagecreatetruecolor(700, 300);

   // 为多边形分配颜色
   $col_poly = imagecolorallocate($img, 0, 255, 0);

   // 绘制多边形
   imageopenpolygon($img, array(
      0, 0,
      100, 200,
      400, 200
   ),
   3,
   $col_poly);

   // 将图片输出到浏览器
   header('Content-type: image/png');
   imagepng($img);
   imagedestroy($img);
?>

输出

示例 2

<?php
   // 使用 imagecreatetruecolor() 函数创建空白图像。
   $image = imagecreatetruecolor(700, 300);
   
   // 分配颜色
   $blue = imagecolorallocate($image, 0, 255, 255);

   // 数组的六个点
   $points = array(
      60, 130,
      130, 230,
      280, 230,
      350, 130,
      210, 30,
      60, 130
   );
   
   // 创建多边形
   imageopenpolygon($image, $points, 6, $blue);

   // 输出到浏览器
   header('Content-type: image/png');
   imagepng($image);
   imagedestroy($image);
?>

输出


相关文章