42 lines
1.3 KiB
JavaScript
Raw Normal View History

#!/usr/bin/env node
/**
* Simple SVG -> PNG converter using sharp.
* Usage: node svg2png.mjs <input.svg> <output.png> <width> <height>
*/
import fs from "node:fs/promises";
import path from "node:path";
import sharp from "sharp";
async function main() {
const [, , inPath, outPath, wArg, hArg] = process.argv;
if (!inPath || !outPath || !wArg || !hArg) {
console.error("Usage: node svg2png.mjs <input.svg> <output.png> <width> <height>");
process.exit(1);
}
const width = Number(wArg);
const height = Number(hArg);
if (!width || !height) {
console.error("Width and height must be numbers");
process.exit(1);
}
const absIn = path.resolve(inPath);
const absOut = path.resolve(outPath);
const svg = await fs.readFile(absIn);
// Render with background white to avoid transparency issues in slides
const png = await sharp(svg, { density: 300 })
.resize(width, height, { fit: "contain", background: { r: 255, g: 255, b: 255, alpha: 1 } })
.png({ compressionLevel: 9 })
.toBuffer();
await fs.mkdir(path.dirname(absOut), { recursive: true });
await fs.writeFile(absOut, png);
console.log(`Wrote ${absOut} (${width}x${height})`);
}
main().catch(err => {
console.error("svg2png failed:", err?.message || err);
process.exit(1);
});