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.
<?php $image = imagecreate(100, 100); $blue = imagecolorallocate($image, 0, 0, 255); $red = imagecolorallocate($image, 255, 0, 0); $green = imagecolorallocate($image, 0, 255, 0); header('Content-type:image/jpeg'); imagejpeg($image); imagedestroy($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.
<?php $size=300; $image=imagecreatetruecolor($size,$size); //用白色背景加黑色边框画个方框 $back=imagecolorallocate($image,255,255,255); $border=imagecolorallocate($image,0,0,0); imagefilledrectangle($image,0,0,$size-1,$size-1,$back); imagerectangle($image,0,0,$size-1,$size-1,$border); $yellow_x=100; $yellow_y=75; $red_x=120; $red_y=165; $blue_x=187; $blue_y=125; $radius=150; //用alpha值分配一些颜色 $yellow=imagecolorallocatealpha($image,255,255,0,75); $red=imagecolorallocatealpha($image,255,0,0,75); $blue=imagecolorallocatealpha($image,0,0,255,75); //画3个交迭的圆 imagefilledellipse($image,$yellow_x,$yellow_y,$radius,$radius,$yellow); imagefilledellipse($image,$red_x,$red_y,$radius,$radius,$red); imagefilledellipse($image,$blue_x,$blue_y,$radius,$radius,$blue); //不要忘记输出正确的header! header('Content-type:image/png'); //最后输出结果 imagepng($image); imagedestroy($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!