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

前端 未结 5 1694
有刺的猬
有刺的猬 2020-12-23 15:58

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 lib

相关标签:
5条回答
  • 2020-12-23 16:52

    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).

    0 讨论(0)
  • 2020-12-23 16:59

    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);
    });
    
    0 讨论(0)
  • 2020-12-23 17:00

    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);
      }
    });
    
    0 讨论(0)
  • 2020-12-23 17:01
    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){
    
            }
          });
    
    0 讨论(0)
  • 2020-12-23 17:03

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

    https://github.com/calipersjs/calipers

    0 讨论(0)
提交回复
热议问题