antonreshetov / image-optimizer

A tool for optimizing images and vector graphics.
MIT License
888 stars 63 forks source link

[Feature Request] WebP Support #23

Closed Christopher-Hayes closed 2 years ago

Christopher-Hayes commented 2 years ago

WebP support

Feature: A config option to also convert images to WebP or convert specific formats to WebP (like PNG)

WebP now has support on all major browser: https://caniuse.com/webp

Not much longer will websites require PNG/JPG fallback for WebP used on the web.


As far as what file formats should be converted to WebP, PNG seems to be the main one:

Converting a JPG -> WebP will retain JPG's sub-par quality at a slightly smaller file size, but converting PNG -> WebP will significantly reduce file size without sacrificing PNG's great image quality. So, maybe an option to convert PNGs -> WebP would make the most sense?


Libraries for converting to WebP

As far as what library could be used, Google has libwebp which looks a little difficult to integrate into an Electron application. Alternatively, there's cwebp which is Google's CLI-friendly version that uses libwebp, which node could run if cwebp is installed. But, getting the user to install it or shipping cwebp with Electron is the tricky part.

A somewhat maintained JS wrapper for cwebp (library has a cwebp binary in it): https://github.com/imagemin/cwebp-bin

An example of Apache's "Echarts" repo using the cwebp-bin in practice: https://github.com/apache/echarts-examples/blob/00e79accd3f1ac638774ae56b84e845d616a3124/tool/build-example.js#L91

A library that supports converting a number of different formats, including webp: https://github.com/Yuriy-Svetlov/compress-images

Christopher-Hayes commented 2 years ago

Created a PR that uses the cwebp-bin library to achieve this.