Home >Backend Development >PHP Tutorial >PHP draws a rectangle and fills it
php Xiaobian Youzi teaches you how to draw a rectangle and fill it with color using PHP language. With simple code, you can easily implement this functionality. Below we will introduce in detail how to use PHP to draw a rectangle and fill the rectangle with color. Let’s explore together!
PHP Draw a rectangle and fill it
Introduction
In php, you can use the GD library to draw and manipulate images, including drawing rectangles and filling them. The GD library provides a series of functions through which various graphics processing tasks can be implemented.
Draw a rectangle
To draw a rectangle, you can use the imagefilledrectangle() function. This function requires the following parameters:
For example, the following code draws a rectangle filled with red:
$image = imagecreate(100, 100); $color = imagecolorallocate($image, 255, 0, 0); imagefilledrectangle($image, 20, 20, 80, 80, $color);
Filled rectangle
To fill a rectangle, you can use the imagefilledrectangle() function. This function uses the same parameters as the imagefilled() function, but it also contains an additional parameter to specify the fill color.
For example, the following code fills a rectangle with a gradient:
$image = imagecreate(100, 100); $start_color = imagecolorallocate($image, 255, 255, 0); $end_color = imagecolorallocate($image, 0, 0, 255); imagefilledrectangle($image, 20, 20, 80, 80, imagecolorgradient($image, 20, 20, 80, 80, $start_color, $end_color));
Precautions
The above is the detailed content of PHP draws a rectangle and fills it. For more information, please follow other related articles on the PHP Chinese website!