scan-svg-geom
v1.0.0
Published
Scans SVG files or directories and extracts geometric metadata — width, height, aspect ratio, and orientation, with fallbacks for viewBox parsing.
Maintainers
Readme
scan-svg-geom
Canonical URL:
https://alexstevovich.com/a/scan-svg-geom-nodejs
Software URL:
https://midnightcitylights.com/software/scan-svg-geom-nodejs
A lightweight Node.js utility for scanning SVG files and extracting geometric metadata such as
width, height, aspect ratio, orientation, and filesize.
Includes fallbacks for parsing width, height, and viewBox attributes when explicit dimensions are missing.
Installation
npm install scan-svg-geomExample
import scanSvgGeom from 'scan-svg-geom';
// Scan a single SVG
const single = await scanSvgGeom('./assets/logo.svg', { log: true });
console.log(single);
/*
{
path: "logo.svg",
width: 512,
height: 512,
aspectRatio: "512/512",
orientation: "square",
format: "svg",
filesize: 1342
}
*/
// Scan all SVGs in a directory
const results = await scanSvgGeom('./assets/icons', { log: true });
console.log(results.length, 'SVGs found.');
// Recursive scan
const recursive = await scanSvgGeom('./assets', { recursive: true });Output Structure
{
"path": "icons/sample.svg",
"width": 128,
"height": 256,
"aspectRatio": "128/256",
"orientation": "portrait",
"format": "svg",
"filesize": 1042
}Methods
scanSvgGeom(targetPath, options?)
Scans a single SVG file or a directory of SVGs and returns geometric metadata.
Parameters
| Name | Type | Default | Description |
| -------------------- | ---------- | ------------ | ----------------------------------------------- |
| targetPath | string | — | Path to a file or directory to scan. |
| options.recursive | boolean | false | Whether to recursively traverse subdirectories. |
| options.extensions | string[] | [ ".svg" ] | File extensions to include. |
| options.log | boolean | false | Enables console output for each processed SVG. |
Returns
- If file: An object containing geometry metadata.
- If directory: An array of geometry metadata objects.
Notes
- Automatically parses
<svg width>/<svg height>/viewBoxattributes whenimage-sizecannot infer dimensions. - Provides graceful fallback for incomplete or invalid SVG metadata.
- Works on both local files and nested directories.
License
Licensed under the MIT License.
