Make WordPress Core


Ignore:
Timestamp:
12/20/2015 02:38:34 AM (11 years ago)
Author:
azaozz
Message:

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

Props joemcgill.
Fixes #34955 for trunk.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/media.php

    r35820 r36031  
    10161016        $image_baseurl = trailingslashit( $image_baseurl );
    10171017
    1018         // Calculate the image aspect ratio.
    1019         $image_ratio = $image_height / $image_width;
    1020 
    10211018        /*
    10221019         * Images that have been edited in WordPress after being uploaded will
     
    10551052                }
    10561053
    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'] );
    10601062                } else {
    1061                         $image_ratio_compare = 0;
    1062                 }
    1063 
    1064                 // If the new ratio differs by less than 0.002, use it.
    1065                 if ( abs( $image_ratio - $image_ratio_compare ) < 0.002 ) {
     1063                        $constrained_size = wp_constrain_dimensions( $image['width'], $image['height'], $image_width );
     1064                        $expected_size = array( $image_width, $image_height );
     1065                }
     1066
     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 ) {
    10661069                        // Add the URL, descriptor, and value to the sources array to be returned.
    10671070                        $sources[ $image['width'] ] = array(
Note: See TracChangeset for help on using the changeset viewer.