imagesavealpha
Whether to retain full alpha channel information when saving images
Description
bool imagesavealpha(GdImage $image
, bool $enable
)
Note:
imagesavealpha is only meaningful for PNG
images, since the full alpha channel is always saved for WebP
and AVIF
. It is not recommended to rely on this behavior,
as it may change in the future. Thus, imagesavealpha
should be called deliberately also for WebP
and
AVIF
images.
Alphablending has to be disabled (imagealphablending($im, false)
)
to retain the alpha-channel in the first place.
Parameters
-
image
-
A GdImage object, returned by one of the image creation functions,
such as imagecreatetruecolor.
-
enable
-
Whether to save the alpha channel or not. Defaults to false
.
Return Values
Returns true
on success or false
on failure.
Examples
Example #1 Basic imagesavealpha Usage
<?php
// Load a png image with alpha channel
$png = imagecreatefrompng('./alphachannel_example.png');
// Turn off alpha blending
imagealphablending($png, false);
// Do desired operations
// Set alpha flag
imagesavealpha($png, true);
// Output image to browser
header('Content-Type: image/png');
imagepng($png);
imagedestroy($png);
?>