David
David

Reputation: 367

Node get image properties (height, width)

I'm looking for a means to get the height and width of images from a given path locally. I know about imagemagick and graphicmagick but I'd prefer a method that doesn't involve installing extra software to the OS. If I can keep it to node modules that would be fantastic.

Does anyone have any ideas that may help me?

Worst case scenario, I'll use IM and GM but like it said would prefer to avoid this path.

Upvotes: 17

Views: 20981

Answers (2)

Nitin Bhapkar
Nitin Bhapkar

Reputation: 364

You can use JIMP(JavaScript Image Manipulation Program). An image processing library for Node written entirely in JavaScript, with zero external or native dependencies. It has so many other image manipulation options available if you want.

var Jimp = require('jimp');
var image = new Jimp("./path/to/image.jpg", function (err, image) {
    var w = image.bitmap.width; //  width of the image
    var h = image.bitmap.height; // height of the image
});

Hope this will help.

Upvotes: 26

NetRoY
NetRoY

Reputation: 191

You can use a pure JS node module https://www.npmjs.org/package/image-size .. doesn't require installing anything extra

var sizeOf = require('image-size');
var dimensions = sizeOf('images/funny-cats.png');
console.log(dimensions.width, dimensions.height);

Upvotes: 11

Related Questions