Wh1*_*Ck5 12
要将任何PNG图像转换为8位PNG,请使用此功能,我刚创建
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);
}
Run Code Online (Sandbox Code Playgroud)
我建议在运行此代码之前确保$sourcePath存在且$destPath可写.也许此功能不适用于某些透明图像.
convertPNGto8bitPNG ('pfc.png', 'pfc8bit.png');
Run Code Online (Sandbox Code Playgroud)
(来源:pfc.png)原始的PNG图像

(目的地:pfc8bit.png)CONVERTED PNG IMAGE(8位)

希望有人觉得这很有帮助.
而不是GD库我强烈建议使用pngquant 1.5+命令行使用exec()或popen()功能.
GD库具有非常差的调色板生成代码.
与其他答案中的图像相同,文件大小与GD库相同,但pngquant仅使用100种颜色(甚至不是256种)进行转换:

pngquant非常支持alpha透明度.
| 归档时间: |
|
| 查看次数: |
8115 次 |
| 最近记录: |