【问题标题】:CollectionFS get image dimensions before transformationCollectionFS 获取转换前的图像尺寸
【发布时间】:2023-03-22 21:23:01
【问题描述】:

我想在使用 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的功能?

【问题讨论】:

    标签: meteor graphicsmagick collectionfs


    【解决方案1】:

    有一个异步 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!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-03
      • 1970-01-01
      • 2011-06-04
      • 2011-04-22
      • 2012-07-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多