diff --git a/core/modules/responsive_image/responsive_image.module b/core/modules/responsive_image/responsive_image.module index ea99ca1..57d8fec 100644 --- a/core/modules/responsive_image/responsive_image.module +++ b/core/modules/responsive_image/responsive_image.module @@ -376,7 +376,7 @@ 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))); } - $srcset[(string) $dimensions['width']] = file_create_url(_responsive_image_image_style_url($image_style_name, $image->getSource())) . ' ' . $dimensions['width'] . 'w'; + $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); } break; @@ -386,7 +386,7 @@ function responsive_image_build_source_attributes(ImageInterface $image, array $ $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. - $srcset[Unicode::substr($multiplier, 0, -1)] = file_create_url(_responsive_image_image_style_url($image_style_mapping['image_mapping'], $image->getSource())) . ' ' . $multiplier; + $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; } } diff --git a/core/modules/responsive_image/src/Tests/ResponsiveImageFieldDisplayTest.php b/core/modules/responsive_image/src/Tests/ResponsiveImageFieldDisplayTest.php index 42f4c47..45a418d 100644 --- a/core/modules/responsive_image/src/Tests/ResponsiveImageFieldDisplayTest.php +++ b/core/modules/responsive_image/src/Tests/ResponsiveImageFieldDisplayTest.php @@ -124,14 +124,19 @@ protected function addTestImageStyleMappings($empty_styles = FALSE) { 'image_mapping_type' => 'image_style', 'image_mapping' => RESPONSIVE_IMAGE_EMPTY_IMAGE, )) + // Test the output with a 1.5x multiplier. + ->addImageStyleMapping('responsive_image_test_module.mobile', '1.5x', array( + 'image_mapping_type' => 'image_style', + 'image_mapping' => 'thumbnail', + )) // Test the output of the 'sizes' attribute. ->addImageStyleMapping('responsive_image_test_module.narrow', '1x', array( 'image_mapping_type' => 'sizes', 'image_mapping' => array( 'sizes' => '(min-width: 700px) 700px, 100vw', 'sizes_image_styles' => array( - 'large' => 'large', - 'medium' => 'medium', + 'large', + 'medium', ), ), )) @@ -159,8 +164,9 @@ protected function doTestResponsiveImageFieldFormatters($scheme, $empty_styles = $node_storage = $this->container->get('entity.manager')->getStorage('node'); $field_name = Unicode::strtolower($this->randomMachineName()); $this->createImageField($field_name, 'article', array('uri_scheme' => $scheme)); - // Create a new node with an image attached. - $test_image = current($this->drupalGetTestFiles('image')); + // Create a new node with an image attached. Make sure we use a large image + // so the scale effects of the image styles always have an effect. + $test_image = current($this->drupalGetTestFiles('image', 39325)); $nid = $this->uploadNodeImage($test_image, $field_name, 'article'); $node_storage->resetCache(array($nid)); $node = $node_storage->load($nid); @@ -170,8 +176,8 @@ protected function doTestResponsiveImageFieldFormatters($scheme, $empty_styles = $image = array( '#theme' => 'image', '#uri' => $image_uri, - '#width' => 40, - '#height' => 20, + '#width' => 360, + '#height' => 240, ); $default_output = str_replace("\n", NULL, drupal_render($image)); $this->assertRaw($default_output, 'Default formatter displaying correctly on full node view.'); @@ -190,8 +196,8 @@ protected function doTestResponsiveImageFieldFormatters($scheme, $empty_styles = $image = array( '#theme' => 'image', '#uri' => $image_uri, - '#width' => 40, - '#height' => 20, + '#width' => 360, + '#height' => 240, ); $default_output = '' . drupal_render($image) . ''; $this->drupalGet('node/' . $nid); @@ -237,6 +243,9 @@ protected function doTestResponsiveImageFieldFormatters($scheme, $empty_styles = $this->assertRaw(''); // Assert the empty image is present. $this->assertRaw('data:image/gif;base64,R0lGODlhAQABAIABAP///wAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=='); + $thumbnail_style = ImageStyle::load('thumbnail'); + // Assert the output of the 'srcset' attribute (small multipliers first). + $this->assertRaw('data:image/gif;base64,R0lGODlhAQABAIABAP///wAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw== 1x, ' . $thumbnail_style->buildUrl($image_uri) . ' 1.5x'); $this->assertRaw('/styles/medium/'); // Assert the output of the breakpoints. $this->assertRaw('media="(min-width: 0px)"'); @@ -244,6 +253,9 @@ protected function doTestResponsiveImageFieldFormatters($scheme, $empty_styles = // Assert the output of the 'sizes' attribute. $this->assertRaw('sizes="(min-width: 700px) 700px, 100vw"'); $this->assertPattern('/media="\(min-width: 560px\)".+?sizes="\(min-width: 700px\) 700px, 100vw"/'); + // Assert the output of the 'srcset' attribute (small images first). + $medium_style = ImageStyle::load('medium'); + $this->assertRaw($medium_style->buildUrl($image_uri) . ' 220w, ' . $large_style->buildUrl($image_uri) . ' 360w'); $this->assertRaw('media="(min-width: 851px)"'); } $this->assertRaw('/styles/large/'); @@ -251,6 +263,7 @@ protected function doTestResponsiveImageFieldFormatters($scheme, $empty_styles = $this->assertTrue(in_array('config:responsive_image.styles.style_one', $cache_tags)); if (!$empty_styles) { $this->assertTrue(in_array('config:image.style.medium', $cache_tags)); + $this->assertTrue(in_array('config:image.style.thumbnail', $cache_tags)); $this->assertRaw('type="image/png"'); } $this->assertTrue(in_array('config:image.style.large', $cache_tags)); diff --git a/core/modules/responsive_image/tests/modules/responsive_image_test_module/responsive_image_test_module.breakpoints.yml b/core/modules/responsive_image/tests/modules/responsive_image_test_module/responsive_image_test_module.breakpoints.yml index 247ab23..0d32372 100644 --- a/core/modules/responsive_image/tests/modules/responsive_image_test_module/responsive_image_test_module.breakpoints.yml +++ b/core/modules/responsive_image/tests/modules/responsive_image_test_module/responsive_image_test_module.breakpoints.yml @@ -4,6 +4,7 @@ responsive_image_test_module.mobile: weight: 0 multipliers: - 1x + - 1.5x - 2x responsive_image_test_module.narrow: label: narrow @@ -11,6 +12,7 @@ responsive_image_test_module.narrow: weight: 1 multipliers: - 1x + - 1.5x - 2x responsive_image_test_module.wide: label: wide @@ -18,4 +20,5 @@ responsive_image_test_module.wide: weight: 2 multipliers: - 1x + - 1.5x - 2x