From 6e49a963b174ad8a509c977ea571afdeca1fe298 Mon Sep 17 00:00:00 2001 From: Andrew Ozz Date: Sun, 20 Dec 2015 02:39:32 +0000 Subject: [PATCH] Responsive images: fix calculations when determining whether to include particular image file in `srcset`. Props joemcgill. Fixes #34955 for trunk. Built from https://develop.svn.wordpress.org/trunk@36031 git-svn-id: http://core.svn.wordpress.org/trunk@35996 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/media.php | 21 ++++++++++++--------- wp-includes/version.php | 2 +- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/wp-includes/media.php b/wp-includes/media.php index 89b88df7b4..9d4025d263 100644 --- a/wp-includes/media.php +++ b/wp-includes/media.php @@ -1015,9 +1015,6 @@ function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attac $image_baseurl = trailingslashit( $image_baseurl ); - // Calculate the image aspect ratio. - $image_ratio = $image_height / $image_width; - /* * Images that have been edited in WordPress after being uploaded will * contain a unique hash. Look for that hash and use it later to filter @@ -1054,15 +1051,21 @@ function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attac continue; } - // Calculate the new image ratio. - if ( $image['width'] ) { - $image_ratio_compare = $image['height'] / $image['width']; + /** + * To check for varying crops, we calculate the expected size of the smaller + * image if the larger were constrained by the width of the smaller and then + * see if it matches what we're expecting. + */ + if ( $image_width > $image['width'] ) { + $constrained_size = wp_constrain_dimensions( $image_width, $image_height, $image['width'] ); + $expected_size = array( $image['width'], $image['height'] ); } else { - $image_ratio_compare = 0; + $constrained_size = wp_constrain_dimensions( $image['width'], $image['height'], $image_width ); + $expected_size = array( $image_width, $image_height ); } - // If the new ratio differs by less than 0.002, use it. - if ( abs( $image_ratio - $image_ratio_compare ) < 0.002 ) { + // If the image dimensions are within 1px of the expected size, use it. + if ( abs( $constrained_size[0] - $expected_size[0] ) <= 1 && abs( $constrained_size[1] - $expected_size[1] ) <= 1 ) { // Add the URL, descriptor, and value to the sources array to be returned. $sources[ $image['width'] ] = array( 'url' => $image_baseurl . $image['file'], diff --git a/wp-includes/version.php b/wp-includes/version.php index c04bd798c6..83e91f57aa 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.5-alpha-36030'; +$wp_version = '4.5-alpha-36031'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.