Search code examples
javascriptnode.jsimagemongodbgridfs-stream

Node.js/Mongodb/GridFS resize images on upload


I am saving uploaded images in Mongodb GridFS with Node.js/Express/gridfs-stream/multyparty using streams. Works fine.

Now I would like to "normalize" (resize) images to some standard format before storing to database.

So is there a solution to have a streaming solution to request -> resize -> store to GridFS without installing external libraries?

Current solution (missing the resize step):

function storeImage(req, err, succ){
  var conn = mongoose.connection;
  var gfs = Grid(conn.db);
  var context = {};

  var form = new multiparty.Form();

  form.on('field', function(name, value){
    context[name] = value;
    console.log(context);
  });

  form.on('part', function(part){
    // handle events only if file part
    if (!part.filename) { return; }

    var options =
    {
      filename: part.filename,
      metadata: context,
      mode: 'w',
      root: 'images'
    };
    var ws = gfs.createWriteStream(options);

    // success GridFS
    ws.on('close', function (file) {
      console.log(file.filename + file._id);
      succ(file._id);
    });

    // error GridFS
    ws.on('error', function (errMsg) {
      console.log('An error occurred!', errMsg);
      err(errMsg);
    });

    part.pipe(ws);
  });

  // Close emitted after form parsed
  form.on('close', function() {
    console.log('Upload completed!');
  });

  form.parse(req);

}

Solution

  • For posterity

    1) Initially I used lwip while I was storing images locally. When people started uploading bigger images (which was added as requirement) lwip started exploding my instance on Heroku and I switched to

    2) gm over ImageMagick running on AWS Lambda that has ImageMagick preconfigured in the default instance. Images now stored on S3 and distributed via CloudFront.