当前位置: 首页> 函数类别大全> imagefilledpolygon

imagefilledpolygon

绘制填充多边形
名称:imagefilledpolygon
分类:图像处理GD
所属语言:php
一句话介绍:绘制填充多边形

imagefilledpolygon函数

适用PHP版本

PHP 4及以上版本

函数说明

该函数用于在图像上绘制一个填充的多边形。你可以通过提供一个包含顶点坐标的数组来定义多边形的形状。

函数语法

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

参数

  • $image: 图像资源,通过函数如imagecreate()创建的图像资源。
  • $points: 一个数组,包含多边形所有顶点的坐标。数组的结构是每一对元素代表一个点的x和y坐标。
  • $num_points: 多边形的顶点数,即$points数组中坐标的对数。
  • $color: 要填充多边形的颜色。可以使用imagecolorallocate()函数获取一个颜色。

返回值

返回布尔值。成功时返回true,失败时返回false。

示例

以下是一个简单的示例,展示了如何使用imagefilledpolygon函数来在图像上绘制一个填充的三角形:

示例代码

$image = imagecreatetruecolor(200, 200);  // 创建一个200x200的图像
$white = imagecolorallocate($image, 255, 255, 255);  // 定义白色
$blue = imagecolorallocate($image, 0, 0, 255);  // 定义蓝色
<p>// 使用白色填充背景<br>
imagefill($image, 0, 0, $white);</p>
<p>// 定义三角形的顶点坐标<br>
$points = array(<br>
100, 50,   // 顶点1 (x1, y1)<br>
150, 150, // 顶点2 (x2, y2)<br>
50, 150   // 顶点3 (x3, y3)<br>
);</p>
<p>// 绘制一个填充的蓝色三角形<br>
imagefilledpolygon($image, $points, 3, $blue);</p>
<p>// 输出图像并释放资源<br>
header('Content-Type: image/png');<br>
imagepng($image);<br>
imagedestroy($image);<br>

示例代码的说明

1. 使用imagecreatetruecolor()创建了一个200x200像素的图像资源。

2. 使用imagecolorallocate()定义了白色和蓝色。

3. 使用imagefill()将背景填充为白色。

4. 定义了一个数组$points,包含三角形的三个顶点坐标。

5. 调用imagefilledpolygon()绘制一个填充的蓝色三角形。

6. 最后,输出图像并销毁资源。

同类函数
热门文章