@mora/tinypng
基于 TinyPNG 封装的一个支持nodejs、命令行 和webpack的图片压缩工具
支持压缩的图片格式
png
jpg/jpeg
svg (TinyPNG 不支持压缩 svg,所以在 @mora/tinypng 中使用了 svgo 来压缩)
背景
当前市面上有很多图片压缩工具,但TinyPNG是我目前所遇到的在保证压缩质量的前提下压缩效率最好的一款压缩工具。官方解释说:
TinyPNG uses smart lossy compression techniques to reduce the file size of your PNG files. By selectively decreasing the number of colors in the image, fewer bytes are required to store the data. The effect is nearly invisible but it makes a very large difference in file size!
不过 TinyPNG 并没有开源,所以它的具体压缩算法不得而知,我们只能通过调用它提供的 API 来压缩图片,而每个月每个帐户只能调用 500 次它的 API,如果想调用更多次数,你需要付费升级你的帐户。
所以,为了可以得到 TinyPNG 高效率压缩的图片,同时又希望无限制的免费调用它的 API,就有了此工具
原理
前提:需要用户手动去TinyPNG官网注册(注册很简单,只要邮箱就行了)多个帐户,获取到每个帐户的 token,提供给此程序
程序会循环使用用户提供的 token 去调用官方 API&#