imagepolygon
绘制多边形
PHP 4.3.0 及以上版本
imagepolygon() 函数用于在图像上绘制一个多边形。你需要为多边形的各个顶点提供一系列的坐标,然后它会根据这些坐标连接点并形成一个封闭的多边形。
bool imagepolygon(resource $image, array $points, int $num_points, int $color);
返回布尔值:如果成功,返回 true;如果失败,返回 false。
下面是一个简单的示例,演示了如何使用 imagepolygon 函数在图像上绘制一个三角形。
此示例首先创建了一个空白图像,然后定义了一个三角形的顶点数组。最后,使用 imagepolygon() 函数绘制该三角形。
<?php
// 创建一个空白图像
$image = imagecreatetruecolor(200, 200);
// 分配颜色
$white = imagecolorallocate($image, 255, 255, 255);
$red = imagecolorallocate($image, 255, 0, 0);
// 定义三角形的顶点
$points = array(50, 50, 150, 50, 100, 150);
// 绘制三角形
imagepolygon($image, $points, 3, $red);
// 输出图像
header("Content-Type: image/png");
imagepng($image);
// 销毁图像资源
imagedestroy($image);
?>
这个代码片段会生成一个包含红色三角形的 PNG 图像。