SVG Image is broken with the webp pipeline

Created on 16 November 2023, about 1 year ago

Problem/Motivation

I had one media field which allows to upload JPG, PNG's and SVG's at the same time. When I tried to apply the image style with webp converter in place then the SVG's image got broken. As core was unable to convert the SVG to webp.

Steps to reproduce

1. Create one image style and select webp converter effect 'Convert Webp'
2. Create some media or image field (allow uploading various image types along with SVG) and then select this image style there (that was created in step 1 )
3. When SVG image is uploaded and displayed on front-end then it is broken.

Proposed resolution

As SVG is already very light in size, so I belief we can have a setting in place to bypass translating any of images that can not be converted to webp format. SVG is one such example. But having a configuration to specify the fallback rules to restrict (or allow) on some specific formats will make life easy.

Remaining tasks

1. Backend Configuration to allow/disallow certain types of images to be converted.
2. Deliver function need to be updated to consider the configuration to serve the fallback styles accordingly.

User interface changes

1. Backend Configuration should be there for Admin usage to restrict/ allow the webp conversion on some specific types when this effect is selected. Please see the attachment

🐛 Bug report
Status

Active

Version

10.2

Component
Image system 

Last updated 1 day ago

Created by

🇮🇳India shiks

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

Comments & Activities

  • Issue created by @shiks
  • 🇮🇳India nikhil_dawar

    1. Usage of Preprocess Image Hook:

    Implemented a preprocess image hook to address a specific issue.

    2. Call to removeAllQueryParameter Function:

    Within the preprocess image hook, called the removeAllQueryParameter function to remove all query parameters from the URL.

    3. Extension Check and Update:

    Checked the file extension by extracting it from the modified URL.
    4. Condition for SVG Files:

    Added a condition to check if the file extension is 'svg'.

    5. Modification of srcset Attribute:

    If the file is an SVG (Scalable Vector Graphics), updated the value of the srcset attribute with the src attribute.

    In summary, the preprocess image hook is utilized to manipulate image URLs. The removeAllQueryParameter function is employed to eliminate all query parameters. Subsequently, the file extension is checked, and if it is an SVG file, the srcset attribute is adjusted to match the src attribute.

    I have applied code in the .theme file

    function removeAllQueryParameters($url) {
      // Parse the URL into its components.
      $urlParts = parse_url($url);
    
      // Check if there are query parameters.
      if (isset($urlParts['query'])) {
          // Remove all query parameters.
          $urlParts['query'] = '';
    
          // Rebuild the full URL.
          $url = $urlParts['scheme'] . '://' . $urlParts['host'] . $urlParts['path'];
          if (!empty($urlParts['fragment'])) {
              $url .= '#' . $urlParts['fragment'];
          }
      }
    
      return $url;
    }
    
    function hook_preprocess_image(&$variables) {
      $modifiedUrl = removeAllQueryParameters($variables['uri']);
      $modified_file_extension = pathinfo($modifiedUrl, PATHINFO_EXTENSION);
      if ($modified_file_extension == 'svg') {
        $variables['attributes']['srcset'] = $variables['attributes']['src'];
      }
    }
  • 🇩🇪Germany Anybody Porta Westfalica

    Just ran into the same issue. I think the idea to add a specific setting to bypass certain mime types or formats (or displaying that they are skipped and adding this programmatically in ways of "Supported mime types" or something like that.

    Maybe there are related issues for other cases or mime types / file formats already?

Production build 0.71.5 2024