GraphicsMagick如何从二进制数据调整图像大小

时间:2014-11-28 03:16:52

标签: meteor imagemagick graphicsmagick

我正在尝试使用GraphicsMagick调整图像大小,然后再上传到Amazon S3。

我能够像这样获取图像文件的二进制数据:

        reader.onload = function(e) {

            Meteor.call('s3_upload', file, reader.result)
        }
        reader.readAsDataURL( file )

但问题是reader.result不是文件URL。这是一个二进制字符串数据。

如何获取此二进制字符串数据并调整其大小?

1 个答案:

答案 0 :(得分:1)

这可能是对您的问题的延迟回复,但对于有相同问题的任何人都要回答这个问题。

您可以在初始化Fs.Store时使用transformWrite选项从GraphicsMagick上传之前调整图像大小

来自collectionFS的

Image Manipulation, 这是来自collectionFS S3 doc

的片段
    var avatarStoreLarge = new FS.Store.S3("avatarsLarge", {
  accessKeyId: "ID-HERE", 
  secretAccessKey: "ACCESS-KEY-HERE", 
  bucket: "avatars.large",
  transformWrite: function(fileObj, readStream, writeStream) {
    gm(readStream, fileObj.name()).resize('250', '250').stream().pipe(writeStream)
  }
});

var avatarStoreSmall = new FS.Store.S3("avatarsSmall", {
  accessKeyId: "ID-HERE", 
  secretAccessKey: "ACCESS-KEY-HERE", 
  bucket: "avatars.small",
  beforeWrite: function(fileObj) {
    fileObj.size(20, {store: "avatarStoreSmall", save: false});
  },
  transformWrite: function(fileObj, readStream, writeStream) {
    gm(readStream, fileObj.name()).resize('20', '20').stream().pipe(writeStream)
  }
});


Avatars = new FS.Collection("avatars", {
  stores: [avatarStoreSmall, avatarStoreLarge],
  filter: {
    allow: {
      contentTypes: ['image/*']
    }
  }
});