WordPress.org

Make WordPress Core

Ticket #12120: 12120.diff

File 12120.diff, 10.7 KB (added by ericlewis, 6 years ago)
  • src/wp-includes/media.php

     
    345345}
    346346
    347347/**
    348  * Calculates the new dimensions for a downsampled image.
     348 * Scale a set of x and y dimensions to fit inside another set of dimensions.
    349349 *
    350350 * If either width or height are empty, no constraint is applied on
    351351 * that dimension.
    352352 *
    353  * @since 2.5.0
     353 * @since 4.2.0
    354354 *
    355  * @param int $current_width Current width of the image.
    356  * @param int $current_height Current height of the image.
    357  * @param int $max_width Optional. Maximum wanted width.
    358  * @param int $max_height Optional. Maximum wanted height.
     355 * @param int    $src_width       Current width.
     356 * @param int    $src_height      Current height.
     357 * @param int    $dest_width      Maximum desired width.
     358 * @param int    $dest_height     Maximum desired height.
     359 * @param string $allow_upscaling Optional. Whether to allow the dimensions to be
     360 *                                scaled up. Default is `disallow_upscaling`.
    359361 * @return array First item is the width, the second item is the height.
    360362 */
    361 function wp_constrain_dimensions( $current_width, $current_height, $max_width=0, $max_height=0 ) {
    362         if ( !$max_width && !$max_height )
    363                 return array( $current_width, $current_height );
     363function wp_scale_dimensions( $src_width, $src_height, $dest_width, $dest_height, $allow_upscaling = 'disallow_upscaling' ) {
     364        if ( ! $dest_width && ! $dest_height ) {
     365                return array( $src_width, $src_height );
     366        }
    364367
    365368        $width_ratio = $height_ratio = 1.0;
    366369        $did_width = $did_height = false;
    367370
    368         if ( $max_width > 0 && $current_width > 0 && $current_width > $max_width ) {
    369                 $width_ratio = $max_width / $current_width;
     371        if ( $dest_width > 0 && $src_width > 0 && ( $allow_upscaling != 'disallow_upscaling' || ( $allow_upscaling == 'disallow_upscaling' && $src_width > $dest_width ) ) ) {
     372                $width_ratio = $dest_width / $src_width;
    370373                $did_width = true;
    371374        }
    372375
    373         if ( $max_height > 0 && $current_height > 0 && $current_height > $max_height ) {
    374                 $height_ratio = $max_height / $current_height;
     376        if ( $dest_height > 0 && $src_height > 0 && ( $allow_upscaling != 'disallow_upscaling' || ( $allow_upscaling == 'disallow_upscaling' && $src_height > $dest_height ) ) ) {
     377                $height_ratio = $dest_height / $src_height;
    375378                $did_height = true;
    376379        }
    377380
     
    379382        $smaller_ratio = min( $width_ratio, $height_ratio );
    380383        $larger_ratio  = max( $width_ratio, $height_ratio );
    381384
    382         if ( (int) round( $current_width * $larger_ratio ) > $max_width || (int) round( $current_height * $larger_ratio ) > $max_height ) {
     385        if ( (int) round( $src_width * $larger_ratio ) > $dest_width || (int) round( $src_height * $larger_ratio ) > $dest_height ) {
    383386                // The larger ratio is too big. It would result in an overflow.
    384387                $ratio = $smaller_ratio;
    385388        } else {
     
    388391        }
    389392
    390393        // Very small dimensions may result in 0, 1 should be the minimum.
    391         $w = max ( 1, (int) round( $current_width  * $ratio ) );
    392         $h = max ( 1, (int) round( $current_height * $ratio ) );
     394        $w = max ( 1, (int) round( $src_width  * $ratio ) );
     395        $h = max ( 1, (int) round( $src_height * $ratio ) );
    393396
    394397        // Sometimes, due to rounding, we'll end up with a result like this: 465x700 in a 177x177 box is 117x176... a pixel short
    395398        // 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.
     
    396399        // Thus we look for dimensions that are one pixel shy of the max value and bump them up
    397400
    398401        // Note: $did_width means it is possible $smaller_ratio == $width_ratio.
    399         if ( $did_width && $w == $max_width - 1 ) {
    400                 $w = $max_width; // Round it up
     402        if ( $did_width && $w == $src_width - 1 ) {
     403                $w = $src_width; // Round it up
    401404        }
    402405
    403406        // Note: $did_height means it is possible $smaller_ratio == $height_ratio.
    404         if ( $did_height && $h == $max_height - 1 ) {
    405                 $h = $max_height; // Round it up
     407        if ( $did_height && $h == $src_height - 1 ) {
     408                $h = $src_height; // Round it up
    406409        }
    407410
    408         return apply_filters( 'wp_constrain_dimensions', array( $w, $h ), $current_width, $current_height, $max_width, $max_height );
     411        return array( $w, $h );
    409412}
    410413
    411414/**
     415 * Calculates the new dimensions for a downsampled image.
     416 *
     417 * If either width or height are empty, no constraint is applied on
     418 * that dimension.
     419 *
     420 * @since 2.5.0
     421 *
     422 * @param int $current_width  Current width of the image.
     423 * @param int $current_height Current height of the image.
     424 * @param int $max_width      Optional. Maximum wanted width.
     425 * @param int $max_height     Optional. Maximum wanted height.
     426 * @return array First item is the width, the second item is the height.
     427 */
     428function wp_constrain_dimensions( $current_width, $current_height, $max_width=0, $max_height=0 ) {
     429        $constrained = wp_scale_dimensions( $current_width, $current_height, $max_width, $max_height );
     430        return apply_filters( 'wp_constrain_dimensions', array( $constrained[0], $constrained[1] ), $current_width, $current_height, $max_width, $max_height );
     431}
     432
     433/**
    412434 * Retrieve calculated resize dimensions for use in WP_Image_Editor.
    413435 *
    414436 * Calculates dimensions and coordinates for a resized image that fits
     
    433455 * @return bool|array False on failure. Returned array matches parameters for `imagecopyresampled()`.
    434456 */
    435457function image_resize_dimensions($orig_w, $orig_h, $dest_w, $dest_h, $crop = false) {
    436 
    437458        if ($orig_w <= 0 || $orig_h <= 0)
    438459                return false;
    439460        // at least one of dest_w or dest_h must be specific
     
    461482                return $output;
    462483
    463484        if ( $crop ) {
    464                 // crop the largest possible portion of the original image that we can size to $dest_w x $dest_h
    465485                $aspect_ratio = $orig_w / $orig_h;
    466486                $new_w = min($dest_w, $orig_w);
    467487                $new_h = min($dest_h, $orig_h);
     
    508528                $s_x = 0;
    509529                $s_y = 0;
    510530
    511                 list( $new_w, $new_h ) = wp_constrain_dimensions( $orig_w, $orig_h, $dest_w, $dest_h );
     531                list( $new_w, $new_h ) = wp_scale_dimensions( $orig_w, $orig_h, $dest_w, $dest_h, 'allow_upscaling' );
    512532        }
    513533
    514         // if the resulting image would be the same size or larger we don't want to resize it
    515         if ( $new_w >= $orig_w && $new_h >= $orig_h && $dest_w != $orig_w && $dest_h != $orig_h ) {
     534        // if the resulting image would be the same size we don't want to resize it.
     535        if ( $new_w == $orig_w && $new_h == $orig_h && $dest_w != $orig_w && $dest_h != $orig_h ) {
    516536                return false;
    517537        }
    518538
  • tests/phpunit/tests/image/dimensions.php

     
    4545        function test_800x800_no_crop() {
    4646                // landscape: resize 640x480 to fit 800x800
    4747                $out = image_resize_dimensions(640, 480, 800, 800, false);
     48
    4849                // dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h
    49                 $this->assertEquals( false, $out );
     50                $this->assertEquals( array(0, 0, 0, 0, 800, 600, 640, 480), $out );
    5051
    5152                // portrait: resize 480x640 to fit 800x800
    5253                $out = image_resize_dimensions(480, 640, 800, 800, false);
    5354                // dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h
    54                 $this->assertEquals( false, $out );
     55                $this->assertEquals( array(0, 0, 0, 0, 600, 800, 480, 640), $out );
    5556        }
    5657
    5758        function test_800x0_no_crop() {
  • tests/phpunit/tests/image/resize.php

     
    5656                unlink( $image );
    5757        }
    5858
     59        /**
     60         * Image resizing larger is allowed since 4.2.0.
     61         */
    5962        function test_resize_larger() {
    60                 // image_resize() should refuse to make an image larger
    6163                $image = $this->resize_helper( DIR_TESTDATA.'/images/test-image.jpg', 100, 100 );
    6264
    63                 $this->assertInstanceOf( 'WP_Error', $image );
    64                 $this->assertEquals( 'error_getting_dimensions', $image->get_error_code() );
     65                $this->assertEquals( 'test-image-100x100.jpg', basename( $image ) );
     66                list($w, $h, $type) = getimagesize($image);
     67                $this->assertEquals( 100, $w );
     68                $this->assertEquals( 100, $h );
     69                $this->assertEquals( IMAGETYPE_JPEG, $type );
    6570        }
    6671
    6772        function test_resize_thumb_128x96() {
  • tests/phpunit/tests/image/size.php

     
    224224                $content_width = $_content_width;
    225225        }
    226226
     227        function test_upscaling_dimensions() {
     228                // Passing no destination dimensions should result in the passed-in dimensions.
     229                $out = wp_scale_dimensions(640, 480, 0, 0, 'allow_upscaling' );
     230                $this->assertSame( array( 640, 480 ), $out );
     231
     232                // Test scaling up.
     233                $out = wp_scale_dimensions(640, 480, 1000, 1500, 'allow_upscaling' );
     234                $this->assertSame( array( 1000, 750 ), $out );
     235
     236                $out = wp_scale_dimensions(640, 480, 2000, 1000, 'allow_upscaling' );
     237                $this->assertSame( array( 1333, 1000 ), $out );
     238
     239                // Scaling down should also work with `allow_upscaling` flag enabled.
     240                $out = wp_scale_dimensions(640, 480, 400, 500, 'allow_upscaling' );
     241                $this->assertSame( array( 400, 300 ), $out );
     242
     243                $out = wp_scale_dimensions(640, 480, 400, 500, 'allow_upscaling' );
     244                $this->assertSame( array( 400, 300 ), $out );
     245        }
    227246}
  • tests/phpunit/tests/post/attachments.php

     
    5151                $id = $this->_make_attachment($upload);
    5252        }
    5353
    54         function test_insert_image_no_thumb() {
    55 
    56                 // this image is smaller than the thumbnail size so it won't have one
    57                 $filename = ( DIR_TESTDATA.'/images/test-image.jpg' );
    58                 $contents = file_get_contents($filename);
    59 
    60                 $upload = wp_upload_bits(basename($filename), null, $contents);
    61                 $this->assertTrue( empty($upload['error']) );
    62 
    63                 $id = $this->_make_attachment($upload);
    64 
    65                 // intermediate copies should not exist
    66                 $this->assertFalse( image_get_intermediate_size($id, 'thumbnail') );
    67                 $this->assertFalse( image_get_intermediate_size($id, 'medium') );
    68 
    69                 // medium and full size will both point to the original
    70                 $downsize = image_downsize($id, 'medium');
    71                 $this->assertEquals( 'test-image.jpg', basename($downsize[0]) );
    72                 $this->assertEquals( 50, $downsize[1] );
    73                 $this->assertEquals( 50, $downsize[2] );
    74 
    75                 $downsize = image_downsize($id, 'full');
    76                 $this->assertEquals( 'test-image.jpg', basename($downsize[0]) );
    77                 $this->assertEquals( 50, $downsize[1] );
    78                 $this->assertEquals( 50, $downsize[2] );
    79 
    80         }
    81 
    8254        function test_insert_image_thumb_only() {
    8355                if ( !function_exists( 'imagejpeg' ) )
    8456                        $this->markTestSkipped( 'jpeg support unavailable' );