PNG优化工具
不久前,我使用了一个名为(我认为)“smush it”的 PNG 优化服务。你给它提供了一个网络链接,它返回了所有 PNG 图像的 zip 文件,其文件大小很好,很好,粉碎了......
我想实现一个类似的优化功能作为我网站图像上传过程的一部分;有谁知道我可以利用一个预先存在的库(最好是 PHP 或 Python)吗?简短的谷歌向我指出了几种命令行样式的工具,但如果可能的话,我宁愿不走这条路。
A while back I used a PNG optimisation service called (I think) "smush it". You fed it a weblink and it returned a zip of all the PNG images with their filesizes nicely, well, smushed...
I want to implement a similar optimisation feature as part of my website's image upload process; does anyone know of a pre-existing library (PHP or Python preferably) that I can tap into for this? A brief Google has pointed me towards several command line style tools, but I'd rather not go down that route if possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 PHP 执行此命令行工具
Execute with PHP this command line tools
只要你的 PHP 是用 GD2 支持编译的(现在很常见):
这将以 GD2 理解的任何图像格式(不仅仅是 PNG)读取,并输出 PNG gzipped 作为最大压缩级别,而不会牺牲质量。
不过,今天它的用处可能比几年前要小。大多数图像编辑器已经这样做了,因为 gzip 的 CPU 消耗不像以前那么高。
As long as your PHP is compiled with GD2 support (quite common nowadays):
This will read in any image format GD2 understands (not just PNG) and output a PNG gzipped as the maximum compression level without sacrificing quality.
It might be of less use today than years ago though; most image editors already do this, since gzipping doesn't cost as much CPU-wise as it used to.
你听说过 PNGCrush 吗?您可以查看源代码(SourceForge 上的 PNG 和 MNG 工具的一部分),并转录或包装它在Python中。
Have you heard of PNGCrush? You could check out the source, part of PNG and MNG Tools at SourceForge, and transcribe or wrap it in Python.
我质疑丢弃其他块(例如 gAMA 和 iCCP)是否明智,但如果这就是您想要做的,那么使用它相当容易 PyPNG 删除块:
ritic_chunks 函数本质上是过滤除关键 PNG 块以外的所有块(关键块的 4 个字母类型以大写字母开头)。
I would question the wisdom of throwing away other chunks (like gAMA and iCCP), but if that's what you want to do it's fairly easy to use PyPNG to remove chunks:
the critical_chunks function is essentially filtering out all but the critical PNG chunks (the 4 letter type for a critical chunk starts with an uppercase letter).