Discord.js canvas Error: Couldn't load image using merge-images

I’m trying to make an image using canvas and merge-images but i get error “Error: Couldn’t load image”

const mergeImages = require('merge-images')
const { Canvas, Image } = require('canvas');

module.exports = {
	name: 'avatar',
	description: 'avatar',
	async execute(message, args) {
		
        let items = [];
        items = ['../../assets/colors/yellow.png']

        let b64 = await mergeImages(items, { Canvas: Canvas, Image: Image });
        b64 = b64.split(';base64,').pop();
        await message.channel.send({ files: [{ attachment: Buffer.from(b64, 'base64'), name: `avatar.png` }] });
	}
}

the “yellow.png” file is located here

yellow.png

Hi. I don’t understand why you need this module. In canvas, you can superimpose images on top of each other without using additional modules.

const { createCanvas, loadImage } = require(“canvas”);

module.exports = {
name: ‘avatar’,
description: ‘avatar’,
async execute(message, args) {

        if (!args[0]) return message.reply('you didn’t provide a color!');

        let color;

        if (!args[0].startsWith("#")) color = `#${args[0]}`;
        else color = args[0];

        if (color.length != 7) return message.reply('wrong color format! Color example: #ff0000');

        let canvas = new createCanvas(600, 600);
        let ctx = canvas.getContext('2d', { alpha: true });

        let avatarImage = await loadImage(member.user.displayAvatarURL({ format: 'png', dynamic: false }));
        ctx.drawImage(avatarImage, 0, 0, 600, 600);

        ctx.beginPath();
        ctx.globalAlpha = 0.5;  // transparency
        ctx.fillStyle = color;    // color

        ctx.moveTo(0, 0);
        ctx.lineTo(600, 0);
        ctx.lineTo(600, 600);
        ctx.lineTo(0, 600);
        ctx.lineTo(0, 0);
        ctx.fill(); 

        const attachment = new MessageAttachment(canvas.toBuffer(), "avatar.png");

        await message.channel.send(attachment);
}

}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.