imagesavealpha

(PHP 4 >= 4.3.2, PHP 5, PHP 7, PHP 8)

imagesavealpha保存图像时是否保留完整的 alpha 通道信息

说明

imagesavealpha(GdImage $image, bool $enable): bool

imagesavealpha() 设置标记,确定在保存图像时是否保存完整的 alpha 通道信息(与单一透明色相反)。 这仅支持支持完整 alpha 通道信息的图像格式,即 PNGWebPAVIF

注意: 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.

必须禁用 alpha 混合(imagealphablending($im, false)),以首先保留 alpha 通道。

参数

image

由图象创建函数(例如imagecreatetruecolor())返回的 GdImage 对象。

enable

是否保存透明(alpha)通道。默认 false

返回值

成功时返回 true, 或者在失败时返回 false

更新日志

版本 说明
8.0.0 image 现在需要 GdImage 实例;之前需要有效的 gd resource

范例

示例 #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);
?>

参见

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top