简体   繁体   中英

how to make the size of an image in node canvas always be 128 px

so I'm having this problem here. I use node canvas to make image commands for me and my friends in my discord bot, but recently I found out that default discord profile pictures are shown bigger rather than custom ones when drawn over my canvas. Can anyone tell me what I should do in order to make the size of the images drawn over my canvas always be 128 px?

client.on("message", async (message) => {
  if (message.author.bot) return;
  if (message.content.startsWith("R!grave")) {
const canvas = Canvas.createCanvas(800, 450);
let ctx = canvas.getContext('2d');
let pooh = await Canvas.loadImage('./ez.jpg');
ctx.drawImage(pooh, 0, 0, canvas.width, canvas.height);

const [first, second] = [...message.mentions.users.values()]

    if (!first || !second)
      return message.channel.send('You need to mention two users!');


    let avatar1 = await Canvas.loadImage(first.displayAvatarURL({ dynamic: false, format: 'png' }));
    ctx.drawImage(avatar1, 106, 34, avatar1.width * 0.9, avatar1.height * 0.9);
    let avatar2 = await Canvas.loadImage(second.displayAvatarURL({ dynamic: false, format: 'png' }));
    ctx.drawImage(avatar2, 355, 91, avatar2.width * 0.9, avatar2.height * 0.9);




let attachment = new Discord.MessageAttachment(canvas.toBuffer(), 'example.png')



    return message.channel.send(attachment);
  }
});

您可以将固定的宽度和高度值传递给ctx.drawImage ,因此画布始终以相同的大小绘制它。

ctx.drawImage(avatar2, 355, 91, 128, 128)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM