SvgParser
in package
FinalYes
Extract intrinsic-size metadata from an SVG file's root `<svg>` element without parsing the full document tree.
Per CSS Images 3 §3 / SVG 2 §6.1, an <svg> element's intrinsic
dimensions come from a small set of root-element attributes:
width="..."andheight="...": explicit intrinsic dimensions when the value is an absolute CSS length (or unitless = px). Percentages ("50%") are not intrinsic and are reported as "no intrinsic value on that axis".viewBox="min-x min-y width height": defines an intrinsic aspect ratio = width / height, even when nowidth/heightattributes are present.- When
widthandviewBoxare both present (butheightis absent), the height is derived aswidth / ratio. Vice versa for height-only + viewBox.
What this parser does NOT do:
- Parse
<style>blocks forsvg { width: ... }(cascade dimensions are not "intrinsic" per CSS Images 3 §3.1). - Resolve
em/rem/vw/vhunits. Those depend on the embedding context; the consumer layout engine has the containing block to resolve them. - Validate the rest of the SVG content.
The returned ImageInfo carries format: 'svg'. The width
and height fields hold the resolved intrinsic pixel dimensions
when both axes are known (either directly via attributes or
computed from one attribute + the viewBox ratio). When neither
intrinsic dimension can be resolved (e.g. viewBox only), both
are zero and ImageInfo::$intrinsicRatio carries the aspect
ratio so the layout can apply the CSS Images 3 §3.3 default
object size fallback.
Table of Contents
Methods
Methods
parse()
public
static parse(string $data) : ImageInfo
Parameters
- $data : string
Return values
ImageInfoparseFile()
public
static parseFile(string $path) : ImageInfo
Parameters
- $path : string