2017-07-24 30 views
2

我已经使用下面的代码在nodejs中使用简单图像npm模块完成图像大小调整。如何调整图像大小并使用nodejs中的multer上传到s3并使用easy-image 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);  
    } 

    } 

我已经有了成功的产出。 然后,我已经使用下面的代码将多张图片上传到了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'中的 我没有定义。?

+0

为什么不使用https://www.npmjs.com/package/multer-imager? –

+0

看看我更新的问题@stdob – Jagadeesh

回答

0

为什么不使用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,则必须添加转换选项,该选项指示如何转换文件。转换选项应该是一个数组,包含可以具有属性id,key和transform的对象。这个例子使用sharp来进行变换(一个众所周知的Node.js图像处理模块)。

相关问题