(PHP 4 >= 4.3.2, PHP 5, PHP 7, PHP 8)
imagesavealpha — 保存图像时是否保留完整的 alpha 通道信息
imagesavealpha() 设置标记,确定在保存图像时是否保存完整的 alpha 通道信息(与单一透明色相反)。
这仅支持支持完整 alpha 通道信息的图像格式,即 PNG、WebP 和 AVIF。
注意: imagesavealpha() is only meaningful for
PNGimages, since the full alpha channel is always saved forWebPandAVIF. It is not recommended to rely on this behavior, as it may change in the future. Thus, imagesavealpha() should be called deliberately also forWebPandAVIFimages.
必须禁用 alpha 混合(imagealphablending($im, false)),以首先保留 alpha 通道。
示例 #1 基础 imagesavealpha() 用法
<?php
// 载入带 alpha 通道的 png 图像
$png = imagecreatefrompng('./alphachannel_example.png');
// 关闭 alpha 混合
imagealphablending($png, false);
// Do desired operations
//并设置 alpha 标志
imagesavealpha($png, true);
// 输出图像到浏览器
header('Content-Type: image/png');
imagepng($png);
imagedestroy($png);
?>