Make WordPress Core

Changeset 46077


Ignore:
Timestamp:
09/07/2019 01:33:16 AM (5 years ago)
Author:
azaozz
Message:

Media: Improve handling of cases where an uploaded image matches exactly a defined intermediate size. In most of these cases the original image has been edited by the user and is "web ready", there is no need for an identical intermediate image.

Introduces the wp_image_resize_identical_dimensions filter so plugins and themes can determine whether a new image with identical dimensions should be created, defaults to false.

Props wpdennis, HKandulla, galbaras, azaozz.
See #32437.

Location:
trunk
Files:
3 edited

Legend:

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

    r45932 r46077  
    73177317    return empty( $required ) || version_compare( phpversion(), $required, '>=' );
    73187318}
     7319
     7320/**
     7321 * Check if two numbers are nearly the same.
     7322 *
     7323 * This is similar to using `round()` but the precision is more fine-grained.
     7324 *
     7325 * @since 5.3.0
     7326 *
     7327 * @param int|float $expected  The expected value.
     7328 * @param int|float $actual    The actual number.
     7329 * @param int|float $precision The allowed variation.
     7330 * @return bool Whether the numbers match whithin the specified precision.
     7331 */
     7332function wp_fuzzy_number_match( $expected, $actual, $precision = 1 ) {
     7333    return abs( (float) $expected - (float) $actual ) <= $precision;
     7334}
  • trunk/src/wp-includes/media.php

    r46066 r46077  
    544544    }
    545545
     546    // Stop if the destination size is larger than the original image dimensions.
     547    if ( empty( $dest_h ) ) {
     548        if ( $orig_w < $dest_w ) {
     549            return false;
     550        }
     551    } elseif ( empty( $dest_w ) ) {
     552        if ( $orig_h < $dest_h ) {
     553            return false;
     554        }
     555    } else {
     556        if ( $orig_w < $dest_w && $orig_h < $dest_h ) {
     557            return false;
     558        }
     559    }
     560
    546561    if ( $crop ) {
    547         // crop the largest possible portion of the original image that we can size to $dest_w x $dest_h
     562        // Crop the largest possible portion of the original image that we can size to $dest_w x $dest_h.
     563        // Note that the requested crop dimensions are used as a maximum bounding box for the original image.
     564        // If the original image's width or height is less than the requested width or height
     565        // only the greater one will be cropped.
     566        // For example when the original image is 600x300, and the requested crop dimensions are 400x400,
     567        // the resulting image will be 400x300.
    548568        $aspect_ratio = $orig_w / $orig_h;
    549569        $new_w        = min( $dest_w, $orig_w );
     
    585605        }
    586606    } else {
    587         // don't crop, just resize using $dest_w x $dest_h as a maximum bounding box
     607        // Resize using $dest_w x $dest_h as a maximum bounding box.
    588608        $crop_w = $orig_w;
    589609        $crop_h = $orig_h;
     
    595615    }
    596616
    597     // if the resulting image would be the same size or larger we don't want to resize it
    598     if ( $new_w >= $orig_w && $new_h >= $orig_h && intval( $dest_w ) !== intval( $orig_w ) && intval( $dest_h ) !== intval( $orig_h ) ) {
    599         return false;
    600     }
    601 
    602     // the return array matches the parameters to imagecopyresampled()
     617    if ( wp_fuzzy_number_match( $new_w, $orig_w ) && wp_fuzzy_number_match( $new_h, $orig_h ) ) {
     618        // The new size has virtually the same dimensions as the original image.
     619
     620        /**
     621         * Filters whether to proceed with making an image sub-size with identical dimensions
     622         * with the original/source image. Differences of 1px may be due to rounding and are ignored.
     623         *
     624         * @since 5.3.0
     625         *
     626         * @param bool The filtered value.
     627         * @param int  Original image width.
     628         * @param int  Original image height.
     629         */
     630        $proceed = (bool) apply_filters( 'wp_image_resize_identical_dimensions', false, $orig_w, $orig_h );
     631
     632        if ( ! $proceed ) {
     633            return false;
     634        }
     635    }
     636
     637    // The return array matches the parameters to imagecopyresampled().
    603638    // int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h
    604639    return array( 0, 0, (int) $s_x, (int) $s_y, (int) $new_w, (int) $new_h, (int) $crop_w, (int) $crop_h );
    605 
    606640}
    607641
     
    665699
    666700    // If the image dimensions are within 1px of the expected size, we consider it a match.
    667     $matched = ( abs( $constrained_size[0] - $expected_size[0] ) <= 1 && abs( $constrained_size[1] - $expected_size[1] ) <= 1 );
     701    $matched = ( wp_fuzzy_number_match( $constrained_size[0], $expected_size[0] ) && wp_fuzzy_number_match( $constrained_size[1], $expected_size[1] ) );
    668702
    669703    return $matched;
  • trunk/tests/phpunit/tests/image/dimensions.php

    r42343 r46077  
    132132        // crop 640x480 to fit 640x480 (no change)
    133133        $out = image_resize_dimensions( 640, 480, 640, 480, true );
     134        $this->assertFalse( $out );
     135
     136        // resize 640x480 to fit 640x480 (no change)
     137        $out = image_resize_dimensions( 640, 480, 640, 480, false );
     138        $this->assertFalse( $out );
     139
     140        // Test with the filter override.
     141        add_filter( 'wp_image_resize_identical_dimensions', '__return_true' );
     142
     143        // crop 640x480 to fit 640x480 (no change)
     144        $out = image_resize_dimensions( 640, 480, 640, 480, true );
    134145        // dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h
    135146        $this->assertEquals( array( 0, 0, 0, 0, 640, 480, 640, 480 ), $out );
     
    139150        // dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h
    140151        $this->assertEquals( array( 0, 0, 0, 0, 640, 480, 640, 480 ), $out );
     152
     153        remove_filter( 'wp_image_resize_identical_dimensions', '__return_true' );
    141154    }
    142155
Note: See TracChangeset for help on using the changeset viewer.