Skip to content
This repository has been archived by the owner on Mar 14, 2019. It is now read-only.

How to: Convert image to a different format

Philipp edited this page Mar 1, 2015 · 1 revision

To convert every file to a specific image format, you can pass a GraphicsMagick format string to the stream method, but you will also need to alter the FS.File instance as necessary in a beforeWrite function.

Images = new FS.Collection("images", {
    stores: [
      new FS.Store.FileSystem("images"),
      new FS.Store.FileSystem("thumbs", {
        beforeWrite: function(fileObj) {
          // We return an object, which will change the
          // filename extension and type for this store only.
          return {
            extension: 'png',
            type: 'image/png'
          };
        },
        transformWrite: function(fileObj, readStream, writeStream) {
          // Transform the image into a 10x10px PNG thumbnail
          gm(readStream).resize(60).stream('PNG').pipe(writeStream);
          // The new file size will be automatically detected and set for this store
        }
      })
    ],
    filter: {
      allow: {
        contentTypes: ['image/*'] //allow only images in this FS.Collection
      }
    }
});

Note that this example requires the cfs-filesystem package.