Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nodejs Sharp webp lossless compression

Tags:

node.js

sharp

I'm developing a similar service of Imgix and I'm using Sharp.

But the webp lossless compression Imgix get better results than Sharp. The same image with the same width and height in Imgix have 453 KB and with Sharp 1.3 MB.

Some recommendation to increase compression without losing quality?

The code that I'm using:

https.get(url, function (response) {
    let transform = sharp().toFormat('webp').resize(width, height);
    return response.pipe(transform).webp({lossless:true}).pipe(res);
});
like image 261
NAG Avatar asked Jul 21 '26 15:07

NAG


2 Answers

I see that document have some fileds in options: quality, alphaQuality, nearLossless, force. Can you try it? And compare with IMGIX

  • quality: Number quality, integer 1-100 (optional, default 80)
  • alphaQuality: Number quality of alpha layer, integer 0-100 (optional, default 100)
  • lossless: Boolean use lossless compression mode (optional, default false)
  • nearLossless: Boolean use near_lossless compression mode (optional, default false)
  • force: Boolean force WebP output, otherwise attempt to use input format (optional, default true)
https.get(url, function (response) {
    let transform = sharp().toFormat('webp').resize(width, height);
    return response.pipe(transform).webp({lossless:true, quality: 60, alphaQuality: 80, force: false}).pipe(res);
});
like image 56
Chuong Tran Avatar answered Jul 23 '26 08:07

Chuong Tran


Documentation on how to use Sharp's webp output options is non-existent AFAICT, but according to this comment the options nearLossless and quality should be used together, while the lossless:true option is equivalent to nearLossless:true,quality:100

In my experience, nearLossless:true,quality:50 will cut the file size to less than half of lossless:true, while retaining most of the quality.

like image 24
Yarin Avatar answered Jul 23 '26 08:07

Yarin