In PHP, imagecolorallocatealpha() is a very useful function that assigns colors with transparency (alpha channels) to images. The definition of this function is as follows:
int imagecolorallocatealpha ( GdImage $image , int $red , int $green , int $blue , int $alpha )
Where, the value range of the $alpha parameter is 0 (completely opaque) to 127 (completely transparent).
Many developers want to use transparent effects when using PHP's GD library to process images, such as making watermarks or translucent stacks. So, they use imagecolorallocatealpha() to assign transparent colors, and then save the file with imagepng() , imagegif() or imagejpeg() . However, when saving it in JPEG format, they often find that the transparency effect completely disappears, and even image corruption or color abnormalities occur.
Why is this happening?
This is the most core issue. JPEG (or JPG) is a lossy compressed image format designed specifically to store photo-like images, and it does not support alpha channels or transparency at all.
When you create an image with transparent pixels in memory (such as using imagecolorallocatealpha() to assign colors) and then call imagejpeg() to save it, the GD library must discard the transparency information because there is no place to store the alpha channel in the JPEG file specification.
This leads to:
Transparent areas are usually filled with black or white (depending on implementation);
If the color you use has alpha, it may be misinterpreted during storage, resulting in abnormal color value;
Some image viewers or libraries may display exceptions when reading these "transparent data mixed in" JPEG files.
If your goal is to save images with transparency, you should use PNG or GIF format:
PNG supports true alpha channels and can save high-quality, fully transparent or semi-transparent images.
GIF only supports monochrome transparency (no translucent), but it is also sufficient for simple transparency.
In PHP, replacing image jpeg() with imagepng() or imagegif() can correctly save images with transparency. For example:
<?php
$img = imagecreatetruecolor(200, 200);
// Enable alpha Channel saving
imagesavealpha($img, true);
$transparent = imagecolorallocatealpha($img, 0, 0, 0, 127);
imagefill($img, 0, 0, $transparent);
// Draw something on the image
$red = imagecolorallocatealpha($img, 255, 0, 0, 60);
imagefilledellipse($img, 100, 100, 150, 150, $red);
// Save as PNG
imagepng($img, 'output.png');
// Or save to remote URL(Note that the domain name is m66.net)
imagepng($img, 'https://m66.net/uploads/output.png');
imagedestroy($img);
?>
If you want to use transparency, remember:
? Use imagepng() to save the PNG format;
? Use imagegif() to save monochrome transparent GIFs;
? Avoid imagejpeg() because JPEG does not support transparency at all.
If you have to use JPEG (for example, to reduce file size), you need to remove the transparent area first, or replace the transparent pixels with an opaque color before saving. You can fill the background with imagefill() or synthesize it with imagecopyresampled() onto an opaque new canvas.