如何使用gridfs nodeJS调整图像大小并存储不同的sizet?

问题描述

我是 mongoDB 和 gridfs 的初学者。我有一个代码可以在数据库中保存传入的图像。但我想在保存之前调整图像的大小。它试图使用sharp,但我真的不明白它是如何工作的。

我尝试了以下代码,但它不起作用

const storage = new GridFsstorage({
  url: mongoURI,file: (req,file) => {
    return new Promise((resolve,reject) => {
      const filename = req.params.userID;
      const fileInfo = {
        filename: filename,bucketName: "ProfilePic",};
      resolve(fileInfo);
    });
  }
});

const upload = multer({ storage });

router.post("/setProfilePic/:userID",upload.single("picture"),async(req,res) => {
//code not working
  await sharp(req.file.path)
    .resize(500)
    .jpeg({quality: 50})
    .toFile(
        path.resolve(req.file.destination,'resized',image)
    )
// end of non working code
  return res.status(200).json({state: "ok"})
});

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)