When using PHP's GD library for image processing, sometimes we need to set a transparent background for the image, such as making the background transparent when generating PNG images instead of filling in white or black. To achieve this effect, you can mainly use the two functions: imagecolorallocatealpha() and imagefill() .
This article will explain in detail how to use these two functions and give the complete sample code.
int imagecolorallocatealpha ( resource $image , int $red , int $green , int $blue , int $alpha )
This function is used to assign a color to the specified image and set transparency.
$red, $green, $blue : RGB color value (0-255)
$alpha : Transparency value (0 is completely opaque, 127 is completely transparent)
The returned value is a color identifier, which can be used in functions such as imagefill() .
bool imagefill ( resource $image , int $x , int $y , int $color )
This function is used to fill the image with the specified color.
$x, $y : Fill in the coordinates of the starting point
$color : The color identifier to use
Combined with imagecolorallocatealpha() , we can fill the entire canvas with transparent colors.
Below is a complete PHP code that demonstrates how to use the GD library to generate a 200x200 PNG image with a transparent background and draw a red circle in the middle.
<?php
// Create a 200x200 True color image
$width = 200;
$height = 200;
$image = imagecreatetruecolor($width, $height);
// Turn off color mixing mode,Enable Save alpha Channel information
imagealphablending($image, false);
imagesavealpha($image, true);
// Assign a completely transparent color
$transparent = imagecolorallocatealpha($image, 0, 0, 0, 127);
// Fill the entire canvas with transparent colors
imagefill($image, 0, 0, $transparent);
// Assign red(opaque)
$red = imagecolorallocate($image, 255, 0, 0);
// Draw a red circle in the middle
imagefilledellipse($image, $width / 2, $height / 2, 100, 100, $red);
// The output image is PNG
header('Content-Type: image/png');
imagepng($image);
// Free memory
imagedestroy($image);
?>
1?? Create image resources <br> We use imagecreatetruecolor() to create a true color canvas.
2?? Enable transparent background
GD does not save alpha information by default, and requires:
imagealphablending($image, false);
imagesavealpha($image, true);
3?? Allocate and fill with transparent colors <br> Use imagecolorallocatealpha() to create a completely transparent color (alpha=127), and then use imagefill() to fill the entire canvas.
4?? Draw content <br> Here we have drawn a red circle, which you can change to text, lines, pictures, etc.
5?? Output image <br> Use header('Content-Type: image/png') to set the output type, then use imagepng() to output to the browser, or pass in file name to save to disk, for example:
imagepng($image, '/path/to/save/output.png');
? Save to file <br> If you want to save the image to the server, you can do this:
imagepng($image, 'https://m66.net/images/output.png');
? Switch to GIF
Although GIF also supports transparent backgrounds, the transparent implementation method is different. You can use imagecolortransparent() .
? Pay attention to permissions <br> When saving to the server directory, make sure that the PHP process has write permissions.