我想使一个函数将base64图像旋转90°。我尝试使用图像和画布,但返回错误。
代码在这里:
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'
});
}
);
};
有人回答为什么它不起作用? :)
谢谢大家
答案 0 :(得分:0)
我们可以使用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);
})
答案 1 :(得分:0)
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) { }
};
正在工作:)