diff --git a/core/modules/responsive_image/responsive_image.module b/core/modules/responsive_image/responsive_image.module index 57d8fec..f7e2192 100644 --- a/core/modules/responsive_image/responsive_image.module +++ b/core/modules/responsive_image/responsive_image.module @@ -376,6 +376,9 @@ function responsive_image_build_source_attributes(ImageInterface $image, array $ if (is_null($dimensions['width'])) { throw new \LogicException(String::format('Could not determine image width for @file using image style with ID: @image_style_name. This image style can not be used for a responsive image style mapping using the \'sizes\' attribute.', array('@file' => $image->getSource(), '@image_style_name' => $image_style_name))); } + // Use the image width as key so we can sort the array later on. + // Images within a srcset should be sorted from small to large, since + // the first matching source will be used. $srcset[intval($dimensions['width'])] = file_create_url(_responsive_image_image_style_url($image_style_name, $image->getSource())) . ' ' . $dimensions['width'] . 'w'; $sizes = array_merge(explode(',', $image_style_mapping['image_mapping']['sizes']), $sizes); } @@ -385,11 +388,16 @@ function responsive_image_build_source_attributes(ImageInterface $image, array $ // Get MIME type. $derivative_mime_type = responsive_image_get_mime_type($image_style_mapping['image_mapping'], $extension); $derivative_mime_types[] = $derivative_mime_type; - // Add the image source with its multiplier. + // Add the image source with its multiplier. Use the multiplier as key + // so we can sort the array later on. Multipliers within a srcset should + // be sorted from small to large, since the first matching source will + // be used. We multiply it by 100 so multipliers with up to two decimals + // can be used. $srcset[intval(Unicode::substr($multiplier, 0, -1) * 100)] = file_create_url(_responsive_image_image_style_url($image_style_mapping['image_mapping'], $image->getSource())) . ' ' . $multiplier; break; } } + // Sort the srcset from small to large image width or multiplier. ksort($srcset); $source_attributes = new \Drupal\Core\Template\Attribute(array( 'srcset' => implode(', ', array_unique($srcset)),