如何在 discord.js 中为 discord bot 合并图像?

我正在尝试创建一个将头像与图像合并的命令,例如,如果有人键入 =jail,则头像上的监狱栏 - 我已经创建了一个对命令进行排序的命令处理程序。但是,我无法弄清楚如何实现这一目标。我正在使用 Windows 平台(显然这意味着我不能使用节点 gd)



小唯快跑啊
浏览 107回答 1
1回答

牧羊人nacy

您可以为此使用 Jimp。这是 NPM 网站:https ://www.npmjs.com/package/jimp它允许您修改图片、添加文本等。您想要的内容类似于以下内容://an array of all images we're using. MAKE SURE THEIR SIZES MATCHvar images = [<link to the user's avatar>, <link to an image of jail bars>]var jimps = []//turns the images into readable variables for jimp, then pushes them into a new arrayfor (var i = 0; i < images.length; i++){&nbsp; &nbsp; jimps.push(jimp.read(images[i]))}//creates a promise to handle the jimpsawait Promise.all(jimps).then(function(data) {&nbsp; &nbsp; return Promise.all(jimps)}).then(async function(data){&nbsp; &nbsp; // --- THIS IS WHERE YOU MODIFY THE IMAGES --- \\&nbsp; &nbsp; data[0].composite(data[1], 0, 0) //adds the second specified image (the jail bars) on top of the first specified image (the avatar). "0, 0" define where the second image is placed, originating from the top left corner&nbsp; &nbsp; //you CAN resize the second image to fit the first one like this, if necessary. The "100, 100" is the new size in pixels.&nbsp; &nbsp; //data[1].resize(100,100)&nbsp; &nbsp; //this saves our modified image&nbsp; &nbsp; data[0].write(<path on your local disk to save the image in>)})现在您所要做的就是从本地磁盘发送图像:message.channel.send(`Jailed!`, {file: `${<path to the image>}`})
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript