php imagerectangle,PHP imagerectangle()用法及代码示例

本文详细介绍了PHP中的imagerectangle()函数,用于在图像上绘制矩形。函数接受六个参数,包括图像资源、矩形的坐标和颜色。通过示例代码展示了如何创建和填充不同颜色的矩形,并输出JPEG图像。了解此函数有助于在PHP中进行图像处理。
摘要由CSDN通过智能技术生成

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);

?>

输出:

12ce0a899c0e5ae9838b2a894aa69db7.png

示例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);

?>

输出:

e30c67231adcb06e6c281b12339372d4.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值