Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get image dimensions from url path

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

like image 636
Mj1992 Avatar asked Nov 28 '22 15:11

Mj1992


2 Answers

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.

like image 138
Vitaly Avatar answered Dec 19 '22 13:12

Vitaly


What you want to do is to download the file first. The easiest way is to use request module. The cool thing is that both request and gm can use streams. The only thing you need to remember when working with streams and gm's identify commands (like size, format, etc) you need to set bufferStream option to true. More info here.

var gm = require('gm');
var request = require('request');
var url = "http://strabo.com/gallery/albums/wallpaper/foo_wallpaper.sized.jpg";

var stream = request(url);
gm(stream, './img.jpg').size({ bufferStream: true }, function (err, size) {
    if (err) { throw err; }
    console.log(size);
});

You could also download file on disk (using request as well) an then use gm as normal.

like image 28
pwlmaciejewski Avatar answered Dec 19 '22 14:12

pwlmaciejewski