节点获取图像属性(高度,宽度)

时间:2022-11-19 13:15:23

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.

我正在寻找一种方法来从本地获取给定路径的图像的高度和宽度。我知道imagemagick和graphicmagick,但我更喜欢一种不涉及在操作系统中安装额外软件的方法。如果我能将它保存到节点模块那将是非常棒的。

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.

在最糟糕的情况下,我将使用IM和GM,但它喜欢说它宁愿避免这条路径。

2 个解决方案

#1


8  

You can use a pure JS node module https://www.npmjs.org/package/image-size .. doesn't require installing anything extra

您可以使用纯JS节点模块https://www.npmjs.org/package/image-size ..不需要安装任何额外的东西

var sizeOf = require('image-size');
var dimensions = sizeOf('images/funny-cats.png');
console.log(dimensions.width, dimensions.height);

#2


5  

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.

您可以使用JIMP(JavaScript Image Manipulation Program)。 Node的图像处理库完全用JavaScript编写,没有外部或本机依赖。如果您愿意,它还有许多其他图像处理选项可用。

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.

希望这会有所帮助。

#1


8  

You can use a pure JS node module https://www.npmjs.org/package/image-size .. doesn't require installing anything extra

您可以使用纯JS节点模块https://www.npmjs.org/package/image-size ..不需要安装任何额外的东西

var sizeOf = require('image-size');
var dimensions = sizeOf('images/funny-cats.png');
console.log(dimensions.width, dimensions.height);

#2


5  

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.

您可以使用JIMP(JavaScript Image Manipulation Program)。 Node的图像处理库完全用JavaScript编写,没有外部或本机依赖。如果您愿意,它还有许多其他图像处理选项可用。

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.

希望这会有所帮助。