CollectionFS 获取转换前的图像尺寸
CollectionFS get image dimensions before transformation
我想在使用 Meteor CollectionFS 上传图片时调整图片大小。但我想根据图像尺寸调整大小。例如,我想将 1000x500 的图像调整为 1024x512,但将 60x100 的图像调整为 64x128 - 为此我需要知道源尺寸。
我的代码基于 CollectionFS documentation:
var createThumb = function(fileObj, readStream, writeStream) {
// Transform the image into a 10x10px thumbnail
gm(readStream, fileObj.name()).resize('10', '10').stream().pipe(writeStream);
};
如何在此处获取源尺寸,以使我的调整大小目标动态化?也许有一些graphicsmagick功能?
有一个异步 GraphicsMagick 函数用于返回图像的大小(尺寸)。
gm(readStream).size({ bufferStream: true }, function(err, value) {
var w = 100, h = 100;
//modify logic here to set the desired output width/height, based on the source width/height
if (value.width == 60 && value.height == 100) {
w = 64;
h = 128;
}
//"this" is the gm context, so you can chain gm functions to "this" inside the size callback.
this.resize(w, h).stream().pipe(writeStream);
});
Github 上 gm
npm 包页面关于此的注释:
GOTCHA: when working with input streams and any 'identify' operation
(size, format, etc), you must pass "{bufferStream: true}" if you also
need to convert (write() or stream()) the image afterwards NOTE: this
buffers the readStream in memory!
我想在使用 Meteor CollectionFS 上传图片时调整图片大小。但我想根据图像尺寸调整大小。例如,我想将 1000x500 的图像调整为 1024x512,但将 60x100 的图像调整为 64x128 - 为此我需要知道源尺寸。
我的代码基于 CollectionFS documentation:
var createThumb = function(fileObj, readStream, writeStream) {
// Transform the image into a 10x10px thumbnail
gm(readStream, fileObj.name()).resize('10', '10').stream().pipe(writeStream);
};
如何在此处获取源尺寸,以使我的调整大小目标动态化?也许有一些graphicsmagick功能?
有一个异步 GraphicsMagick 函数用于返回图像的大小(尺寸)。
gm(readStream).size({ bufferStream: true }, function(err, value) {
var w = 100, h = 100;
//modify logic here to set the desired output width/height, based on the source width/height
if (value.width == 60 && value.height == 100) {
w = 64;
h = 128;
}
//"this" is the gm context, so you can chain gm functions to "this" inside the size callback.
this.resize(w, h).stream().pipe(writeStream);
});
Github 上 gm
npm 包页面关于此的注释:
GOTCHA: when working with input streams and any 'identify' operation (size, format, etc), you must pass "{bufferStream: true}" if you also need to convert (write() or stream()) the image afterwards NOTE: this buffers the readStream in memory!