How to resize an image and upload using multer in nodejs to s3 and using easy-image npm module?
Asked Answered
C

1

7

I have done resizing of an image using easy image npm module in nodejs using the below code.

var easyimg = require('easyimage');

easyimg.rescrop({
     src:'1.jpg', dst:'/var/www/html/bangalore.jpg',
     width:100, height:100

  }),function(image,err){
     // console.log('Resized and cropped: ' + image.width + ' x ' + image.height);
     if(image){
     console.log(image);   
     }
     else{
     console.log(err);    
     }

  }

I have got successfull output. Then i have uploaded my image to s3 using the below code with multer.

var storage = multerS3({
              s3: s3,
              bucket: 'my_bucket_name',
              key: function (req, file, cb) {
                  console.log(file);
                  file_name = file.originalname;
                  var newFileName = Date.now() + "-" + file.originalname;
                  cb(null, newFileName);
              }
           });
              var upload = multer({storage: storage}).single('profileImage');
             upload(req, resq, function (err,res,response) {
              console.log(response);
             });

Now my question is how to resize an image before uploading to s3 and then uploading the resized image to s3?

I have also tried using multer-imager module.

var transfer = imager({

              secretAccessKey: 'secretAccessKey',
              accessKeyId: 'myaccesskey',
              dirname:'avatar',
              bucket: 'my_bucket',

              region:'myregion',

              key: function (req, file, cb) {
                  console.log(file);
                  file_name = file.originalname;
                  var newFileName = Date.now() + "-" + file.originalname;

                cb(null, newFileName);
                console.log(newFileName);

              },                                    //
    gm: {                                 // [Optional]: define graphicsmagick options
      width: 200,                         // doc: http://aheckmann.github.io/gm/docs.html#resize
      height: 200,
      options: '!',
      format: 'png'                       // Default: jpg
    }
           });
              var upload = multer({storage: transfer}).single('myimage');
             upload(req, resq, function (err,res,response) {
              console.log(req.file); //i am getting this as undefined
             })

But it doesnt work. in 'req.file' i am getting undefined.?

Camelback answered 24/7, 2017 at 13:17 Comment(3)
Why not use npmjs.com/package/multer-imager?Rees
Look my updated question @stdobCamelback
Instead of depending 3rd parties you can go with multer, aws-sdk and sharp. Here's a full example of how to do it. https://mcmap.net/q/1478275/-multer-s3-with-sharp-upload-and-resizePierides
D
14

Why not using multer-s3-transofrm with built in transforms combined with multer s3 module?

var upload = multer({
  storage: multerS3({
    s3: s3,
    bucket: 'some-bucket',
    shouldTransform: function (req, file, cb) {
      cb(null, /^image/i.test(file.mimetype))
    },
    transforms: [{
      id: 'original',
      key: function (req, file, cb) {
        cb(null, 'image-original.jpg')
      },
      transform: function (req, file, cb) {
        //Perform desired transformations
        cb(null, sharp().resize(600, 600).max())
      }
    }]
  })
})

From the docs:

The optional shouldTransform option tells multer whether it should transform the file before it is uploaded. By default, it is set to false. If set to true, transforms option must be added, which tells how to transform the file.

transforms option should be an Array, containing objects with can have properties id, key and transform.

This example uses sharp for the transform (a well known Node.js image processing module).

Dialectal answered 8/12, 2017 at 17:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.