Make WordPress Core

Changeset 36031


Ignore:
Timestamp:
12/20/2015 02:38:34 AM (8 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.

Location:
trunk
Files:
2 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(
  • trunk/tests/phpunit/tests/media.php

    r35820 r36031  
    890890        // The srcset should be false.
    891891        $this->assertFalse( $srcset );
     892    }
     893
     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 ) );
    892936    }
    893937
Note: See TracChangeset for help on using the changeset viewer.