我想将大尺寸图像从jpg或png压缩到webp,所以我尝试了imagemin和imagemin-webp库,并使用multer将它们集成到我的图片上传代码中:
enum
但是使用console.log(files),这是我上传2个文件时得到的:
let storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './uploads/rooms');
},
filename: function (req, file, cb) {
cb(null, req.params.room_id + "_" + Date.now() + "_" + Math.floor(Math.random() * 10000) + file.originalname.substr(-7));
}
});
let upload = multer({ storage: storage })
//add pictures to a room
venuesRoutes.route('/:id/room/:room_id/add_pictures').post(upload.array('pics', 20), function (req, res) {
Venue.findOne({ _id: req.params.id, user: req.user._id }).populate('city').populate('rooms.availability').exec(function (err, venue) {
if (err) {
res.status(404).send("venue is not found");
}
else {
let room = venue.rooms.id(req.params.room_id)
req.files.map((pic) => {
(async () => {
const files = await imagemin([pic.path], {
destination: './uploads/compressed-images',
plugins: [imageminWebp({ quality: 50 })],
});
console.log(files);
})();
room.pics.push({ path: pic.path })
})
venue.save(function (err) {
if (!err) res.status(200).send({ msg: "pictures updated", venue: venue });
});
}
});
});