Home >Backend Development >PHP Tutorial >PHP draw a rectangle
php Xiaobian Xigua teaches you how to draw a rectangle using PHP language. With simple code, you can easily create a rectangular shape, showing off your programming skills. Following the guidance of this article, you can quickly master how to draw beautiful rectangles on web pages, making your web pages more lively and interesting. Start learning now!
Draw a rectangle using PHP
In php, you can use the GD library to draw rectangles. The GD library is a graphics library that provides a series of functions for processing images. Here are the steps on how to draw a rectangle on an image using the GD library:
Create image object
$image = imagecreate($width, $height);
$width
and $height
specify the width and height of the rectangle. Assign color
$color = imagecolorallocate($image, $red, $green, $blue);
$red
, $green
and $blue
specify the RGB value of the color. Draw a rectangle
imagerectangle($image, $x1, $y1, $x2, $y2, $color);
$x1
, $y1
and $x2
, $y2
specify the coordinates of the upper left and lower right corners of the rectangle. Output image
header("Content-Type: image/png"); imagepng($image); imagedestroy($image);
imagepng()
The function outputs the image into PNG format. Sample code
The following is a complete PHP script that uses the GD library to draw a rectangle on an image:
This creates a 200x100 pixel image and draws a red rectangle over the image.
other options
In addition to using the imagerectangle()
function, you can also use the following functions to draw a rectangle:
imagefilledrectangle()
: Draw a filled rectangle. imagefilledrectangle16()
: Draws a 16-bit filled rectangle. imagerectangle16()
: Draw a 16-bit rectangle. Precautions
imagesetthickness()
function to set the line thickness of the rectangle. imageline()
function to draw a line, which can be used to create rectangles and other shapes. The above is the detailed content of PHP draw a rectangle. For more information, please follow other related articles on the PHP Chinese website!