首页 > 解决方案 > 我需要组合两个图像,以便它们在 discord.js 中并排在一起

问题描述

我想这样做,所以我的机器人将两个上传的图像组合成一个图像,上传的图像并排(水平)放置。我的程序提示用户上传第一张照片,然后上传第二张照片。收到照片后,我希望将它们组合起来。我尝试使用 Jimp 将照片合成到更大的裁剪照片上,但我无法让机器人发送合成结果。

// ------------------------------------------ front photo ------------------------------------------

        message.channel.send({embed: {color: '#ffdf20', description: 'Upload front photo'}});
        
        // waits for link message,
        await message.channel.awaitMessages(filter, {
            max: 1,
            time: 60000,
            errors: ['time']
        })
        .then(async(collected) => {
            frontphoto = collected.first().attachments.first().url;

    // ------------------------------------------ back photo ------------------------------------------

            message.channel.send({embed: {color: '#ffdf20', description: 'Upload back photo'}});
            
            // waits for link message,
            await message.channel.awaitMessages(filter, {
                max: 1,
                time: 60000,
                errors: ['time']
            })
            .then(async(collected) => {
                backphoto = collected.first().attachments.first().url;

这是我尝试对 Jimp 执行的操作,但我的程序遇到内存泄漏并且无法发布照片。上传的两张照片的尺寸都是 h:4032 和 w:3024,所以最终的图像尺寸应该是 h:4032 和 w:6048。

let h, w;
let combined_photo;

Jimp.read(frontphoto).then(front => {
   Jimp.read(backphoto).then(back => {
      Jimp.read(<random image backgroun>).then(async(new_image) => {
         h = front.bitmap.height;
         w = (front.bitmap.width) * 2;

         await new_image.resize(w,h);

         console.log(new_image.bitmap.height);
         console.log(new_image.bitmap.width);

         await new_image.composite(front,0,0);
         await new_image.comspostie(back,w/2,0);

         combined_photo = new_image;
      });
   });
});

标签: javascriptnode.jsnpmdiscord.js

解决方案


推荐阅读