// 企业微信资产包(第五十二刀) // 两类载体,两套判据: // 头像 —— 企业微信显示为圆形裁切 ⇒ 必须为圆设计(avatar-standard / avatar-ceremonial 已是圆面) // 企业logo —— 管理后台固定板 702×180(口径为二手转述,见 WECOM-MANIFEST.md 的存疑条) // 栅格化用仓库自带的 sharp;尺寸一律由 viewBox 反推 density,不靠"看着调"。 const fs = require("fs"); const path = require("path"); const sharp = require("sharp"); const HERE = __dirname; const REPO = path.dirname(path.dirname(HERE)); const D = path.join(HERE, "assets-delivery"); const OUT = path.join(HERE, "assets-wecom"); const AVATARS = { full: path.join(D, "avatar-standard.svg"), ring2: path.join(D, "avatar-ceremonial.svg"), }; const LOGOS = { stacked: { ink: path.join(REPO, "public/logo.svg"), reversed: path.join(REPO, "public/logo-white.svg") }, name: { ink: path.join(D, "name-alone.svg"), reversed: path.join(D, "name-alone-reversed.svg") }, }; const PLATE = { w: 702, h: 180 }; const PAD = 0.86; // 留白版:板内四周各内缩 7% 板高(13px)后再装墨 function viewBox(file) { const m = /viewBox="([^"]+)"/.exec(fs.readFileSync(file, "utf8")); if (!m) throw new Error(`${path.basename(file)} 没有 viewBox`); const [, , w, h] = m[1].split(/\s+/).map(Number); if (!(w > 0 && h > 0)) throw new Error(`${path.basename(file)} viewBox 非法: ${m[1]}`); return { w, h }; } // 纯路径校验:交付件不许有 ,不许引外部字体 // ⚠ 先剥 XML 注释 —— 这些文件的来源注释里就写着「现网那份用 」,不剥会把判据读成自伤 function assertPurePaths(file) { const s = fs.readFileSync(file, "utf8").replace(//g, ""); if (/`); if (/https?:\/\//.test(s.replace(/xmlns="[^"]*"/g, ""))) throw new Error(`${path.basename(file)} 含外链`); } async function raster(svg, targetH) { const vb = viewBox(svg); const density = (72 * targetH) / vb.h; // 1 用户单位 = targetH/vb.h 设备像素 // ⚠ sharp.resize(宽, 高):targetH 是**高**,别把它当宽传进去(上一版就是这么把墨块排成 180×384 的) return sharp(svg, { density }).resize(Math.round((targetH * vb.w) / vb.h), targetH, { fit: "fill" }).png().toBuffer(); } // 按**墨盒**居中,不按画布盒。实测这些字标的 viewBox 上下留了空盒: // name-alone 画布高 96 而墨只 66 单位,stacked 画布 119.82 而墨 100 上下各空 —— 不裁就直接偏心。 async function trimInk(buf) { const { data, info } = await sharp(buf).ensureAlpha().raw().toBuffer({ resolveWithObject: true }); const { width: w, height: h } = info; let x0 = w, y0 = h, x1 = -1, y1 = -1; for (let y = 0; y < h; y++) { for (let x = 0; x < w; x++) { if (data[(y * w + x) * 4 + 3] > 8) { if (x < x0) x0 = x; if (x > x1) x1 = x; if (y < y0) y0 = y; if (y > y1) y1 = y; } } } if (x1 < x0) throw new Error("一个不透明像素都没有"); const meta = await sharp(buf).metadata(); const slack = { top: y0, bottom: meta.height - 1 - y1, left: x0, right: meta.width - 1 - x1 }; const cropped = await sharp(buf).extract({ left: x0, top: y0, width: x1 - x0 + 1, height: y1 - y0 + 1 }).png().toBuffer(); return { buf: cropped, slack }; } // 先高倍栅格 + 裁墨,再把**墨盒**装进板内最大缩放 —— 两个理由: // ① 这些字标的 viewBox 上下带空盒(实测 stacked 墨只占画布高 73%、name 52%),按画布排等于缩得不够; // ② name 的墨宽高比实测 4.50,比 702×180 的板(3.90)还扁 ⇒ 它只能按**宽**限死,按高排必溢出。 async function inkFit(svg, boxW, boxH) { const probe = 720; const { buf: trimmed } = await trimInk(await raster(svg, probe)); const m = await sharp(trimmed).metadata(); const sW = boxW / m.width; const sH = boxH / m.height; const s = Math.min(sW, sH); const w = Math.round(m.width * s); const h = Math.round(m.height * s); const buf = await sharp(trimmed).resize(w, h, { fit: "fill" }).png().toBuffer(); return { buf, limit: sW < sH ? "width" : "height", inkOverCanvas: m.height / probe, aspect: m.width / m.height }; } async function plate(inkBuf, bg) { const m = await sharp(inkBuf).metadata(); const left = Math.round((PLATE.w - m.width) / 2); const top = Math.round((PLATE.h - m.height) / 2); const buf = await sharp({ create: { width: PLATE.w, height: PLATE.h, channels: 4, background: bg } }) .composite([{ input: inkBuf, left, top }]) .png() .toBuffer(); return { buf, ink: { w: m.width, h: m.height }, pad: { l: left, r: PLATE.w - m.width - left, t: top, b: PLATE.h - m.height - top } }; } const WHITE_BG = { r: 255, g: 255, b: 255, alpha: 1 }; const CLEAR = { r: 0, g: 0, b: 0, alpha: 0 }; (async () => { fs.mkdirSync(OUT, { recursive: true }); const report = { plate: PLATE, pad: PAD, files: {} }; for (const [kind, svg] of Object.entries(AVATARS)) { assertPurePaths(svg); for (const px of [512, 144]) { const name = `wecom-avatar-${kind}-${px}.png`; const buf = await raster(svg, px); await sharp(buf).resize(px, px, { fit: "fill" }).png().toFile(path.join(OUT, name)); report.files[name] = { source: path.relative(HERE, svg), size: `${px}x${px}`, kind: "avatar" }; } } for (const [variant, src] of Object.entries(LOGOS)) { assertPurePaths(src.ink); assertPurePaths(src.reversed); const padPx = Math.round(PLATE.h * ((1 - PAD) / 2)); const boxes = { full: { w: PLATE.w, h: PLATE.h }, pad: { w: PLATE.w - 2 * padPx, h: PLATE.h - 2 * padPx } }; for (const [fit, box] of Object.entries(boxes)) { const suffix = fit === "full" ? "" : "-pad"; const jobs = [ [`wecom-logo-${variant}${suffix}-${PLATE.w}x${PLATE.h}-white.png`, src.ink, WHITE_BG], [`wecom-logo-${variant}${suffix}-${PLATE.w}x${PLATE.h}-transparent.png`, src.ink, CLEAR], [`wecom-logo-${variant}${suffix}-${PLATE.w}x${PLATE.h}-reversed.png`, src.reversed, CLEAR], ]; for (const [name, svg, bg] of jobs) { const { buf: inkBuf, limit, aspect, inkOverCanvas } = await inkFit(svg, box.w, box.h); const { buf, ink, pad } = await plate(inkBuf, bg); await fs.writeFileSync(path.join(OUT, name), buf); report.files[name] = { source: path.relative(HERE, svg), size: `${PLATE.w}x${PLATE.h}`, kind: "plate", ink, pad, limit, inkAspect: Number(aspect.toFixed(3)), inkOverCanvas: Number(inkOverCanvas.toFixed(3)), }; } if (fit === "full") { // @2x 高清备份(只出白底板,上传框按 702×180 用) const big = { w: PLATE.w * 2, h: PLATE.h * 2 }; const { buf: trimmed } = await inkFit(src.ink, big.w, big.h); const m = await sharp(trimmed).metadata(); const name = `wecom-logo-${variant}-2x-${big.w}x${big.h}-white.png`; const left = Math.floor((big.w - m.width) / 2); const top = Math.floor((big.h - m.height) / 2); const buf = await sharp({ create: { width: big.w, height: big.h, channels: 4, background: WHITE_BG } }) .composite([{ input: trimmed, left, top }]) .png() .toBuffer(); await fs.writeFileSync(path.join(OUT, name), buf); report.files[name] = { source: path.relative(HERE, src.ink), size: `${big.w}x${big.h}`, kind: "plate", ink: { w: m.width, h: m.height }, pad: { l: left, r: big.w - m.width - left, t: top, b: big.h - m.height - top }, }; } } } report.avatars = Object.keys(AVATARS); fs.writeFileSync(path.join(OUT, "pack-report.json"), JSON.stringify(report, null, 2)); const names = Object.keys(report.files).sort(); console.log(`生成 ${names.length} 张 → assets-wecom/`); for (const n of names) { const f = report.files[n]; const kb = (fs.statSync(path.join(OUT, n)).size / 1024).toFixed(1); console.log( ` ${n} ${f.size} ${kb}KB` + (f.ink ? ` 墨${f.ink.w}x${f.ink.h} 留白 l${f.pad.l}/${f.pad.r} t${f.pad.t}/${f.pad.b}` : "") + (f.inkOverCanvas ? ` 墨/画布 ${f.inkOverCanvas} 墨宽高比 ${f.inkAspect}` : "")+(f.limit ? ` 限${f.limit}` : "") ); } })().catch((e) => { console.error("FAIL", e.message); process.exit(1); });