CollectionFS在转换之前获取图像尺寸

时间:2016-02-28 20:30:55

标签: meteor graphicsmagick collectionfs

我想在使用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函数?

1 个答案:

答案 0 :(得分:2)

有一个异步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:处理输入流和任何“识别”操作时   (大小,格式等),如果你也必须传递“{bufferStream:true}”   之后需要转换(write()或stream())图像注意:这个   将readStream缓存在内存中!

相关问题