70 lines
1.9 KiB
JavaScript
70 lines
1.9 KiB
JavaScript
const { createCanvas, loadImage } = require("canvas");
|
|
const fs = require("fs");
|
|
|
|
/**
|
|
*
|
|
* @param {Array} images The array of images to be merged together
|
|
* @param {function} callback The callback function to call with the merged images
|
|
* @returns {*} A PNG encoded image
|
|
*/
|
|
exports.mergeImages = async (images, callback) => {
|
|
if (!images) return callback(new Error("No images provided to merge"), undefined);
|
|
const numberOfImages = images.length;
|
|
const finalImageWidth = 1024;
|
|
const finalImageHeight = 1024;
|
|
|
|
switch(numberOfImages) {
|
|
case 1:
|
|
break;
|
|
case 2:
|
|
break;
|
|
case 3:
|
|
break;
|
|
case 4:
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
|
|
const imagePosition = {
|
|
w: 400,
|
|
h: 88,
|
|
x: 400,
|
|
// Calculate the Y of the image based on the number of
|
|
// lines in the title.
|
|
y: titleText.length === 2 ? 75 : 100,
|
|
};
|
|
|
|
|
|
}
|
|
|
|
// Do the same with the title's Y value.
|
|
const titleY = titleText.length === 2 ? 300 : 350;
|
|
const titleLineHeight = 100;
|
|
// And the author's Y value.
|
|
const authorY = titleText.length === 2 ? 525 : 500;
|
|
|
|
const canvas = createCanvas(width, height);
|
|
const context = canvas.getContext("2d");
|
|
|
|
context.fillStyle = "#764abc";
|
|
context.fillRect(0, 0, width, height);
|
|
|
|
context.font = "bold 70pt 'PT Sans'";
|
|
context.textAlign = "center";
|
|
context.fillStyle = "#fff";
|
|
|
|
context.fillText(titleText[0], 600, titleY);
|
|
if (titleText[1]) context.fillText(titleText[1], 600, titleY + titleLineHeight);
|
|
|
|
context.font = "40pt 'PT Sans'";
|
|
context.fillText(`by ${post.author}`, 600, authorY);
|
|
|
|
await loadImage("./assets/logo.png").then((image) => {
|
|
const { w, h, x, y } = imagePosition;
|
|
context.drawImage(image, x, y, w, h);
|
|
});
|
|
|
|
const buffer = canvas.toBuffer("image/png");
|
|
fs.writeFileSync("./image.png", buffer); |