我正在制作一个图像生成器服务,该服务可让用户提供文本或图像参数,以便对其进行操纵。它使用Express.js,Canvas,Node-Superfetch。
但是,我在生成图像时遇到了问题...问题是,当我提供以下命令时,控制台中出现“ TypeError:无效参数”和“ stream.on不是函数”图片链接参数进入图片生成器链接。
代码:
Challenger.js(创建图像的文件)
const { createCanvas, loadImage, registerFont } = require("canvas");
const request = require('node-superfetch');
module.exports = async function BeLikeBillEndpoint(image) {
const base = await loadImage('https://i.imgur.com/7I8JFDS.png');
const {body} = await request.get(image);
const data = await loadImage(body);
const canvas = createCanvas(base.width, base.height);
const ctx = canvas.getContext('2d');
ctx.drawImage(base, 0, 0);
ctx.drawImage(data, 1620,350, 800, 800, 184/2);
return canvas.createPNGStream();
return canvas.toBuffer();
}
index.js(处理图像的位置)
const ChallengerEndpoint = require("./backend/generate/challenger");
function status(type, msg, res) {
if (type === false) res.status(400);
return res.json({sucess: type, message: msg});
}
function streamStatus(type, stream, res) {
if (type === false) res.status(400);
// now do stuff with the stream
let data;
stream.on("data", (b) => {
res.write(b);
});
stream.on("end", () => {
res.end();
});
};
function isImage(link) {
let type = link.substring(link.lastIndexOf('.') + 1);
let isPNG = /(png)/gi.test(type); //eslint-disable-line
if (!isPNG) return false;
return true;
}
//Later in the code...
app.get('/api/challenger', async (req, res) => {
if (!req.query.url) return status(false, 'No URL Provided', res);
let check = await isImage(req.query.url);
if (!check) return status(false, 'The Image Isnt Of The PNG Type.', res);
let result = await ChallengerEndpoint(req.query.url);
return streamStatus(result[0], result[1], res);
});
结果应如下所示: http://ichigo-api.glitch.me/api/challenger?url=https://i.imgur.com/7xTdTUK.png
如果您想查看完整的代码,我可以提供。