NodeJS:旋转base64图像并保存

NodeJS : Rotate a base64 image and save it

我想制作一个将 base64 图像旋转 90° 的功能。我尝试使用图像和 Canvas 但它 returns 错误。

这是代码:

exports.Test64 = (req, res, next) => {
 console.log("TEST UPS")
  upsres.find().then(
    (ups) => {
      console.log('YES');
      console.log(ups[0].b64Image); // it works but after i don't think
      var image = new Image();
      var canvas = document.getElementById("c");
      var ctx = canvas.getContext("2d");
      image.src = ups[0].b64Image;
      canvas.width = image.height;
      canvas.height = image.width;
      ctx.rotate(-90 * Math.PI / 180);
      ctx.translate(-canvas.heigt,0);
      ctx.drawImage(image, 0, 0);
      dataURL= canvas.toDataURL();
      console.log(dataURL);
      res.status(200).json(dataURL);
      );
    }
  ).catch(
    (error) => {
      res.status(400).json({
        error:'Not Working'
      });
    }
  );
};

有人知道为什么它不起作用吗? :)

感谢大家

我们可以使用jimp libray来旋转图像。 请使用以下代码

   var Jimp = require('jimp');
    const base64str ="R0lG"//
     const buf = Buffer.from(base64str, 'base64');
    const image = await Jimp.read(buf);

   // rotate Function having a rotation as 90
    image.rotate(90).getBase64(Jimp.MIME_JPEG, function (err, src) {
    console.log("rb is \n")
    console.log(src);
  })
exports.Testups = async (req, res, next) => {
  try {
    let ups = await upsres.find()
    const base64str = ups[0].b64Image;
    const buf = Buffer.from(base64str, 'base64');
    let image = await Jimp.read(buf);

    // rotate Function having a rotation as 90
    image.rotate(-90).getBase64(Jimp.MIME_PNG, function (err, src) {
    res.status(200).json(src);
    })
  }
  catch (e) { }
};

行得通:)