Files
NianAIGC/lib/server/mock-image.ts
2026-05-29 10:26:02 +08:00

39 lines
2.0 KiB
TypeScript

import { createHash } from "node:crypto";
export function createMockImageBuffer(input: {
title: string;
prompt?: string;
capability: string;
resolution?: string;
}): Buffer {
const seed = createHash("sha1").update(`${input.capability}:${input.prompt || ""}`).digest("hex");
const colorA = `#${seed.slice(0, 6)}`;
const colorB = `#${seed.slice(6, 12)}`;
const colorC = `#${seed.slice(12, 18)}`;
const prompt = escapeXml(shorten(input.prompt || input.title, 96));
const subtitle = escapeXml(input.resolution ? `${input.capability} / ${input.resolution}` : input.capability);
const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="1536" height="1024" viewBox="0 0 1536 1024">
<rect width="1536" height="1024" fill="#f8faf7"/>
<rect x="88" y="88" width="1360" height="848" rx="20" fill="${colorA}" opacity="0.92"/>
<path d="M88 782 C320 620 452 942 676 760 C915 565 1058 785 1448 530 L1448 936 L88 936 Z" fill="${colorB}" opacity="0.72"/>
<path d="M90 230 C282 151 450 328 660 224 C870 120 1060 170 1446 104 L1446 366 C1080 455 880 326 682 426 C480 528 282 408 90 508 Z" fill="${colorC}" opacity="0.56"/>
<circle cx="1218" cy="276" r="106" fill="#ffffff" opacity="0.78"/>
<text x="144" y="720" font-family="Arial, 'PingFang SC', sans-serif" font-size="62" font-weight="700" fill="#ffffff">${prompt}</text>
<text x="144" y="792" font-family="Arial, 'PingFang SC', sans-serif" font-size="30" fill="#ffffff" opacity="0.86">${subtitle}</text>
<text x="144" y="856" font-family="Arial, 'PingFang SC', sans-serif" font-size="24" fill="#ffffff" opacity="0.72">Mock result: configure Volcengine credentials to call Jimeng Visual API.</text>
</svg>`;
return Buffer.from(svg);
}
function shorten(value: string, max: number): string {
return value.length > max ? `${value.slice(0, max - 1)}...` : value;
}
function escapeXml(value: string): string {
return value
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;");
}