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

我已经使用下面的代码在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'我变得不确定。

为什么不使用Muller的内置转换结合Muller 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是否应该在file upload之前转换文件。 默认情况下,它被设置为false。 如果设置为true,则必须添加转换选项,该选项指示如何转换文件。 转换选项应该是一个数组,包含具有属性id,key和transform的对象。 这个例子使用sharp来进行变换(一个众所周知的Node.jsimage processing模块)。