我已经使用下面的代码在 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”中,我变得未定义。?