/** * 轮播配图(白色插画)生成器 * ------------------------------------------------------------ * 与 gen-icons.mjs 完全同一套链路:同一份 SVG 渲染「白底」「黑底」两遍 → * 两式联立解出真实 alpha → contentBox 定位内容区 → 2× 降采样 → 192×192 真实透明 PNG。 * 运行: node tools/gen-banner-art.mjs */ import fs from 'node:fs' import path from 'node:path' import os from 'node:os' import url from 'node:url' import { execFileSync } from 'node:child_process' import { BANNER_ART, buildBannerArtSvg } from './banner-art.mjs' import { decodePng, encodePng, extractAlpha, transparentRatio, contentBox, crop, downscale } from './png-util.mjs' const ROOT = path.resolve(path.dirname(url.fileURLToPath(import.meta.url)), '..') const OUT_DIR = path.join(ROOT, 'static', 'banners') const SIZE = 384 // 渲染分辨率(96 坐标系 → 4×) const TARGET = 192 // 最终输出边长 const TMP = fs.mkdtempSync(path.join(os.tmpdir(), 'fc-bannerart-')) const SRC = path.join(TMP, 'src') const RST = path.join(TMP, 'out') fs.mkdirSync(SRC, { recursive: true }) fs.mkdirSync(RST, { recursive: true }) fs.mkdirSync(OUT_DIR, { recursive: true }) /* 1. 两版 SVG */ const names = [] for (const def of BANNER_ART) { const base = def.file.replace(/\.png$/, '') fs.writeFileSync(path.join(SRC, `${base}__w.svg`), buildBannerArtSvg(def, '#FFFFFF'), 'utf8') fs.writeFileSync(path.join(SRC, `${base}__b.svg`), buildBannerArtSvg(def, '#000000'), 'utf8') names.push(base) } /* 2. 渲染(qlmanage 会把内容贴到正方形画布上,其余补白,用 contentBox 裁掉) */ try { execFileSync( 'qlmanage', ['-t', '-s', String(SIZE), '-o', RST, ...names.flatMap((n) => [path.join(SRC, `${n}__w.svg`), path.join(SRC, `${n}__b.svg`)])], { stdio: 'ignore' }, ) } catch (e) { /* 忽略,后续按产物校验 */ } /* 3. 反解 alpha -> 透明 PNG */ let ok = 0 const missing = [] const lowAlpha = [] const sizeSet = new Set() for (const n of names) { const fw = path.join(RST, `${n}__w.svg.png`) const fb = path.join(RST, `${n}__b.svg.png`) if (!fs.existsSync(fw) || !fs.existsSync(fb)) { missing.push(n) continue } const black = decodePng(fs.readFileSync(fb)) const box = contentBox(black) let img = extractAlpha(crop(decodePng(fs.readFileSync(fw)), box), crop(black, box)) img = downscale(img, Math.max(1, Math.floor(img.width / TARGET))) const ratio = transparentRatio(img) if (ratio < 0.1) lowAlpha.push(`${n}(${ratio.toFixed(2)})`) fs.writeFileSync(path.join(OUT_DIR, `${n}.png`), encodePng(img)) sizeSet.add(`${img.width}×${img.height}`) ok++ } fs.rmSync(TMP, { recursive: true, force: true }) const bytes = fs .readdirSync(OUT_DIR) .filter((f) => f.endsWith('.png')) .reduce((s, f) => s + fs.statSync(path.join(OUT_DIR, f)).size, 0) console.log(`✅ 轮播配图生成完成:${ok}/${names.length} 张 → static/banners/(${[...sizeSet].join(', ')},真实透明,合计 ${(bytes / 1024).toFixed(1)}KB)`) if (missing.length) console.log(`⚠️ 未生成:${missing.join(', ')}`) if (lowAlpha.length) console.log(`⚠️ 透明区域异常:${lowAlpha.join(', ')}`) if (missing.length) process.exitCode = 1