【问题标题】:How to resize an image and upload using multer in nodejs to s3 and using easy-image npm module?如何使用 nodejs 中的 multer 调整图像大小并上传到 s3 并使用 easy-image npm 模块?
【发布时间】:2017-12-30 02:33:30
【问题描述】:

我已经使用以下代码在 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”中,我变得不确定。?

【问题讨论】:

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


【解决方案1】:

为什么不使用带有内置transforms 和multer s3 模块的multer-s3-transofrm

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 选项,它告诉如何转换文件。

transforms 选项应该是一个数组,包含具有属性 id、key 和 transform 的对象。

此示例使用sharp 进行转换(众所周知的 Node.js 图像处理模块)。

【讨论】:

    猜你喜欢
    • 2018-01-01
    • 2015-06-18
    • 2018-05-07
    • 2023-04-11
    • 2019-05-03
    • 2017-05-22
    • 2011-09-18
    • 2015-04-08
    • 2017-03-22
    相关资源
    最近更新 更多