Home  >  Article  >  Backend Development  >  PHP draws a rectangle and fills it

PHP draws a rectangle and fills it

WBOY
WBOYforward
2024-03-21 10:10:32992browse

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:

  • Image Resources
  • Start X coordinate
  • Start Y coordinate
  • The width of the rectangle
  • Height of rectangle
  • Fill color

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

  • When using the GD library, you need to ensure that the GD extension is enabled.
  • Before using the imagefilledrectangle() function, an image resource must be created.
  • The format of the fill color depends on the version of the GD library. For GD library version 1, the format is an RGB value; for GD library version 2, the format is an RGBA value.
  • The starting coordinates of the rectangle specify the coordinates of its upper left corner.

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!

Statement:
This article is reproduced at:lsjlt.com. If there is any infringement, please contact admin@php.cn delete
Previous article:PHP draws a single pixelNext article:PHP draws a single pixel