Read width and height from viewBox if explicit attributes are not available

Created on 11 August 2021, over 3 years ago
Updated 9 September 2024, 2 months ago

Problem/Motivation

Many SVG files get exported without width and height attributes. This module will then apply a default value of 64 for both attributes.
As setting these attributes on the <img> tag gets more and more important these days, we should make another attempt to retrieve width and height through the viewBox attribute.

Proposed resolution

In svg_image_get_image_file_dimensions(), try to read width and height from the viewBox attribute if the first attempt is not successful.

πŸ“Œ Task
Status

Needs work

Version

3.0

Component

Code

Created by

πŸ‡©πŸ‡ͺGermany mrshowerman Munich

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • First commit to issue fork.
  • πŸ‡©πŸ‡ͺGermany FalkNisius Berlin

    Sorry, that I react late. Thanks for the work.

    My team runs in the issue with missing width and height, and the approach should be changed.

    The actual code prefers the width and height attribute from the svg-tag. Most exporters write 100% to that attributes, and it results to a width and height of 100 pixel, because the code strips the % sign.

    The viewBox attribute is more usual and should be preferred if it exists.

    The code depends on the letter case of the attributes, that is not always given, that viewBox is written as camel case.

    The first two values of viewBox should not be ignored, they can have positive and negative values. The values in the viewBox can be float. We have to build a difference between the right and left corner values. At least the absolute values of the difference should be used.

  • Status changed to Needs work 5 months ago
  • πŸ‡΅πŸ‡±Poland bronismateusz

    I tested patch #4 and it only works when the image style uses crop or scale and crop. Then the values for width and height are actually passed on correctly. If these filters are not applied, then these attributes are missing, for the img field.
    In addition, I also see that the rotate or desaturate filter does not work either.

  • πŸ‡΅πŸ‡±Poland bronismateusz

    I have prepared a patch that fixes the addition of width and height attributes for the other filters in the image style.

  • πŸ‡¨πŸ‡ΏCzech Republic milos.kroulik

    milos.kroulik β†’ made their first commit to this issue’s fork.

  • πŸ‡©πŸ‡ͺGermany mrshowerman Munich

    Left a few notes on the MR.

Production build 0.71.5 2024