image-resize-to-bytes
v1.0.1
Published
resize image to desired file size
Downloads
7
Readme
image-resize-to-bytes
Down-sizing bigger images into the best dimensions that meets the specified bytes limits, this will try to produce a resize that has least lost in image quality, e.g. when uploading the image via APIs that has file size restriction.
It resize images using sharp which supports for all JPEG, PNG, WebP, TIFF, GIF and SVG images.
Installation
$ npm install --save image-resize-to-bytes
Usage
const resizeToBytes = require('image-resize-to-bytes');
const axios = require('axios');
axios
.get(
'https://assets.vogue.com/photos/5ce2feec0f6084cdac2394bf/master/pass/00016-BURBERRY-RESORT-2020-LONDON.jpg',
{ responseType: 'arraybuffer' }
)
.then(({ data: image }) => {
console.log(image.byteLength); // 1.05m
// resize to ~200kb
resizeToBytes(image, 200000).then(smallImage => {
console.log(smallImage.byteLength); // 190kb
});
});
License
MIT © Garry Yao