Skip to content

Instantly share code, notes, and snippets.

@victorwpbastos
Created May 29, 2020 16:22
Show Gist options
  • Save victorwpbastos/ad1e9144f18692aab91e93635462b669 to your computer and use it in GitHub Desktop.
Save victorwpbastos/ad1e9144f18692aab91e93635462b669 to your computer and use it in GitHub Desktop.
Reduce image size with Sharp
const sharp = require('sharp');
const fs = require('fs');
const input = sharp('./image.jpg');
input
.flatten(
{
r: 255,
g: 255,
b: 255
}
)
.jpeg(
{
chromaSubsampling: '4:4:4',
quality: 100 * 0.75,
progressive: true,
force: true
}
)
.withMetadata()
.toBuffer()
.then(
output => {
console.log(output);
fs.writeFileSync('output.jpg', output);
}
)
.catch(
err => {
console.log(err);
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment