Skip to content

garryyao/image-resize-to-bytes

Repository files navigation

image-resize-to-bytes NPM version Build Status Dependency Status

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

About

Down-sizing image to certain bytes

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published