Answer the question
In order to leave comments, you need to log in
How to efficiently compress images in nodejs?
Good day.
The task is to compress images and then save them.
Googling, I found the best option - the sharp package .
But here a problem arose.
My code:
sharp()
.withMetadata()
.jpeg({ quality: 50, force: false })
.png({ compressionLevel: 9, force: false })
.webp({ quality: 50, force: false })
.toBuffer();
Answer the question
In order to leave comments, you need to log in
Sir, you are doing something crazy.
First, the image is converted to zip with 50% quality, then to png using lossless compression, and finally converted to webp again with 50% quality.
After all this, I'm more than sure that you save everything with the original file extension, in other words, convert the resulting webp to zipeg / png. And given that PNG is incompressible, how do you expect to get a PNG with a much smaller volume?
sharp()
.withMetadata()
.jpeg({ quality: 50 })
.toFile(path.join(__dirname, `some/path/${filename}.jpg`));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question