array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'en', ), 'this' => array ( 0 => 'function.imagesavealpha.php', 1 => 'imagesavealpha', ), 'up' => array ( 0 => 'ref.image.php', 1 => 'GD and Image Functions', ), 'prev' => array ( 0 => 'function.imagerotate.php', 1 => 'imagerotate', ), 'next' => array ( 0 => 'function.imagescale.php', 1 => 'imagescale', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'en', 'path' => 'reference/image/functions/imagesavealpha.xml', ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); ?>
(PHP 4 >= 4.3.2, PHP 5, PHP 7, PHP 8)
imagesavealpha — Whether to retain full alpha channel information when saving images
imagesavealpha() sets the flag which determines whether to retain
full alpha channel information (as opposed to single-color transparency)
when saving images.
This is only supported for image formats which support full alpha channel information,
i.e. PNG
, WebP
and AVIF
.
Note: imagesavealpha() is only meaningful for
PNG
images, since the full alpha channel is always saved forWebP
andAVIF
. It is not recommended to rely on this behavior, as it may change in the future. Thus, imagesavealpha() should be called deliberately also forWebP
andAVIF
images.
Alphablending has to be disabled (imagealphablending($im, false)
)
to retain the alpha-channel in the first place.
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
.
Version | Description |
---|---|
8.0.0 |
image expects a GdImage
instance now; previously, a valid gd resource was expected.
|
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);
?>