Update image references to use WebP format for improved performance and remove unused design candidates from the project. Adjusted placeholder image handling in the faculty import script and updated metadata for various pages to reflect the new image paths.
This commit is contained in:
1 parent
5b0a394216
commit
05b0a1af72
94 files changed
+181
-36
No files matched your search
@@ -0,0 +1,143 @@
|
||||
/**
|
||||
* Convert fixed static assets (brand/logos) to compact WebP.
|
||||
* Skips course uploads, faculty photos (except placeholder), and favicon PNGs.
|
||||
*/
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import sharp from "sharp";
|
||||
|
||||
const ROOT = process.cwd();
|
||||
|
||||
const SKIP_DIR_PARTS = [
|
||||
path.join("public", "images", "courses"),
|
||||
path.join("public", "brand", "icons"),
|
||||
];
|
||||
|
||||
const SKIP_FILES = new Set(
|
||||
[
|
||||
"public/icon-192.png",
|
||||
"public/icon-512.png",
|
||||
"public/apple-touch-icon.png",
|
||||
"src/app/icon.png",
|
||||
"src/app/apple-icon.png",
|
||||
].map((p) => path.normalize(p)),
|
||||
);
|
||||
|
||||
const LOGO_NAMES = new Set([
|
||||
"mst-faculty.png",
|
||||
"mst-faculty-icon.png",
|
||||
"mst-faculty-full-with-text.png",
|
||||
"MstAutIcon_horizontal.png",
|
||||
"MstAutIcon.png",
|
||||
"logo_fa.png",
|
||||
"logo_fa_w.png",
|
||||
"amirkabir.png",
|
||||
"amirkabir-black-bg.png",
|
||||
]);
|
||||
|
||||
function walk(dir, acc = []) {
|
||||
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
||||
const full = path.join(dir, entry.name);
|
||||
if (entry.isDirectory()) walk(full, acc);
|
||||
else if (/\.(png|jpe?g)$/i.test(entry.name)) acc.push(full);
|
||||
}
|
||||
return acc;
|
||||
}
|
||||
|
||||
function shouldSkip(abs) {
|
||||
const rel = path.relative(ROOT, abs);
|
||||
const norm = path.normalize(rel);
|
||||
if (SKIP_FILES.has(norm)) return true;
|
||||
if (SKIP_DIR_PARTS.some((d) => norm.startsWith(d))) return true;
|
||||
// Faculty photos from admin/sync — keep. Convert only the fixed placeholder.
|
||||
if (norm.startsWith(path.join("public", "images", "faculty"))) {
|
||||
return path.basename(norm) !== "placeholder.png";
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function presetFor(rel, meta) {
|
||||
const base = path.basename(rel);
|
||||
const isLogo = LOGO_NAMES.has(base);
|
||||
const isHorizontal = base === "MstAutIcon_horizontal.png";
|
||||
const isPlaceholder = base === "placeholder.png";
|
||||
|
||||
if (isHorizontal) {
|
||||
return { lossless: true };
|
||||
}
|
||||
if (isLogo) {
|
||||
return { lossless: true };
|
||||
}
|
||||
if (isPlaceholder) {
|
||||
return { maxEdge: 640, quality: 58, alphaQuality: 70 };
|
||||
}
|
||||
// Decorative / hero illustrations (often under overlays)
|
||||
return { maxEdge: 1080, quality: 48, alphaQuality: 65 };
|
||||
}
|
||||
|
||||
function destPath(abs) {
|
||||
return abs.replace(/\.(png|jpe?g)$/i, ".webp");
|
||||
}
|
||||
|
||||
const candidates = [
|
||||
...walk(path.join(ROOT, "public", "brand")),
|
||||
...walk(path.join(ROOT, "public", "CandidatesIMages")),
|
||||
path.join(ROOT, "public", "MstAutIcon_horizontal.png"),
|
||||
path.join(ROOT, "public", "MstAutIcon.png"),
|
||||
path.join(ROOT, "public", "images", "faculty", "placeholder.png"),
|
||||
].filter((f) => fs.existsSync(f) && !shouldSkip(f));
|
||||
|
||||
let before = 0;
|
||||
let after = 0;
|
||||
|
||||
for (const abs of candidates) {
|
||||
const rel = path.relative(ROOT, abs);
|
||||
const srcSize = fs.statSync(abs).size;
|
||||
before += srcSize;
|
||||
|
||||
const img = sharp(abs);
|
||||
const meta = await img.metadata();
|
||||
const cfg = presetFor(rel, meta);
|
||||
let pipeline = img;
|
||||
|
||||
const w = meta.width ?? 0;
|
||||
const h = meta.height ?? 0;
|
||||
if (cfg.maxHeight && h > cfg.maxHeight) {
|
||||
pipeline = pipeline.resize({ height: cfg.maxHeight, withoutEnlargement: true });
|
||||
} else if (cfg.maxEdge) {
|
||||
const edge = Math.max(w, h);
|
||||
if (edge > cfg.maxEdge) {
|
||||
pipeline = pipeline.resize({
|
||||
width: w >= h ? cfg.maxEdge : undefined,
|
||||
height: h > w ? cfg.maxEdge : undefined,
|
||||
withoutEnlargement: true,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const out = destPath(abs);
|
||||
await pipeline
|
||||
.webp(
|
||||
cfg.lossless
|
||||
? { lossless: true, effort: 6 }
|
||||
: {
|
||||
quality: cfg.quality,
|
||||
alphaQuality: cfg.alphaQuality,
|
||||
effort: 6,
|
||||
smartSubsample: true,
|
||||
},
|
||||
)
|
||||
.toFile(out);
|
||||
|
||||
const outSize = fs.statSync(out).size;
|
||||
after += outSize;
|
||||
fs.unlinkSync(abs);
|
||||
const pct = ((1 - outSize / srcSize) * 100).toFixed(0);
|
||||
console.log(
|
||||
`${(srcSize / 1024).toFixed(0).padStart(6)}KB → ${(outSize / 1024).toFixed(0).padStart(5)}KB (-${pct}%) ${rel}`,
|
||||
);
|
||||
}
|
||||
|
||||
console.log(
|
||||
`\nTOTAL ${(before / 1024 / 1024).toFixed(2)} MB → ${(after / 1024 / 1024).toFixed(2)} MB (${candidates.length} files)`,
|
||||
);
|
||||
Reference in new issue
Block a user