imagerectangle()函数是PHP中的一个内置函数,用于绘制矩形。
用法:
bool imagerectangle( $image, $x1, $y1, $x2, $y2, $color )
参数:该函数接受上述和以下所述的六个参数:
$image:它由图像创建功能之一(例如imagecreatetruecolor())返回。它用于创建图像的尺寸。
$x1:此参数用于设置左上角的x坐标。
$y1:此参数用于设置左上方的y坐标。
$x2:此参数用于设置右下角的x坐标。
$y2:此参数用于设置右下角的y坐标。
$color:使用imagecolorallocate()创建的颜色标识符。
返回值:如果成功,此函数返回True;如果失败,则返回False。
以下示例程序旨在说明PHP中的imagerectangle()函数:
示例1:
// Create an image of given size
$image = imagecreatetruecolor(400, 400);
// Create an image color
$green = imagecolorallocate($image, 0, 153, 0);
$white = imagecolorallocate($image, 255, 255, 255);
// Draw the rectangle
imagerectangle($image, 50, 50, 350, 350, $green);
imagerectangle($image, 100, 100, 300, 300, $white);
// Output and free from memory
header('Content-Type: image/jpeg');
imagejpeg($image);
imagedestroy($image);
?>
输出:
示例2:
// Create an image of given size
$image = imagecreatetruecolor(400, 400);
// Set the background color of image
$background_color = imagecolorallocate($image, 255, 255, 255);
// Fill background with above selected color
imagefill($image, 0, 0, $background_color);
// Create an image color
$green = imagecolorallocate($image, 0, 153, 0);
$white = imagecolorallocate($image, 155, 53, 32);
// Draw the rectangle
imagerectangle($image, 50, 50, 350, 350, $green);
imagerectangle($image, 100, 100, 300, 300, $white);
// Output and free from memory
header('Content-Type: image/jpeg');
imagejpeg($image);
imagedestroy($image);
?>
输出: