Get image dimensions from url path

Mj1992 picture Mj1992 · Jul 6, 2014 · Viewed 9.6k times · Source

I am trying to load the dimensions of an image from url. So far I've tried using GraphicsMagick but it gives me ENOENT error.

Here's the code so far I've written.

var gm = require('gm');

...

gm(img.attribs.src).size(function (err, size) {
      if (!err) {
            if( size.width>200 && size.height>200)
            {
                console.log('Save this image');
            }
      }
}); 

Where img.attribs.src contains the url source path of the image.

Update

value of img.attribs.src

http://rack.1.mshcdn.com/assets/header_logo.v2-30574d105ad07318345ec8f1a85a3efa.png

Answer

Vitaly picture Vitaly · Jun 25, 2016

https://github.com/nodeca/probe-image-size it does exactly you asked about, without heavy dependencies. Also, it downloads only necessary peace of files.

Example:

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

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

Disclaimer: I am the author of this package.