Sharp: Quality for JPEG not work

Created on 30 Nov 2018  路  2Comments  路  Source: lovell/sharp

Hello.

                Sharp('dog.jpg')
                    .resize({width: 500, withoutEnlargement: true})
                    .jpeg({quality: 10, progressive: true})
                    .webp({quality: 50, lossless: true})
                    .png({quality: 50, compressionLevel: 8})
                    .toFile(path.join(filepath, fieldname, size, filename),
                        function (err) {
                            console.log(err);
                        });

Original: 44 KB

dog

Quality 10%: 270 KB

1543543222958-dog

How to compress JPEG image?

question

Most helpful comment

@lovell Thank you very much.

                Sharp('dog.jpg')
                    .resize({width: 500, withoutEnlargement: true})
                    .jpeg({quality: 80, progressive: true, force: false})
                    .webp({quality: 80, lossless: true, force: false})
                    .png({quality: 80, compressionLevel: 8, force: false})
                    .toFile(path.join(filepath, fieldname, size, filename),
                        function (err) {
                            console.log(err);
                        });

Quality 80%: 18 KB

1543584532539-dog

All 2 comments

Hello, the output you've posted here is PNG rather than JPEG, hence the larger size. PNG is lossless and does not have a quality setting.

Did you see the force option on the jpeg(), webp() and png() functions? You'll probably want to set it to force: false for all three if you're specifying all three.

@lovell Thank you very much.

                Sharp('dog.jpg')
                    .resize({width: 500, withoutEnlargement: true})
                    .jpeg({quality: 80, progressive: true, force: false})
                    .webp({quality: 80, lossless: true, force: false})
                    .png({quality: 80, compressionLevel: 8, force: false})
                    .toFile(path.join(filepath, fieldname, size, filename),
                        function (err) {
                            console.log(err);
                        });

Quality 80%: 18 KB

1543584532539-dog

Was this page helpful?
0 / 5 - 0 ratings

Related issues

emmtte picture emmtte  路  3Comments

janaz picture janaz  路  3Comments

AVVS picture AVVS  路  3Comments

kachurovskiy picture kachurovskiy  路  3Comments

jaekunchoi picture jaekunchoi  路  3Comments