PHP draws a rectangle and fills it

WBOY
Release: 2024-03-21 10:14:02
forward
927 people have browsed 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:

  • 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);
Copy after login

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));
Copy after login

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!

source:lsjlt.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!