phpdftk API Documentation

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="..." and height="...": 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 no width/height attributes are present.
  • When width and viewBox are both present (but height is absent), the height is derived as width / ratio. Vice versa for height-only + viewBox.

What this parser does NOT do:

  • Parse <style> blocks for svg { width: ... } (cascade dimensions are not "intrinsic" per CSS Images 3 §3.1).
  • Resolve em / rem / vw / vh units. 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

parse()  : ImageInfo
parseFile()  : ImageInfo

Methods


        
On this page

Search results