如何调整图像大小并使用nodejs中的multer上传到s3并使用easy-image npm模块?

时间:2017-07-24 13:17:30

标签: node.js amazon-s3 multer multer-s3

我使用下面的代码在nodejs中使用简单的图像npm模块调整了图像的大小。

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);    
     }

  }

我有成功的输出。 然后我使用下面的代码和multer将我的图像上传到s3。

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);
             });

现在我的问题是如何在上传到s3之前调整图像大小,然后将调整后的图像上传到s3?

我也尝试过使用multer-imager模块。

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
             })

但它不起作用。 在' req.file'我未定义了。?

1 个答案:

答案 0 :(得分:1)

为什么不将multer的内置变换与multer s3模块结合使用?

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())
      }
    }]
  })
})

来自文档:可选的shouldTransform选项告诉multer是否应该在上传文件之前对其进行转换。默认情况下,它设置为false。如果设置为true,则必须添加转换选项,该选项说明如何转换文件。 transforms选项应该是一个Array,包含可以具有属性id,key和transform的对象。此示例使用sharp进行转换(众所周知的Node.js图像处理模块)。

相关问题