Node get image properties (height, width)
Asked Answered
C

2

17

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.

Cazzie answered 29/3, 2013 at 2:39 Comment(0)
S
26

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.

Sitton answered 20/8, 2017 at 10:14 Comment(0)
C
11

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);
Cocktail answered 19/3, 2014 at 21:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.