/** * 生成站点 Open Graph 分享图 public/og-image.png(1200x630)。 * * 手写 PNG 编码器(node:zlib deflateSync),零第三方依赖、确定性可复现: * - 深蓝渐变背景 + 中央白色菱形 logo 母题 + 星点(固定种子伪随机) * * 用法: node scripts/generate-og.mjs */ import { deflateSync } from 'node:zlib' import { writeFileSync } from 'node:fs' const W = 1200 const H = 630 // ---------- PNG 编码工具 ---------- let crcTable = null function crc32(buf) { if (!crcTable) { crcTable = new Int32Array(256) for (let n = 0; n < 256; n++) { let c = n for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1 crcTable[n] = c } } let c = 0xffffffff for (let i = 0; i < buf.length; i++) c = crcTable[(c ^ buf[i]) & 0xff] ^ (c >>> 8) return (c ^ 0xffffffff) >>> 0 } function chunk(type, data) { const len = Buffer.alloc(4) len.writeUInt32BE(data.length) const typeBuf = Buffer.from(type, 'ascii') const crcBuf = Buffer.alloc(4) crcBuf.writeUInt32BE(crc32(Buffer.concat([typeBuf, data]))) return Buffer.concat([len, typeBuf, data, crcBuf]) } // ---------- 确定性伪随机(mulberry32)---------- let seed = 20260806 function rand() { seed = (seed + 0x6d2b79f5) | 0 let t = seed t = Math.imul(t ^ (t >>> 15), t | 1) t ^= t + Math.imul(t ^ (t >>> 7), t | 61) return ((t ^ (t >>> 14)) >>> 0) / 4294967296 } // ---------- 像素绘制 ---------- const raw = Buffer.alloc(H * (1 + W * 4)) const lerp = (a, b, t) => Math.round(a + (b - a) * t) const clamp255 = (v) => Math.max(0, Math.min(255, v)) const cTop = [15, 23, 42] // #0f172a const cBottom = [30, 41, 59] // #1e293b const cAccent = [99, 102, 241] // indigo const cx = W / 2 const cy = H / 2 const diamondHalf = 132 // 菱形对角线半长(L1 度量) const diamondEdge = 16 // 菱形边缘柔和过渡像素 // 基础渐变 + 中央径向光晕 + 菱形 for (let y = 0; y < H; y++) { const rowStart = y * (1 + W * 4) raw[rowStart] = 0 // filter: none const t = y / (H - 1) let r = lerp(cTop[0], cBottom[0], t) let g = lerp(cTop[1], cBottom[1], t) let b = lerp(cTop[2], cBottom[2], t) for (let x = 0; x < W; x++) { const dx = x - cx const dy = y - cy const dist = Math.sqrt(dx * dx + dy * dy) const glow = Math.max(0, 1 - dist / 520) let pr = lerp(r, cAccent[0], glow * 0.45) let pg = lerp(g, cAccent[1], glow * 0.45) let pb = lerp(b, cAccent[2], glow * 0.45) const l1 = Math.abs(dx) + Math.abs(dy) if (l1 <= diamondHalf) { const edge = clamp255(Math.max(0, diamondHalf - l1) / diamondEdge) pr = lerp(pr, 255, edge * 0.94) pg = lerp(pg, 255, edge * 0.94) pb = lerp(pb, 255, edge * 0.94) } const off = rowStart + 1 + x * 4 raw[off] = clamp255(pr) raw[off + 1] = clamp255(pg) raw[off + 2] = clamp255(pb) raw[off + 3] = 255 } } // 星点(后画,覆盖在基色上) const stars = [] for (let i = 0; i < 180; i++) { stars.push({ x: rand() * W, y: rand() * H, r: 0.5 + rand() * 2.2, a: 0.12 + rand() * 0.35 }) } for (const s of stars) { const r = s.r + 1 const x0 = Math.max(0, Math.floor(s.x - r)) const x1 = Math.min(W - 1, Math.ceil(s.x + r)) const y0 = Math.max(0, Math.floor(s.y - r)) const y1 = Math.min(H - 1, Math.ceil(s.y + r)) for (let y = y0; y <= y1; y++) { const rowStart = y * (1 + W * 4) for (let x = x0; x <= x1; x++) { if (Math.hypot(s.x - x, s.y - y) > s.r) continue const off = rowStart + 1 + x * 4 raw[off] = clamp255(lerp(raw[off], 255, s.a)) raw[off + 1] = clamp255(lerp(raw[off + 1], 255, s.a)) raw[off + 2] = clamp255(lerp(raw[off + 2], 255, s.a)) } } } // ---------- 组装 PNG ---------- const ihdr = Buffer.alloc(13) ihdr.writeUInt32BE(W, 0) ihdr.writeUInt32BE(H, 4) ihdr[8] = 8 // bit depth ihdr[9] = 6 // color type: RGBA ihdr[10] = 0 // compression ihdr[11] = 0 // filter ihdr[12] = 0 // interlace const png = Buffer.concat([ Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]), chunk('IHDR', ihdr), chunk('IDAT', deflateSync(raw, { level: 9 })), chunk('IEND', Buffer.alloc(0)), ]) writeFileSync('public/og-image.png', png) console.log(`og-image.png generated: ${W}x${H}, ${png.length} bytes`)