Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the width and height of an image in node.js

Tags:

node.js

Is it possible to get the width and height of an image in node.js (on the server side, not the client side)? I need to find the width and height of an image in a node.js library that I'm writing.

like image 671
Anderson Green Avatar asked Sep 22 '12 01:09

Anderson Green


People also ask

How to get width and height of image in NodeJS?

file) { const fileBuffer = req. file. buffer; var dimensions = sizeOf(fileBuffer); if (width == dimensions. width && height == dimensions.

How can get image width and height in jquery?

var imageWidth = $(Imgsize). width(); alert(imageWidth);


2 Answers

Installing GraphicsMagick or ImageMagick isn't at all needed, determining the dimensions of a image is as easy as looking at the header. image-size is a pure javascript implementation of said feature which is very easy to use.

https://github.com/netroy/image-size

var sizeOf = require('image-size'); sizeOf('images/funny-cats.png', function (err, dimensions) {   console.log(dimensions.width, dimensions.height); }); 
like image 142
Linus Unnebäck Avatar answered Sep 21 '22 14:09

Linus Unnebäck


Yes this is possible but you will need to install GraphicsMagick or ImageMagick.

I have used both and I can recommend GraphicsMagick it's lot faster.

Once you have installed both the program and it's module you would do something like this to get the width and height.

gm = require('gm');  // obtain the size of an image gm('test.jpg') .size(function (err, size) {   if (!err) {     console.log('width = ' + size.width);     console.log('height = ' + size.height);   } }); 
like image 42
saeed Avatar answered Sep 20 '22 14:09

saeed