Meteor-Community-Packages / Meteor-CollectionFS

Reactive file manager for Meteor
MIT License
1.05k stars 237 forks source link

Alternative to cfs:graphicsmagick #885

Open MilosStanic opened 8 years ago

MilosStanic commented 8 years ago

Hello all, especially authors. Since I had some problems deploying my app and using cfs:graphicsmagick, I decided to explore an alternative. This may be a nice addition to the Wiki. The alternative is to use elad/node-imagemagick-native which utilizes native bindings to C/C++ Magick++ library, rather than calling cli commands like cfs:graphicsmagick.

Here's how:

To be able to use Magick++ library you have to install it. Please refer to installation instructions here.

First you need to install meteorhacks:npm. Please follow their instructions.

When you install it, it will create for you in the root of your project a file packages.json. Make its contents like this:

{
    "imagemagick-native": "1.8.0"
}

Then run meteor, and meteor will download the npm lib, abort, and ask you to start meteor again.

Now, to the example. In your /lib directory, create a file named, say images.js and paste:

if(Meteor.isServer){ 
  var imagemagick = Meteor.npmRequire('imagemagick-native');
}

var createThumb = function(fileObj, readStream, writeStream) {
  // Transform the image into a 72x72px thumbnail
  readStream.pipe(imagemagick.streams.convert({
    srcData: readStream,
    width: 72,
    height: 72,
    debug: true,
    resizeStyle: 'aspectfill', // is the default, or 'aspectfit' or 'fill'
    gravity: 'Center' // optional: position crop area when using 'aspectfill'
  })).pipe(writeStream);
};

Images = new FS.Collection("images", {
  stores: [
    new FS.Store.FileSystem("thumbs", { transformWrite: createThumb }),
    new FS.Store.FileSystem("images"),
  ],
  filter: {
    allow: {
      contentTypes: ['image/*'] //allow only images in this FS.Collection
    }
  }
});

I hope this helps someone. Thanks for your wonderful work on the Meteor-CollectionFS!

nooitaf commented 8 years ago

Nice alternative indeed, thanks for sharing :)