Ticket #34955: 34955.2.diff
File 34955.2.diff, 3.6 KB (added by , 9 years ago) |
---|
-
src/wp-includes/media.php
diff --git src/wp-includes/media.php src/wp-includes/media.php index 89b88df..68fa843 100644
function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attac 1015 1015 1016 1016 $image_baseurl = trailingslashit( $image_baseurl ); 1017 1017 1018 // Calculate the image aspect ratio.1019 $image_ratio = $image_height / $image_width;1020 1021 1018 /* 1022 1019 * Images that have been edited in WordPress after being uploaded will 1023 1020 * contain a unique hash. Look for that hash and use it later to filter … … function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attac 1054 1051 continue; 1055 1052 } 1056 1053 1057 // Calculate the new image ratio. 1058 if ( $image['width'] ) { 1059 $image_ratio_compare = $image['height'] / $image['width']; 1054 /** 1055 * To check for varying crops, we calculate the expected size of the smaller 1056 * image if the larger were constrained by the width of the smaller and then 1057 * see if it matches what we're expecting. 1058 */ 1059 if ( $image_width > $image['width'] ) { 1060 $constrained_size = wp_constrain_dimensions( $image_width, $image_height, $image['width'] ); 1061 $expected_size = array( $image['width'], $image['height'] ); 1060 1062 } else { 1061 $image_ratio_compare = 0; 1063 $constrained_size = wp_constrain_dimensions( $image['width'], $image['height'], $image_width ); 1064 $expected_size = array( $image_width, $image_height ); 1062 1065 } 1063 1066 1064 // If the new ratio differs by less than 0.002, use it.1065 if ( abs( $ image_ratio - $image_ratio_compare ) < 0.002) {1067 // If the image dimensions are within 1px of the expected size, use it. 1068 if ( abs( $constrained_size[0] - $expected_size[0] ) <= 1 && abs( $constrained_size[1] - $expected_size[1] ) <= 1 ) { 1066 1069 // Add the URL, descriptor, and value to the sources array to be returned. 1067 1070 $sources[ $image['width'] ] = array( 1068 1071 'url' => $image_baseurl . $image['file'], -
tests/phpunit/tests/media.php
diff --git tests/phpunit/tests/media.php tests/phpunit/tests/media.php index 3da75a0..d6fcd95 100644
EOF; 892 892 } 893 893 894 894 /** 895 * @ticket 34955 896 */ 897 function test_wp_calculate_image_srcset_ratio_variance() { 898 // Mock data for this test. 899 $size_array = array( 218, 300); 900 $image_src = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-768x1055-218x300.png'; 901 $image_meta = array( 902 'width' => 768, 903 'height' => 1055, 904 'file' => '2015/12/test-768x1055.png', 905 'sizes' => array( 906 'thumbnail' => array( 907 'file' => 'test-768x1055-150x150.png', 908 'width' => 150, 909 'height' => 150, 910 'mime-type' => 'image/png', 911 ), 912 'medium' => array( 913 'file' => 'test-768x1055-218x300.png', 914 'width' => 218, 915 'height' => 300, 916 'mime-type' => 'image/png', 917 ), 918 'custom-600' => array( 919 'file' => 'test-768x1055-600x824.png', 920 'width' => 600, 921 'height' => 824, 922 'mime-type' => 'image/png', 923 ), 924 'post-thumbnail' => array( 925 'file' => 'test-768x1055-768x510.png', 926 'width' => 768, 927 'height' => 510, 928 'mime-type' => 'image/png', 929 ), 930 ), 931 ); 932 933 $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'; 934 935 $this->assertSame( $expected_srcset, wp_calculate_image_srcset( $size_array, $image_src, $image_meta ) ); 936 } 937 938 939 /** 895 940 * @ticket 33641 896 941 */ 897 942 function test_wp_get_attachment_image_srcset() {