【问题标题】:Sharp image rotation works only once锐利的图像旋转只工作一次
【发布时间】:2019-09-29 04:02:12
【问题描述】:

我在 Node.js 的 Express 服务器上创建了一个将图像旋转 90 度的函数。 它在第一次被调用时起作用,但不是第二次。如果我重新启动服务器,那么它会再次运行一次,所以如果我重新启动服务器 3 次,那么我可以将图像一直旋转。

Product.findById 是一个 mongoose 查询,用于查找从前端请求中指定的图像 ID 的图像名称。

在第 7 行的第一次和第二次尝试中,console.log 返回正确的植物路径/名称,并且没有引发错误。 响应状态是 200“图像旋转”,两次也是


router.patch("/rotate/:image", (req, res, next) => {
  let image = ""
  Product.findById(req.params.image)
  .exec()
  .then(result => {
    image = './uploads/resized/'+result.image
    console.log("image", image)

    sharp(image)
    .rotate(90)
    .withMetadata()
    .toBuffer(function(err, buffer) {
      if(err) throw err
      fs.writeFile(image, buffer, function() {
        res.status(200).json("image rotated")
      });
    })
  })
  .catch(err => {res.status(400).json("invalid img id")
    console.log(err)})
  })

预期的输出是在每个 http 请求上旋转 90 度的图像,但实际输出只是在第一个 http 请求上旋转 90 度的图像。

【问题讨论】:

    标签: javascript node.js fs sharp


    【解决方案1】:

    导入sharp模块后尝试禁用缓存写入以下行:

    sharp.cache(false);
    

    【讨论】:

    • 非常感谢,这正是问题所在!
    猜你喜欢
    • 2021-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 2019-07-19
    • 2017-01-17
    • 1970-01-01
    • 2017-01-04
    相关资源
    最近更新 更多