node.js - node-gd 的复制功能无法识别

标签 node.js image

我继承了Discord使用 node-gd 的机器人制作带有用户输入数据的横幅。制作横幅时,我使用 .copy功能将图像放置在基本图像的顶部。但是,当我运行我的代码时,它抛出一个错误,说它不是一个函数。

这是相关的代码( user.input 是一个数字 1-5 ,默认是 ? ):

   if (user.input != '?') {
    imgTemp = gd.createFromPng(`./img/INPUT_${user.input}.png`);
    imgTemp.copy(img, 213, 60, 0, 0, 37, 17);
}

这是错误:
> /home/discordbots/Desktop/bots/splatoonBot/splatBot2.js:676
>             imgTemp.copy(img, 213, 60, 0, 0, 37, 17);
>                     ^ TypeError: imgTemp.copy is not a function
>     at generateBanner (/home/discordbots/Desktop/bots/splatoonBot/splatBot2.js:676:21)
>     at Client.<anonymous> (/home/discordbots/Desktop/bots/splatoonBot/splatBot2.js:894:25)
>     at Client.emit (events.js:315:20)
>     at MessageCreateAction.handle (/home/discordbots/Desktop/bots/splatoonBot/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)
>     at Object.module.exports [as MESSAGE_CREATE] (/home/discordbots/Desktop/bots/splatoonBot/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
>     at WebSocketManager.handlePacket (/home/discordbots/Desktop/bots/splatoonBot/node_modules/discord.js/src/client/websocket/WebSocketManager.js:386:31)
>     at WebSocketShard.onPacket (/home/discordbots/Desktop/bots/splatoonBot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:436:22)
>     at WebSocketShard.onMessage (/home/discordbots/Desktop/bots/splatoonBot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:293:10)
>     at WebSocket.onMessage (/home/discordbots/Desktop/bots/splatoonBot/node_modules/ws/lib/event-target.js:125:16)
>     at WebSocket.emit (events.js:315:20)

最佳答案

这是因为 gd.createFromPng返回一个 promise 。它不是包含方法 copy 的类.您可以在 node-gd documentation 中阅读更多相关信息。 .

所以本质上:

if (user.input != '?') {
    // the "await" keyword lets you grab the actual value from the promise
    let imgTemp = await gd.createFromPng(`./img/INPUT_${user.input}.png`);
    // You can then implement the copy method on this newly returned value
    imgTemp.copy(img, 213, 60, 0, 0, 37, 17);
}

希望这能解决您的问题。

关于node.js - node-gd 的复制功能无法识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61946611/

相关文章:

android - 如何从内部存储中选择图像并将其插入到 SQLite 中?

html - Angular 2 - 动态设置图像 URL

image - 了解霍夫变换

mysql - Expressjs - 无法在 session 变量中设置未定义的属性 'userId'

node.js - Nodemailer - 错误 : Missing credentials for "PLAIN" at SMTPConnection. _formatError

javascript - 登录 Gmail 失败,原因不明

javascript - Node.js 中的 SocketCAN

node.js - 在 Heroku 上部署 Nodejs 无法提供位于子文件夹中的静态文件

image - gnuplot:如何为每个像素绘制一个2D数组元素而没有空白

javascript - 智能调整图像预览大小?