Space in image name breaks srcset

Created on 3 February 2016, almost 9 years ago
Updated 14 September 2023, about 1 year ago

A load of images on our site aren't displaying. It turns out they all have a space in the image filename.

This is the error:

Failed parsing 'srcset' attribute value since it has an unknown descriptor.

I just tried uploading an image with a space in the name and it gets converted to an underscore, so presumably an old version of drupal supported spaces in filenames but doesn't anymore???

Hmmm, well return str_replace(' ', '%20', $url) fixes it but it's not ideal:

function _picture_image_style_url($style_name, $path, $timestamp = NULL) {
  if ($style_name == PICTURE_EMPTY_IMAGE) {
    return 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7';
  }
  if ($style_name == PICTURE_ORIGINAL_IMAGE) {
    $url = file_create_url($path);
  }
  else {
    $url = image_style_url($style_name, $path);
  }

  if (!empty($timestamp)) {
    $url .= (strpos($url, '?') !== FALSE ? '&' : '?') . drupal_http_build_query(array('timestamp' => $timestamp));
  }
  return str_replace(' ', '%20', $url);
}
🐛 Bug report
Status

Fixed

Version

2.13

Component

Code

Created by

🇳🇿New Zealand sphism

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

Comments & Activities

Not all content is available!

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

Production build 0.71.5 2024