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.
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):
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.
The running results are as shown below:
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)
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.
The running results are as shown below:
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!