Mj1992
Mj1992

Reputation: 3504

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

Upvotes: 8

Views: 16510

Answers (4)

Vitaly
Vitaly

Reputation: 3460

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.

Upvotes: 26

Mj1992
Mj1992

Reputation: 3504

I've used this library to perform the operation successfully.

Upvotes: 2

pwlmaciejewski
pwlmaciejewski

Reputation: 116

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.

Upvotes: 5

Muhammad Ali
Muhammad Ali

Reputation: 2014

You need the image-size NPM, this work fine at my end hope work at your

var sizeOf = require('image-size');
sizeOf('/images/'+adsImage, function (err, dimensions) {
        var imgwidth= dimensions.width;
        var imgheight= dimensions.height;
        console.log(imgheight +" = = = =  "+imgwidth)
  if( imgwidth>200 && imgheight>200)
        {
            console.log('Save this image');
        }


});

Upvotes: 0

Related Questions