imagefilledpolygon
绘制填充多边形
PHP 4及以上版本
该函数用于在图像上绘制一个填充的多边形。你可以通过提供一个包含顶点坐标的数组来定义多边形的形状。
imagefilledpolygon(resource $image, array $points, int $num_points, int $color): bool
返回布尔值。成功时返回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. 最后,输出图像并销毁资源。