Skip to content

Get image size without full download. Supported image types: JPG, GIF, PNG, WebP, BMP, TIFF, PSD.

License

Notifications You must be signed in to change notification settings

jasonsenssylvain/probe-image-size

 
 

Repository files navigation

probe-image-size

Build Status NPM version

Get image size without full download. Supported image types: JPG, GIF, PNG, WebP, BMP, TIFF, PSD.

Install

npm install probe-image-size --save

Example

var probe = require('probe-image-size');

// Get by URL
//
probe('http://example.com/image.jpg', function (err, result) {
  console.log(result); // => { width: xx, height: yy, type: 'jpg', mime: 'image/jpeg' }
});

// By URL with options
//
probe({ url: 'http://example.com/image.jpg', timeout: 5000 }, function (err, result) {
  console.log(result); // => { width: xx, height: yy, length: zz, type: 'jpg', mime: 'image/jpeg' }
});

// From the stream
//
var input = require('fs').createReadStream('image.jpg');

probe(input, function (err, result) {
  console.log(result); // => { width: xx, height: yy, type: 'jpg', mime: 'image/jpeg' }

  // terminate input, depends on stream type,
  // this example is for fs streams only.
  input.destroy();
});

API

probe(src, callback(err, result))

src can be of this types:

  • String - URL to fetch
  • Object - options for request, defaults are { timeout: 5000, maxRedirects: 2 }
  • Stream - readable stream

result contains:

{
  width: XX,
  height: YY,
  length: ZZ, // byte length of the file (if available, HTTP only)
  type: ..., // image 'type' (usual file name extention)
  mime: ...  // mime type
}

err is an error, which is extended with:

  • code - equals to ECONTENT if the library failed to parse the file;
  • status - equals to a HTTP status code if it receives a non-200 response.

Note. If you use stream as source, it's your responsibility to terminate reading in callback. That will release resources as soon as possible. On http requests that's done automatically.

Similar projects

License

MIT

About

Get image size without full download. Supported image types: JPG, GIF, PNG, WebP, BMP, TIFF, PSD.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 93.1%
  • Makefile 6.9%