Make WordPress Core

Changeset 30660


Ignore:
Timestamp:
11/30/2014 07:53:18 PM (10 years ago)
Author:
wonderboymusic
Message:

Use round() instead of floor() when resizing image dimensions.

Updates unit tests.

Props SergeyBiryukov, kitchin.
Fixes #18532.

Location:
trunk
Files:
5 edited

Legend:

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

    r30656 r30660  
    380380    $larger_ratio  = max( $width_ratio, $height_ratio );
    381381
    382     if ( intval( $current_width * $larger_ratio ) > $max_width || intval( $current_height * $larger_ratio ) > $max_height )
     382    if ( (int) round( $current_width * $larger_ratio ) > $max_width || (int) round( $current_height * $larger_ratio ) > $max_height ) {
    383383        // The larger ratio is too big. It would result in an overflow.
    384384        $ratio = $smaller_ratio;
    385     else
     385    } else {
    386386        // The larger ratio fits, and is likely to be a more "snug" fit.
    387387        $ratio = $larger_ratio;
     388    }
    388389
    389390    // Very small dimensions may result in 0, 1 should be the minimum.
    390     $w = max ( 1, intval( $current_width  * $ratio ) );
    391     $h = max ( 1, intval( $current_height * $ratio ) );
     391    $w = max ( 1, (int) round( $current_width  * $ratio ) );
     392    $h = max ( 1, (int) round( $current_height * $ratio ) );
    392393
    393394    // Sometimes, due to rounding, we'll end up with a result like this: 465x700 in a 177x177 box is 117x176... a pixel short
    394395    // We also have issues with recursive calls resulting in an ever-changing result. Constraining to the result of a constraint should yield the original result.
    395396    // Thus we look for dimensions that are one pixel shy of the max value and bump them up
    396     if ( $did_width && $w == $max_width - 1 )
     397
     398    // Note: $did_width means it is possible $smaller_ratio == $width_ratio.
     399    if ( $did_width && $w == $max_width - 1 ) {
    397400        $w = $max_width; // Round it up
    398     if ( $did_height && $h == $max_height - 1 )
     401    }
     402
     403    // Note: $did_height means it is possible $smaller_ratio == $height_ratio.
     404    if ( $did_height && $h == $max_height - 1 ) {
    399405        $h = $max_height; // Round it up
    400 
    401     return array( $w, $h );
     406    }
     407
     408    return apply_filters( 'wp_constrain_dimensions', array( $w, $h ), $current_width, $current_height, $max_width, $max_height );
    402409}
    403410
     
    460467        $new_h = min($dest_h, $orig_h);
    461468
    462         if ( !$new_w ) {
    463             $new_w = intval($new_h * $aspect_ratio);
    464         }
    465 
    466         if ( !$new_h ) {
    467             $new_h = intval($new_w / $aspect_ratio);
     469        if ( ! $new_w ) {
     470            $new_w = (int) round( $new_h * $aspect_ratio );
     471        }
     472
     473        if ( ! $new_h ) {
     474            $new_h = (int) round( $new_w / $aspect_ratio );
    468475        }
    469476
  • trunk/tests/phpunit/tests/image/editor_gd.php

    r30549 r30660  
    283283            // #0
    284284            array(
    285                 'file'      => 'waffles-10x6.jpg',
     285                'file'      => 'waffles-10x7.jpg',
    286286                'width'     => 10,
    287                 'height'    => 6,
     287                'height'    => 7,
    288288                'mime-type' => 'image/jpeg',
    289289            ),
     
    323323            // #5
    324324            array(
    325                 'file'      => 'waffles-55x36.jpg',
     325                'file'      => 'waffles-55x37.jpg',
    326326                'width'     => 55,
    327                 'height'    => 36,
     327                'height'    => 37,
    328328                'mime-type' => 'image/jpeg',
    329329            ),
     
    331331            // #6
    332332            array(
    333                 'file'      => 'waffles-82x55.jpg',
    334                 'width'     => 82,
     333                'file'      => 'waffles-83x55.jpg',
     334                'width'     => 83,
    335335                'height'    => 55,
    336336                'mime-type' => 'image/jpeg',
  • trunk/tests/phpunit/tests/image/editor_imagick.php

    r30549 r30660  
    283283            // #0
    284284            array(
    285                 'file'      => 'waffles-10x6.jpg',
     285                'file'      => 'waffles-10x7.jpg',
    286286                'width'     => 10,
    287                 'height'    => 6,
     287                'height'    => 7,
    288288                'mime-type' => 'image/jpeg',
    289289            ),
     
    323323            // #5
    324324            array(
    325                 'file'      => 'waffles-55x36.jpg',
     325                'file'      => 'waffles-55x37.jpg',
    326326                'width'     => 55,
    327                 'height'    => 36,
     327                'height'    => 37,
    328328                'mime-type' => 'image/jpeg',
    329329            ),
     
    331331            // #6
    332332            array(
    333                 'file'      => 'waffles-82x55.jpg',
    334                 'width'     => 82,
     333                'file'      => 'waffles-83x55.jpg',
     334                'width'     => 83,
    335335                'height'    => 55,
    336336                'mime-type' => 'image/jpeg',
  • trunk/tests/phpunit/tests/image/resize.php

    r29120 r30660  
    1414        $image = $this->resize_helper( DIR_TESTDATA.'/images/test-image.jpg', 25, 25 );
    1515
    16         $this->assertEquals( 'test-image-25x25.jpg', basename($image) );
     16        $this->assertEquals( 'test-image-25x25.jpg', basename( $image ) );
    1717        list($w, $h, $type) = getimagesize($image);
    1818        $this->assertEquals( 25, $w );
     
    2626        $image = $this->resize_helper( DIR_TESTDATA.'/images/test-image.png', 25, 25 );
    2727
    28         $this->assertEquals( 'test-image-25x25.png', basename($image) );
     28        if ( ! is_string( $image ) ) {  // WP_Error, stop GLib-GObject-CRITICAL assertion
     29            $this->markTestSkipped( sprintf( 'No PNG support in the editor engine %s on this system', $this->editor_engine ) );
     30            return;
     31        }
     32
     33        $this->assertEquals( 'test-image-25x25.png', basename( $image ) );
    2934        list($w, $h, $type) = getimagesize($image);
    3035        $this->assertEquals( 25, $w );
     
    3843        $image = $this->resize_helper( DIR_TESTDATA.'/images/test-image.gif', 25, 25 );
    3944
    40         $this->assertEquals( 'test-image-25x25.gif', basename($image) );
     45        if ( ! is_string( $image ) ) {  // WP_Error, stop GLib-GObject-CRITICAL assertion
     46            $this->markTestSkipped( sprintf( 'No GIF support in the editor engine %s on this system', $this->editor_engine ) );
     47            return;
     48        }
     49
     50        $this->assertEquals( 'test-image-25x25.gif', basename( $image ) );
    4151        list($w, $h, $type) = getimagesize($image);
    4252        $this->assertEquals( 25, $w );
     
    5868        $image = $this->resize_helper( DIR_TESTDATA.'/images/2007-06-17DSC_4173.JPG', 128, 96 );
    5969
    60         $this->assertEquals( '2007-06-17DSC_4173-63x96.jpg', basename($image) );
     70        $this->assertEquals( '2007-06-17DSC_4173-64x96.jpg', basename( $image ) );
    6171        list($w, $h, $type) = getimagesize($image);
    62         $this->assertEquals( 63, $w );
     72        $this->assertEquals( 64, $w );
    6373        $this->assertEquals( 96, $h );
    6474        $this->assertEquals( IMAGETYPE_JPEG, $type );
     
    7080        $image = $this->resize_helper( DIR_TESTDATA.'/images/2007-06-17DSC_4173.JPG', 128, 0 );
    7181
    72         $this->assertEquals( '2007-06-17DSC_4173-128x192.jpg', basename($image) );
     82        $this->assertEquals( '2007-06-17DSC_4173-128x193.jpg', basename( $image ) );
    7383        list($w, $h, $type) = getimagesize($image);
    7484        $this->assertEquals( 128, $w );
    75         $this->assertEquals( 192, $h );
     85        $this->assertEquals( 193, $h );
    7686        $this->assertEquals( IMAGETYPE_JPEG, $type );
    7787
     
    8292        $image = $this->resize_helper( DIR_TESTDATA.'/images/2007-06-17DSC_4173.JPG', 0, 96 );
    8393
    84         $this->assertEquals( '2007-06-17DSC_4173-63x96.jpg', basename($image) );
     94        $this->assertEquals( '2007-06-17DSC_4173-64x96.jpg', basename( $image ) );
    8595        list($w, $h, $type) = getimagesize($image);
    86         $this->assertEquals( 63, $w );
     96        $this->assertEquals( 64, $w );
    8797        $this->assertEquals( 96, $h );
    8898        $this->assertEquals( IMAGETYPE_JPEG, $type );
     
    94104        $image = $this->resize_helper( DIR_TESTDATA.'/images/2007-06-17DSC_4173.JPG', 150, 150, true );
    95105
    96         $this->assertEquals( '2007-06-17DSC_4173-150x150.jpg', basename($image) );
     106        $this->assertEquals( '2007-06-17DSC_4173-150x150.jpg', basename( $image ) );
    97107        list($w, $h, $type) = getimagesize($image);
    98108        $this->assertEquals( 150, $w );
     
    106116        $image = $this->resize_helper( DIR_TESTDATA.'/images/2007-06-17DSC_4173.JPG', 150, 100, true );
    107117
    108         $this->assertEquals( '2007-06-17DSC_4173-150x100.jpg', basename($image) );
     118        $this->assertEquals( '2007-06-17DSC_4173-150x100.jpg', basename( $image ) );
    109119        list($w, $h, $type) = getimagesize($image);
    110120        $this->assertEquals( 150, $w );
     
    118128        $image = $this->resize_helper( DIR_TESTDATA.'/images/2007-06-17DSC_4173.JPG', 50, 150, true );
    119129
    120         $this->assertEquals( '2007-06-17DSC_4173-50x150.jpg', basename($image) );
     130        $this->assertEquals( '2007-06-17DSC_4173-50x150.jpg', basename( $image ) );
    121131        list($w, $h, $type) = getimagesize($image);
    122132        $this->assertEquals( 50, $w );
     
    143153     */
    144154    public function test_resize_bad_image() {
     155
     156        if ( $this->editor_engine == 'WP_Image_Editor_Imagick' ) {
     157            $this->markTestSkipped( sprintf( 'Avoid GLib-GObject-CRITICAL assertion in %s', $this->editor_engine ) );
     158            return;
     159        }
     160
    145161        $image = $this->resize_helper( DIR_TESTDATA.'/export/crazy-cdata.xml', 25, 25 );
    146162        $this->assertInstanceOf( 'WP_Error', $image );
  • trunk/tests/phpunit/tests/image/size.php

    r27358 r30660  
    1414        // no constraint - should have no effect
    1515        $out = wp_constrain_dimensions(640, 480, 0, 0);
    16         $this->assertEquals(array(640, 480), $out);
     16        $this->assertSame( array( 640, 480 ), $out );
    1717
    1818        $out = wp_constrain_dimensions(640, 480);
    19         $this->assertEquals(array(640, 480), $out);
     19        $this->assertSame( array( 640, 480 ), $out );
    2020
    2121        $out = wp_constrain_dimensions(0, 0, 0, 0);
    22         $this->assertEquals(array(0, 0), $out);
     22        $this->assertSame( array( 0, 0 ), $out );
     23
     24        $out = wp_constrain_dimensions(465, 700, 177, 177);
     25        $this->assertSame( array( 118, 177 ), $out );
    2326    }
    2427
     
    2932        // image size is smaller than the constraint - no effect
    3033        $out = wp_constrain_dimensions(500, 600, 1024, 768);
    31         $this->assertEquals(array(500, 600), $out);
     34        $this->assertSame( array( 500, 600 ), $out );
    3235
    3336        $out = wp_constrain_dimensions(500, 600, 0, 768);
    34         $this->assertEquals(array(500, 600), $out);
     37        $this->assertSame( array( 500, 600 ), $out );
    3538
    3639        $out = wp_constrain_dimensions(500, 600, 1024, 0);
    37         $this->assertEquals(array(500, 600), $out);
     40        $this->assertSame( array( 500, 600 ), $out );
    3841    }
    3942
     
    4447        // image size is equal to the constraint - no effect
    4548        $out = wp_constrain_dimensions(1024, 768, 1024, 768);
    46         $this->assertequals(array(1024, 768), $out);
     49        $this->assertSame( array( 1024, 768 ), $out );
    4750
    4851        $out = wp_constrain_dimensions(1024, 768, 0, 768);
    49         $this->assertequals(array(1024, 768), $out);
     52        $this->assertSame( array( 1024, 768 ), $out );
    5053
    5154        $out = wp_constrain_dimensions(1024, 768, 1024, 0);
    52         $this->assertequals(array(1024, 768), $out);
     55        $this->assertSame( array( 1024, 768 ), $out );
    5356    }
    5457
     
    5962        // image size is larger than the constraint - result should be constrained
    6063        $out = wp_constrain_dimensions(1024, 768, 500, 600);
    61         $this->assertequals(array(500, 375), $out);
     64        $this->assertSame( array( 500, 375 ), $out );
    6265
    6366        $out = wp_constrain_dimensions(1024, 768, 0, 600);
    64         $this->assertequals(array(800, 600), $out);
     67        $this->assertSame( array( 800, 600 ), $out );
    6568
    6669        $out = wp_constrain_dimensions(1024, 768, 500, 0);
    67         $this->assertequals(array(500, 375), $out);
     70        $this->assertSame( array( 500, 375 ), $out );
    6871
    6972        // also try a portrait oriented image
    7073        $out = wp_constrain_dimensions(300, 800, 500, 600);
    71         $this->assertequals(array(225, 600), $out);
     74        $this->assertSame( array( 225, 600 ), $out );
    7275
    7376        $out = wp_constrain_dimensions(300, 800, 0, 600);
    74         $this->assertequals(array(225, 600), $out);
     77        $this->assertSame( array( 225, 600 ), $out );
    7578
    7679        $out = wp_constrain_dimensions(300, 800, 200, 0);
    77         $this->assertequals(array(200, 533), $out);
     80        $this->assertSame( array( 200, 533 ), $out );
    7881    }
    7982
     
    8487        // one dimension is larger than the constraint, one smaller - result should be constrained
    8588        $out = wp_constrain_dimensions(1024, 768, 500, 800);
    86         $this->assertequals(array(500, 375), $out);
     89        $this->assertSame( array( 500, 375 ), $out );
    8790
    8891        $out = wp_constrain_dimensions(1024, 768, 2000, 700);
    89         $this->assertequals(array(933, 700), $out);
     92        $this->assertSame( array( 933, 700 ), $out );
    9093
    9194        // portrait
    9295        $out = wp_constrain_dimensions(768, 1024, 800, 500);
    93         $this->assertequals(array(375, 500), $out);
     96        $this->assertSame( array( 375, 500 ), $out );
    9497
    9598        $out = wp_constrain_dimensions(768, 1024, 2000, 700);
    96         $this->assertequals(array(525, 700), $out);
     99        $this->assertSame( array( 525, 700 ), $out );
    97100    }
    98101
     
    102105    function test_shrink_dimensions_default() {
    103106        $out = wp_shrink_dimensions(640, 480);
    104         $this->assertEquals(array(128, 96), $out);
     107        $this->assertSame( array( 128, 96 ), $out );
    105108
    106109        $out = wp_shrink_dimensions(480, 640);
    107         $this->assertEquals(array(72, 96), $out);
     110        $this->assertSame( array( 72, 96 ), $out );
    108111    }
    109112
     
    114117        // image size is smaller than the constraint - no effect
    115118        $out = wp_shrink_dimensions(500, 600, 1024, 768);
    116         $this->assertEquals(array(500, 600), $out);
     119        $this->assertSame( array( 500, 600 ), $out );
    117120
    118121        $out = wp_shrink_dimensions(600, 500, 1024, 768);
    119         $this->assertEquals(array(600, 500), $out);
     122        $this->assertSame( array( 600, 500 ), $out );
    120123    }
    121124
     
    126129        // image size is equal to the constraint - no effect
    127130        $out = wp_shrink_dimensions(500, 600, 500, 600);
    128         $this->assertEquals(array(500, 600), $out);
     131        $this->assertSame( array( 500, 600 ), $out );
    129132
    130133        $out = wp_shrink_dimensions(600, 500, 600, 500);
    131         $this->assertEquals(array(600, 500), $out);
     134        $this->assertSame( array( 600, 500 ), $out );
    132135    }
    133136
     
    138141        // image size is larger than the constraint - result should be constrained
    139142        $out = wp_shrink_dimensions(1024, 768, 500, 600);
    140         $this->assertequals(array(500, 375), $out);
     143        $this->assertSame( array( 500, 375 ), $out );
    141144
    142145        $out = wp_shrink_dimensions(300, 800, 500, 600);
    143         $this->assertequals(array(225, 600), $out);
     146        $this->assertSame( array( 225, 600 ), $out );
    144147    }
    145148
     
    150153        // one dimension is larger than the constraint, one smaller - result should be constrained
    151154        $out = wp_shrink_dimensions(1024, 768, 500, 800);
    152         $this->assertequals(array(500, 375), $out);
     155        $this->assertSame( array( 500, 375 ), $out );
    153156
    154157        $out = wp_shrink_dimensions(1024, 768, 2000, 700);
    155         $this->assertequals(array(933, 700), $out);
     158        $this->assertSame( array( 933, 700 ), $out );
    156159
    157160        // portrait
    158161        $out = wp_shrink_dimensions(768, 1024, 800, 500);
    159         $this->assertequals(array(375, 500), $out);
     162        $this->assertSame( array( 375, 500 ), $out );
    160163
    161164        $out = wp_shrink_dimensions(768, 1024, 2000, 700);
    162         $this->assertequals(array(525, 700), $out);
     165        $this->assertSame( array( 525, 700 ), $out );
    163166    }
    164167
    165168    function test_constrain_size_for_editor_thumb() {
    166169        $out = image_constrain_size_for_editor(600, 400, 'thumb');
    167         $this->assertEquals(array(150, 100), $out);
     170        $this->assertSame( array( 150, 100 ), $out );
    168171
    169172        $out = image_constrain_size_for_editor(64, 64, 'thumb');
    170         $this->assertEquals(array(64, 64), $out);
     173        $this->assertSame( array( 64, 64 ), $out );
    171174    }
    172175
     
    182185
    183186        $out = image_constrain_size_for_editor(600, 400, 'medium');
    184         $this->assertEquals(array(500, 333), $out);
     187        $this->assertSame( array( 500, 333 ), $out );
    185188
    186189        $out = image_constrain_size_for_editor(400, 600, 'medium');
    187         $this->assertEquals(array(400, 600), $out);
     190        $this->assertSame( array( 400, 600 ), $out );
    188191
    189192        $out = image_constrain_size_for_editor(64, 64, 'medium');
    190         $this->assertEquals(array(64, 64), $out);
     193        $this->assertSame( array( 64, 64 ), $out );
    191194
    192195        // content_width should be ignored
    193196        $content_width = 350;
    194197        $out = image_constrain_size_for_editor(600, 400, 'medium');
    195         $this->assertEquals(array(500, 333), $out);
     198        $this->assertSame( array( 500, 333 ), $out );
    196199
    197200        $content_width = $_content_width;
     
    205208        $content_width = 400;
    206209        $out = image_constrain_size_for_editor(600, 400, 'full');
    207         $this->assertEquals(array(600, 400), $out);
     210        $this->assertSame( array( 600, 400 ), $out );
    208211
    209212        $out = image_constrain_size_for_editor(64, 64, 'full');
    210         $this->assertEquals(array(64, 64), $out);
     213        $this->assertSame( array( 64, 64 ), $out );
    211214
    212215        // content_width default is 500
     
    214217
    215218        $out = image_constrain_size_for_editor(600, 400, 'full');
    216         $this->assertEquals(array(600, 400), $out);
     219        $this->assertSame( array( 600, 400 ), $out );
    217220
    218221        $out = image_constrain_size_for_editor(64, 64, 'full');
    219         $this->assertEquals(array(64, 64), $out);
     222        $this->assertSame( array( 64, 64 ), $out );
    220223
    221224        $content_width = $_content_width;
Note: See TracChangeset for help on using the changeset viewer.