in src/utils/image.ts [70:110]
export async function createTiledComposite(
imageBuffers: Buffer[],
imageWidth: number = DEFAULT_IMAGE_SIZE,
imageHeight: number = DEFAULT_IMAGE_SIZE
): Promise<Buffer> {
let canvasWidth = imageWidth;
let canvasHeight = imageHeight;
if (imageBuffers.length === 2) {
canvasWidth = imageWidth * 2; // Double the width for two images side by side
canvasHeight = imageHeight; // Height remains the same
} else if (imageBuffers.length === 3 || imageBuffers.length === 4) {
canvasWidth = imageWidth * 2; // Double the width for a 2x2 grid
canvasHeight = imageHeight * 2; // Double the height for a 2x2 grid
}
const images: OverlayOptions[] = imageBuffers.map((buffer, i) => {
let left = (i % 2) * imageWidth; // 0 for quadrant 1 and 3, imageWidth for quadrant 2 and 4
let top = i < 2 ? 0 : imageHeight; // 0 for quadrants 1 and 2, imageHeight for quadrants 3 and 4
return {
input: buffer,
left: left,
top: top,
};
});
// If there are 3 images, the last quadrant should be empty, so no need to put any image there.
return await sharp({
create: {
width: canvasWidth,
height: canvasHeight,
channels: 4,
background: { r: 255, g: 255, b: 255, alpha: 0 },
},
})
.composite(images)
.png()
.toBuffer();
}