要将任何 PNG 图像转换为 8 位 PNG,请使用我刚刚创建的此函数
函数convertPNGto8bitPNG()
function convertPNGto8bitPNG ($sourcePath, $destPath) {
$srcimage = imagecreatefrompng($sourcePath);
list($width, $height) = getimagesize($sourcePath);
$img = imagecreatetruecolor($width, $height);
$bga = imagecolorallocatealpha($img, 0, 0, 0, 127);
imagecolortransparent($img, $bga);
imagefill($img, 0, 0, $bga);
imagecopy($img, $srcimage, 0, 0, 0, 0, $width, $height);
imagetruecolortopalette($img, false, 255);
imagesavealpha($img, true);
imagepng($img, $destPath);
imagedestroy($img);
}
参数
-
$sourcePath - 源 PNG 文件的路径
-
$destPath - 目标 PNG 文件的路径
注意
我建议在运行此代码之前确保$sourcePath 存在并且$destPath 是可写的。可能此功能不适用于某些透明图像。
用法
convertPNGto8bitPNG ('pfc.png', 'pfc8bit.png');
示例(原始 -> 8 位)
(来源:pfc.png)原始PNG图像
(目标:pfc8bit.png)转换后的 PNG 图像(8 位)
希望有人觉得这很有帮助。