How to set canvas color in php

青灯夜游
Release: 2023-03-11 17:38:02
Original
2690 people have browsed it

How to set the canvas color: 1. Use the "imagecolorallocate(image,red,green,blue)" statement; 2. Use the "imagecolorallocatealpha(image,red,green,blue,alpha)" statement.

How to set canvas color in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

Method 1: imagecolorallocate() function

The imagecolorallocate() function can allocate colors to an image resource. If multiple colors need to be set in the image, just call this function multiple times. The syntax format of the function is as follows:

imagecolorallocate(resource $image, int $red, int $green, int $blue):
Copy after login

Among them, $image is the image resource to set the color, and the imagecolorallocate() function will return an identifier, which represents the color composed of the given RGB components; $ red, $green and $blue are the red, green and blue components of the required color respectively, and the value range is an integer from 0 to 255 or hexadecimal 0x00 to 0xFF.

Tip: If the image resource is created using the imagecreate() function, it will be filled with the background color by default when the imagecolorallocate() function is called for the first time.

[Example] Use the imagecolorallocate() function to set the color for the image.

Copy after login

The running results are as shown below:

How to set canvas color in php

Method 2: Use the imagecolorallocatealpha() function

imagecolorallocatealpha () The function has the same function as imagecolorallocate(), but there is an additional parameter alpha for setting the transparency. The syntax format of the function is as follows:

imagecolorallocatealpha(resource $image, int $red, int $green, int $blue, int $alpha)
Copy after login

Among them, $image is the image resource to set the color; $red , $green and $blue are the red, green, and blue components of the required color respectively. The value range is an integer from 0 to 255 or hexadecimal 0x00 to 0xFF; $alpha is used to set the transparency of the color. Values range from 0 to 127, with 0 indicating full opacity and 127 indicating full transparency.

[Example] Use the imagecolorallocatealpha() function to set the color for the image.

Copy after login

The running results are as shown below:

How to set canvas color in php

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to set canvas color in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
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
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!