Détail du package

request-image-size

FdezRomero10.6kMIT2.2.0

Detect image dimensions via request.

image, size, dimension, resolution

readme

request-image-size

NPM

Detects image dimensions via request instead of Node.js native http/https, allowing for options and following redirects by default. It reduces network traffic by aborting requests as soon as image-size is able to obtain the image size.

Since version 2.0.0 it returns an ES6 native Promise that resolves with the size object or rejects with an Error. Requires Node.js v4+.

If you prefer using a callback, please use version 1.3.0 instead (docs)

Supports all the image formats supported by image-size:

  • BMP
  • CUR
  • GIF
  • ICO
  • JPEG
  • PNG
  • PSD
  • TIFF
  • WebP
  • SVG
  • DDS

Dependencies

Basic usage

const requestImageSize = require('request-image-size');

requestImageSize('http://nodejs.org/images/logo.png')
.then(size => console.log(size))
.catch(err => console.error(err));

Result:

{ width: 245, height: 66, type: 'png', downloaded: 856 }

Advanced usage

Specifying a request options object (docs):

const requestImageSize = require('request-image-size');

const options = {
  url: 'http://nodejs.org/images/logo.png',
  headers: {
    'User-Agent': 'request-image-size'
  }
};

requestImageSize(options)
.then(size => console.log(size))
.catch(err => console.error(err));

License

Copyright (c) 2017 Rodrigo Fernández Romero

Licensed under the MIT license.

Based on http-image-size from Johannes J. Schmidt.

changelog

2.1.0 (2017-10-05)

  • Minor change: Better error handling by returning an error of type HttpError if the response status code is 4xx/5xx, instead of the generic TypeError: unsupported file type: undefined (file: undefined) passed by image-size. Fixes #8, thanks to @Arturszott and @dustingraves for their contribution.

2.0.0 (2017-08-10)

  • Breaking change: request-image-size now returns an ES6 native promise instead of using a callback. Please read about the new API in the README. Requires Node.js v4+.
  • Updated image-size to 0.6.1, adds support for ICO/CUR.

1.3.0 (2017-04-20)

This is the last version using a callback instead of returning a promise.

Basic usage

var requestImageSize = require('request-image-size');

requestImageSize('http://nodejs.org/images/logo.png', function(err, size, downloaded) {

  if (err) {
    return console.error('An error has ocurred:', error);
  }

  if (!size) {
    return console.error('Could not get image size');
  }

  console.log('Image is %dpx x %dpx, downloaded %d bytes', size.width, size.height, downloaded);

});

Advanced usage

Specifying a request options object (docs):

var requestImageSize = require('request-image-size');

var options = {
  url: 'http://nodejs.org/images/logo.png',
  headers: {
    'User-Agent': 'request-image-size'
  }
};

requestImageSize(options, function(err, size, length) {
  console.log(err, size, length);
});

The callback receives three arguments: err, size, downloaded:

  • err returns an Error object if anything goes wrong.
  • size is in the form { width: 245, height: 66, type: 'png' }.
  • downloaded is the number of bytes downloaded before being able to extract the image size.