imagefilledrectangle
绘制一个填充的矩形
适用 PHP 版本:PHP 4.3.0 及以上版本
imagefilledrectangle 函数用于在图像上绘制一个填充的矩形。
imagefilledrectangle(resource $image, int $x1, int $y1, int $x2, int $y2, int $color): bool
该函数成功时返回 true,失败时返回 false。
以下是一个使用 imagefilledrectangle 函数的示例:
<?php // 创建一个空白图像 $image = imagecreate(200, 200); <p>// 分配颜色<br> $bg_color = imagecolorallocate($image, 255, 255, 255); // 背景颜色为白色<br> $rect_color = imagecolorallocate($image, 255, 0, 0); // 矩形颜色为红色</p> <p>// 在图像上绘制填充矩形<br> imagefilledrectangle($image, 50, 50, 150, 150, $rect_color);</p> <p>// 输出图像<br> header('Content-Type: image/png');<br> imagepng($image);</p> <p>// 销毁图像资源<br> imagedestroy($image);<br> ?><br>
这段代码首先创建一个 200x200 像素的空白图像,并为其分配白色背景和红色矩形的填充颜色。接着,通过调用 imagefilledrectangle 函数,在图像上绘制了一个位于坐标 (50, 50) 到 (150, 150) 之间的红色填充矩形。最后,图像通过 imagepng 函数输出为 PNG 格式,并销毁图像资源。