如何使用 PHP GD 库将 PNG 转换为 8 位 PNG
我想编写一个例程,以 PNG 图像路径作为参数,并将该图像转换为 8 位 PNG 图像。我需要为此使用 PHP GD 库。
I want to write a routine which takes PNG image path as parameter and convert that image into 8-bit PNG image. I need to use PHP GD library for this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要将任何 PNG 图像转换为 8 位 PNG,请使用此函数,我刚刚创建了
函数 ConvertPNGto8bitPNG ()
参数
注意
我建议在运行此代码之前确保
$sourcePath
存在并且$destPath
可写。也许此功能不适用于某些透明图像。使用
示例(原始 -> 8 位)
(来源:pfc.png) 原始 PNG 图像
(目标:pfc8bit.png)转换后的 PNG 图像(8 位)
希望有人觉得这有帮助。
To convert any PNG image to 8-bit PNG use this function, I've just created
function convertPNGto8bitPNG ()
Parameters
Note
I recommend to make sure that
$sourcePath
exists and$destPath
is writable before running this code. Maybe this function won't work with some transparent images.Usage
Example (original -> 8-bit)
(Source: pfc.png) ORIGINAL PNG IMAGE
(Destination: pfc8bit.png) CONVERTED PNG IMAGE (8-bit)
Hope someone finds this helpful.
我强烈建议使用
exec()
或使用 pngquant 1.5+ 命令行,而不是 GD 库popen() 函数。
GD 库的调色板生成代码质量非常差。
与其他答案中的图像相同,文件大小与 GD 库相同,但使用
pngquant
转换为仅 100 种颜色(甚至不是 256):pngquant 很好地支持 alpha 透明度。
Instead of GD library I strongly recommend using pngquant 1.5+ commandline using
exec()
orpopen()
functions.GD library has very poor-quality palette generation code.
Same image as in the other answer, same file size as GD library, but converted using
pngquant
to merely 100 colors (not even 256):pngquant supports alpha transparency very well.