Research international AI news for article posts
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
import sharp from "sharp";
|
||||
|
||||
function escapeXml(value: string) {
|
||||
return value.replace(/[&<>"']/g, (char) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" })[char]!);
|
||||
}
|
||||
|
||||
function headlineLines(value: string, max = 24): string[] {
|
||||
const words = value.trim().replace(/\s+/g, " ").split(" ");
|
||||
const lines: string[] = [];
|
||||
let line = "";
|
||||
for (const word of words) {
|
||||
const candidate = line ? `${line} ${word}` : word;
|
||||
if (candidate.length > max && line) { lines.push(line); line = word; }
|
||||
else line = candidate;
|
||||
}
|
||||
if (line) lines.push(line);
|
||||
return lines.slice(0, 3);
|
||||
}
|
||||
|
||||
/** Adds a reliable Thai editorial headline after image generation; image models do not typeset Thai safely. */
|
||||
export async function addArticleHeadline(image: Buffer, headline: string, accent = "#0077B6"): Promise<Buffer> {
|
||||
const lines = headlineLines(headline);
|
||||
if (!lines.length) return image;
|
||||
const svg = `<svg width="1024" height="1024" xmlns="http://www.w3.org/2000/svg">
|
||||
<defs><linearGradient id="shade" x1="0" y1="0" x2="0" y2="1"><stop offset="0" stop-color="#101820" stop-opacity="0"/><stop offset="0.28" stop-color="#101820" stop-opacity="0.82"/><stop offset="1" stop-color="#101820" stop-opacity="0.97"/></linearGradient></defs>
|
||||
<rect x="0" y="500" width="1024" height="524" fill="url(#shade)"/>
|
||||
<rect x="64" y="590" width="112" height="9" rx="4" fill="${escapeXml(accent)}"/>
|
||||
<text x="64" y="650" fill="#FFFFFF" font-family="Noto Sans Thai, sans-serif" font-size="29" font-weight="600">AI UPDATE</text>
|
||||
${lines.map((line, index) => `<text x="64" y="${735 + index * 88}" fill="#FFFFFF" font-family="Noto Sans Thai, sans-serif" font-size="64" font-weight="700">${escapeXml(line)}</text>`).join("")}
|
||||
</svg>`;
|
||||
return sharp(image, { limitInputPixels: 40_000_000 }).resize(1024, 1024, { fit: "cover" }).composite([{ input: Buffer.from(svg) }]).jpeg({ quality: 90 }).toBuffer();
|
||||
}
|
||||
Reference in New Issue
Block a user