函数名:imagerectangle()
适用版本:PHP 4, PHP 5, PHP 7
用法:imagerectangle() 函数用于在图像资源中绘制一个矩形。它可以用于创建边框、绘制形状等。
语法:bool imagerectangle(resource $image, int $x1, int $y1, int $x2, int $y2, int $color)
参数:
- $image:表示图像资源,通过imagecreate()函数创建。
- $x1、$y1:表示矩形左上角顶点的坐标。
- $x2、$y2:表示矩形右下角顶点的坐标。
- $color:表示矩形的颜色,可以是一个整数表示的颜色值,或通过imagecolorallocate()函数创建的颜色。
返回值:成功时返回true,失败时返回false。
示例:
// 创建一个空白图像资源
$image = imagecreate(400, 300);
// 创建一个颜色
$color = imagecolorallocate($image, 255, 0, 0); // 红色
// 绘制一个矩形
imagerectangle($image, 50, 50, 200, 150, $color);
// 在浏览器中显示图像
header('Content-type: image/png');
imagepng($image);
// 销毁图像资源
imagedestroy($image);
以上示例创建了一个大小为400x300的空白图像资源,然后使用imagecolorallocate()函数创建了一个红色的颜色。接下来,使用imagerectangle()函数在图像资源中绘制了一个矩形,左上角顶点坐标为(50, 50),右下角顶点坐标为(200, 150),并使用之前创建的红色进行填充。最后,通过header()函数和imagepng()函数将图像输出到浏览器,并使用imagedestroy()函数销毁图像资源。