ai-chatbot/lib/canvas/layout-replace-quality-board.ts

23 lines
1.3 KiB
TypeScript

import sharp from "sharp";
export async function makeLayoutReplaceQualityBoard(params: {
template: Buffer; source: Buffer; product: Buffer; result: Buffer;
}) {
const panes = [
{ image: params.template, label: "A TEMPLATE", left: 10, top: 10, width: 360, height: 480 },
{ image: params.source, label: "B SOURCE ORIGINAL", left: 10, top: 540, width: 360, height: 360 },
{ image: params.product, label: "C SOURCE PRODUCT", left: 390, top: 10, width: 990, height: 350 },
{ image: params.result, label: "D FINAL RESULT", left: 390, top: 410, width: 990, height: 940 },
];
const overlays: { input: Buffer; left: number; top: number }[] = [];
for (const pane of panes) {
const label = Buffer.from(`<svg width="${pane.width}" height="32"><rect width="100%" height="100%" fill="#173b70"/><text x="10" y="23" font-size="18" font-family="sans-serif" font-weight="bold" fill="white">${pane.label}</text></svg>`);
const image = await sharp(pane.image, { failOn: "error" }).rotate()
.resize(pane.width, pane.height, { fit: "contain", background: "#ffffff" }).png().toBuffer();
overlays.push({ input: label, left: pane.left, top: pane.top },
{ input: image, left: pane.left, top: pane.top + 32 });
}
return sharp({ create: { width: 1390, height: 1390, channels: 3, background: "#d9d9d9" } })
.composite(overlays).png().toBuffer();
}