简体   繁体   中英

Using imagemin to compress images and store in mongodb

Im trying to retrieve an image that has been posted to our nodejs server, compress it and then store it in mongodb. I'm using imagemin which is working ie it can take the incoming image, compress it and store it off to a static destination folder. However, I want to take the output of the imagemin process and store that image in mongo rather than the destination folder!

This is logic sits inside the multer middleware;

app.use(multer({
    dest : './static/uploads/',
    limits : {...
    rename : function(fieldname, filename) {....
    onFileUploadComplete : function(file) {

    //var imagemin = new Imagemin().src(file.path).dest('./static/uploads/').use(compressionType); 
    var imagemin = new Imagemin().src(file.path).use(compressionType);
     imagemin.run(function(err, files) {        
         if (err) {             
           return next(err);        
         }
        console.log('Files optimized successfully!');   });
        // DO SOME LOGIC HERE TO STORE IMAGE(S) TO MONGO
    }
};

From reading the FAQ for imagemin, is the run() function the correct place to handle the image? Is this the image that is stored to the destination folder?

Sample package.json with versions;

{
    "name": "test-project",
    "description": "Another Test",
    "version": "0.0.1",
    "engines": {
        "node": ">= 0.10.x"
    },
    "private": true,
    "dependencies": {
        "express": "3.x",
        "multer": "0.x",
        "imagemin": "3.0.0",
        "mongoose": "~3.8.8",
        "mongoose-thumbnail": "0.0.1",
        "mongoose-file": "0.0.2"        
    }
}

Any help or direction is appreciated.

J

This might not be the answer you want -- but you probably don't want to store images in MongoDB. Database systems are not really meant to handle the storage of large, binary files like images. This is because storing images is going to eat up both a lot of diskspace, as well as slow down queries considerably.

If you're building a production application, you might instead want to look at storing your image files into a file storage service like Amazon S3: http://aws.amazon.com/s3/ You can do this using a number of libraries, including the officially supported AWS Node library: http://aws.amazon.com/sdk-for-node-js/

Now -- if you really want to store images in MongoDB anyway, you could do something like this:

var imagemin = new Imagemin().src(file.path).use(compressionType);
imagemin.run(function(err, files) {        
  if (err) {             
    return next(err);        
  }
  for (var i = 0; i < files.length; i++) {
    var model = new MyMongooseModel({
      image: files[i].contents,  // files[i].contents is a Buffer that contains the image file's contents
      ...
    });
    model.save(function(err) {
      if (err) {
        return next(err);
      }
    });
  }
});

This requires you to be using the Mongoose library for working with MongoDB: http://mongoosejs.com/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM