array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'zh', ), 'this' => array ( 0 => 'function.imagesavealpha.php', 1 => 'imagesavealpha', ), 'up' => array ( 0 => 'ref.image.php', 1 => 'GD 和图像处理 函数', ), 'prev' => array ( 0 => 'function.imagerotate.php', 1 => 'imagerotate', ), 'next' => array ( 0 => 'function.imagescale.php', 1 => 'imagescale', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'zh', '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 — 保存图像时是否保留完整的 alpha 通道信息
imagesavealpha() 设置标记,确定在保存图像时是否保存完整的 alpha 通道信息(与单一透明色相反)。
这仅支持支持完整 alpha 通道信息的图像格式,即 PNG
、WebP
和 AVIF
。
注意: 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.
必须禁用 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);
?>