Optimize images from a Node.js script
By Flavio Copes
Learn how to optimize images from a Node.js script with sharp, converting a PNG to lossless webp and resizing or rotating it in just a few lines.
~~~
One tool I’ve been using to optimize images is sharp.
import sharp from 'sharp'
Here’s how I used it to convert images to webp:
await sharp('image.png')
.webp({ lossless: true })
.toFile('image.webp')
It can do a lot more image processing stuff like rotate resize etc.
If you just need to resize or compress a couple of images without writing a script, I made a browser-based image resizer tool — nothing gets uploaded anywhere.
~~~
Related posts about node: