imagerectangle
绘制一个矩形
PHP 4及以上版本
imagerectangle 函数用于在图像上绘制一个矩形。通过指定矩形的起始点、宽度和高度,可以在图像上绘制矩形框。该矩形框的颜色可以通过指定颜色资源来设置。
bool imagerectangle(resource $image, int $x1, int $y1, int $x2, int $y2, int $color)
该函数返回一个布尔值:如果成功则返回 true,如果失败则返回 false。
下面的代码展示了如何使用 imagerectangle 函数在图像上绘制一个矩形。
在示例中,我们首先创建一个图像资源,然后使用 imagerectangle() 函数在该图像上绘制一个红色矩形。最后,我们输出该图像到浏览器并销毁图像资源。
<?php // 创建一个空白图像 $image = imagecreatetruecolor(200, 200); <p>// 分配颜色(红色)<br> $red = imagecolorallocate($image, 255, 0, 0);</p> <p>// 绘制矩形,坐标为(50, 50)到(150, 150)<br> imagerectangle($image, 50, 50, 150, 150, $red);</p> <p>// 输出图像<br> header('Content-Type: image/png');<br> imagepng($image);</p> <p>// 销毁图像资源<br> imagedestroy($image);<br> ?><br>
此代码将在浏览器中显示一个红色矩形,位于图像的指定位置。