Responsive images: fix calculations when determining whether to include particular image file in `srcset`.

Props joemcgill.
Fixes #34955 for trunk.

git-svn-id: https://develop.svn.wordpress.org/trunk@36031 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Andrew Ozz 2015-12-20 02:38:34 +00:00
parent c593ceb5e8
commit 58b7d3e136
2 changed files with 56 additions and 9 deletions

View File

@ -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'],

View File

@ -891,6 +891,50 @@ EOF;
$this->assertFalse( $srcset );
}
/**
* @ticket 34955
*/
function test_wp_calculate_image_srcset_ratio_variance() {
// Mock data for this test.
$size_array = array( 218, 300);
$image_src = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-768x1055-218x300.png';
$image_meta = array(
'width' => 768,
'height' => 1055,
'file' => '2015/12/test-768x1055.png',
'sizes' => array(
'thumbnail' => array(
'file' => 'test-768x1055-150x150.png',
'width' => 150,
'height' => 150,
'mime-type' => 'image/png',
),
'medium' => array(
'file' => 'test-768x1055-218x300.png',
'width' => 218,
'height' => 300,
'mime-type' => 'image/png',
),
'custom-600' => array(
'file' => 'test-768x1055-600x824.png',
'width' => 600,
'height' => 824,
'mime-type' => 'image/png',
),
'post-thumbnail' => array(
'file' => 'test-768x1055-768x510.png',
'width' => 768,
'height' => 510,
'mime-type' => 'image/png',
),
),
);
$expected_srcset = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-768x1055-218x300.png 218w, http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-768x1055-600x824.png 600w, http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-768x1055.png 768w';
$this->assertSame( $expected_srcset, wp_calculate_image_srcset( $size_array, $image_src, $image_meta ) );
}
/**
* @ticket 33641
*/