获取node.js中图像的宽度和高度

时间:2022-11-19 13:20:25

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.

是否可以在节点中获取图像的宽度和高度。js(在服务器端,而不是客户端)?我需要在一个节点中找到图像的宽度和高度。我正在写的js库。

5 个解决方案

#1


32  

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

是的,这是可能的,但是您需要安装GraphicsMagick或ImageMagick。

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

我两者都用过,我推荐GraphicsMagick它更快。

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

#2


87  

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.

安装GraphicsMagick或ImageMagick根本不需要,确定图像的大小就像查看头部一样简单。image-size是一个简单的javascript实现,该功能非常易于使用。

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

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

#3


7  

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

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 cause, it supports local streams too.

更有趣的问题是“如何在不从远程服务器下载完整文件的情况下检测图像大小”。probe-image-size会有所帮助。当然,它也支持本地流。

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

它是用纯JS编写的,不需要任何严重的依赖关系(ImageMagick等)。

#4


2  

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

Calipers是另一个可以确定图像尺寸的纯Javascript库。

https://github.com/calipersjs/calipers

https://github.com/calipersjs/calipers

#5


1  

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

        }
      });

#1


32  

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

是的,这是可能的,但是您需要安装GraphicsMagick或ImageMagick。

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

我两者都用过,我推荐GraphicsMagick它更快。

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

#2


87  

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.

安装GraphicsMagick或ImageMagick根本不需要,确定图像的大小就像查看头部一样简单。image-size是一个简单的javascript实现,该功能非常易于使用。

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

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

#3


7  

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

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 cause, it supports local streams too.

更有趣的问题是“如何在不从远程服务器下载完整文件的情况下检测图像大小”。probe-image-size会有所帮助。当然,它也支持本地流。

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

它是用纯JS编写的,不需要任何严重的依赖关系(ImageMagick等)。

#4


2  

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

Calipers是另一个可以确定图像尺寸的纯Javascript库。

https://github.com/calipersjs/calipers

https://github.com/calipersjs/calipers

#5


1  

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

        }
      });