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

node.js

node.js Problem Overview


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.

node.js Solutions


Solution 1 - node.js

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);
});

Solution 2 - node.js

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);
  }
});

Solution 3 - node.js

https://github.com/nodeca/probe-image-size

More interesting problem is "how to detect image size without full file download from remote server". probe-image-size will help. Of course, it supports local streams too.

It's written in pure JS and does not need any heavy dependencies (ImageMagick and so on).

Solution 4 - node.js

Calipers is another pure Javascript library that can determine the dimensions of images.

https://github.com/calipersjs/calipers

Solution 5 - node.js

var sizeOf = require('image-size');
    sizeOf(my_file_item.completeFilename, function (err, dimensions) {
        try{
          if(!err){
            let image_dimensions = dimensions || "";
            let width = 200; // we want 200 
            let height = parseInt(width/(image_dimensions.width/image_dimensions.height));
    
           
          }else{
            
          }
          // console.log(ex);
        }catch(ex){
          
        }
      });

Solution 6 - node.js

I used Jimp: https://www.npmjs.com/package/jimp npm install --save jimp

const jimage1 = await Jimp.read(imgBuffer);
const jimage2 = await Jimp.read(imgUrl);
const width = jimage1.bitmap.width;

It's effectively a wrapper for pixel-match

Solution 7 - node.js

Easiest way to do this with multer using buffer-image-size

I have used buffer-image-size to validate the image dimensions. I need to validate the exact image size before uploading it to the bucket.

var sizeOf = require('buffer-image-size');
var dimensions = sizeOf(yourImageBuffer);
console.log(dimensions.width, dimensions.height);

ex:

Router.js

Router.post('/upload_image',
         multerUpload.single('image'),     // multer upload configuration
         imageDimensionHandler(500,200),   // here I validate image dimension
         upload_image_controller           // controller function
       );

here is my middleware that I used after multerUpload.single('image') middleware.

imageDimensionHandler.js ( middleware )

const sizeOf = require('buffer-image-size');

const imageDimensionHandler = (width, height) => (async (req, res, next) => {
    if (req.file) {
        const fileBuffer = req.file.buffer;
        var dimensions = sizeOf(fileBuffer);

        if (width == dimensions.width && height == dimensions.height) {
            next();
        } else {
            throw new Error(`expected image size is ${condition.width} x ${condition.height} pixel.`);
        }
    } else {
        next();
    }
});

Note: please ignore it if you don't find it appropriate for you.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAnderson GreenView Question on Stackoverflow
Solution 1 - node.jsLinus UnnebäckView Answer on Stackoverflow
Solution 2 - node.jssaeedView Answer on Stackoverflow
Solution 3 - node.jsVitalyView Answer on Stackoverflow
Solution 4 - node.jsMarcusView Answer on Stackoverflow
Solution 5 - node.jsVIKAS KOHLIView Answer on Stackoverflow
Solution 6 - node.jsBlaze GawlikView Answer on Stackoverflow
Solution 7 - node.jsVrushabh RanpariyaView Answer on Stackoverflow