AI编程助手
AI免费问答

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

王林   2023-09-14 17:45   1038浏览 转载

imagefilledpolygon() 是一个内置 php 函数,用于绘制填充多边形。

语法

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

参数

imagefilledpolygon()采用四个不同的参数 - $image、$points、$ num_points 和 $颜色。

  • $图像 - 使用 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(&#39;Content-type: image/png&#39;);
   imagepng($img);
   imagedestroy($img);
?>

输出

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

示例 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(&#39;Content-type: image/png&#39;);
   imagepng($img);
   imagedestroy($img);
?>

输出

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

php免费学习视频:立即学习
踏上前端学习之旅,开启通往精通之路!从前端基础到项目实战,循序渐进,一步一个脚印,迈向巅峰!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除