簡體   English   中英

discord.js 將圖像放在另一個圖像上

[英]discord.js putting an image on another image

我一直在嘗試發出一個命令,當您發送圖像或圖像的 url 時,它將該圖像放在我已經在代碼中的另一個圖像上,並且每次嘗試將其更改為imageTemp (這是您放置圖像的圖像)或image`` or messageAttachment```。 我會把我的代碼放在下面,這樣你就可以看到問題所在。

const { MessageAttachment } = require('discord.js');
const Jimp = require('jimp');
const discord = require('discord.js')

module.exports = {
  name: "img",
  aliases: [],
  run: async (client, message, args) => {
    let messageAttachment = message.attachments.size > 0 ? message.attachments.array()[0].url : null
    try {
      let imageTemp = "https://upload.wikimedia.org/wikipedia/commons/8/8a/Banana-Single.jpg"
      let image = await Jimp.read(messageAttachment);
      let buffer = await image.getBufferAsync(Jimp.MIME_JPEG);

    Jimp.read(imageTemp).then(image => {
    image.composite(messageAttachment, 10, 10)
    })

      message.channel.send(new MessageAttachment(buffer));
    } catch (err) {
      console.log(err);
      message.channel.send('Oops, there was an error. Try inputting the command again.');
    }
  },
};

這是我得到的結果和錯誤https://imgur.com/a/C6RcTOf

結果

Jimp.composite()方法接受一個Jimp圖像對象作為它的第一個參數。 不是您傳入的 url ( messageAttachment )。此外,您還要在修改圖像之前將圖像保存到緩沖區。

messageAttachmentnull時,會發生您提到的錯誤。 如果郵件沒有附件,您應該檢查並返回。

const imageTemp = "https://upload.wikimedia.org/wikipedia/commons/8/8a/Banana-Single.jpg";
const messageAttachment = message.attachments.size > 0 ? message.attachments.array()[0].url : null;

// Exit if there is no message attachment present in the message
if (!messageAttachment) return;

// Resolve the temporary image url to Jimp object
const firstImage = await Jimp.read(imageTemp);
// Resolve the message attachment image url to Jimp object
const secondImage = await Jimp.read(messageAttachment);

// Composite the two images together (modifies the firstImage object)
firstImage.composite(secondImage, 10, 10);

// Save the resulting image to buffer
const buffer = await firstImage.getBufferAsync(Jimp.MIME_JPEG);

// Send the buffer as a message attachment
message.channel.send("Combined image:", {files: [{ attachment: buffer }]});

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM