const { createCanvas, loadImage } = require('canvas'); async function gura(image) { try { const backgroundImg = await loadImage('https://files.catbox.moe/trfgwb.png'); const inputImg = await loadImage(image); const canvas = createCanvas(backgroundImg.width, backgroundImg.height); const ctx = canvas.getContext('2d'); ctx.drawImage(backgroundImg, 0, 0); const boxX = 395; const boxY = 200; const boxWidth = 310; const boxHeight = 310; const imgAspectRatio = inputImg.width / inputImg.height; let sourceX, sourceY, sourceWidth, sourceHeight; if (imgAspectRatio > 1) { sourceHeight = inputImg.height; sourceWidth = inputImg.height; sourceX = (inputImg.width - sourceWidth) / 2; sourceY = 0; } else { sourceWidth = inputImg.width; sourceHeight = inputImg.width; sourceX = 0; sourceY = (inputImg.height - sourceHeight) / 2; } ctx.drawImage(inputImg, sourceX, sourceY, sourceWidth, sourceHeight, boxX, boxY, boxWidth, boxHeight); return canvas.toBuffer('image/png'); } catch (error) { throw new Error(error.message); } } // Usage: const fs = require('fs'); const resp = await gura(fs.readFileSync('./walogo.jpg')); await fs.writeFileSync('./guraa.png', resp);