Skip to content

VectorImageMedium reads the wrong viewBox values for an SVG's size #4347

Description

@onetrev

VectorImageMedium::__construct() falls back to the viewBox with [,$width,$height,] = $size;, which takes min-y and width (indexes 1–2) rather than width and height (2–3).

Repro on 2.2.4 (the same code is on 2.0 and develop): put an SVG with only a viewBox in a page folder and read page.media['a.svg'].get('width').

  • viewBox="0 0 120 40" gives no width or height (expected 120 × 40): min-y "0" fails the $width && $height check.
  • viewBox="0 10 200 100" gives 10 × 200.

Fix: [, , $width, $height] = $size;. Splitting on /[\s,]+/ instead of ' ' would also accept comma-separated viewBoxes, which SVG allows.

Related: the (int) cast reads width="100%" as 100.

Activity

  1. rhukster commented on Oct 6, 2026

    @rhukster
    Member

    Thanks @onetrev, great catch, and the repro made it quick to confirm. The viewBox fallback was reading the wrong two numbers, so an SVG with only a viewBox came out with no size or the wrong one.

    This is fixed on develop for Grav 2.2.5. It now takes the width and height from the viewBox correctly, accepts comma-separated viewBoxes too, and no longer treats a relative size like width="100%" as 100 pixels (it uses the viewBox in that case, and sets no size when there isn't one). Tests with real SVG files cover all of these.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions