GraphicsMagick how to resize images from binary data

396 Views Asked by At

I'm trying to use GraphicsMagick to resize the image before uploading to Amazon S3.

I was able to get the binary data of the image file from like this:

        reader.onload = function(e) {

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

But the problem is reader.result is not a file URL. It's a binary string data.

How can I take this binary string data and resize it?

1

There are 1 best solutions below

0
On

This might be a late response to your question but answering this for anyone who has the same question.

You can resize images before upload with GraphicsMagick from transformWrite option while initializing the Fs.Store

Image Manipulation from collectionFS, here is a snippet from 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/*']
    }
  }
});