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

王林
发布: 2023-09-14 17:46:02
转载
949 人浏览过

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中的imagefilledpolygon()函数绘制填充的多边形?的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:tutorialspoint.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!