From 1de168e016670ae289534be084d5f6e138a395a2 Mon Sep 17 00:00:00 2001 From: Andrew Ozz Date: Sat, 7 Sep 2019 01:34:55 +0000 Subject: [PATCH] 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. Built from https://develop.svn.wordpress.org/trunk@46077 git-svn-id: http://core.svn.wordpress.org/trunk@45889 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/functions.php | 16 +++++++++++++ wp-includes/media.php | 50 ++++++++++++++++++++++++++++++++------- wp-includes/version.php | 2 +- 3 files changed, 59 insertions(+), 9 deletions(-) diff --git a/wp-includes/functions.php b/wp-includes/functions.php index f32e236778..ab86835314 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -7316,3 +7316,19 @@ function is_wp_version_compatible( $required ) { function is_php_version_compatible( $required ) { return empty( $required ) || version_compare( phpversion(), $required, '>=' ); } + +/** + * Check if two numbers are nearly the same. + * + * This is similar to using `round()` but the precision is more fine-grained. + * + * @since 5.3.0 + * + * @param int|float $expected The expected value. + * @param int|float $actual The actual number. + * @param int|float $precision The allowed variation. + * @return bool Whether the numbers match whithin the specified precision. + */ +function wp_fuzzy_number_match( $expected, $actual, $precision = 1 ) { + return abs( (float) $expected - (float) $actual ) <= $precision; +} diff --git a/wp-includes/media.php b/wp-includes/media.php index 6f104a20c7..76fa325919 100644 --- a/wp-includes/media.php +++ b/wp-includes/media.php @@ -543,8 +543,28 @@ function image_resize_dimensions( $orig_w, $orig_h, $dest_w, $dest_h, $crop = fa return $output; } + // Stop if the destination size is larger than the original image dimensions. + if ( empty( $dest_h ) ) { + if ( $orig_w < $dest_w ) { + return false; + } + } elseif ( empty( $dest_w ) ) { + if ( $orig_h < $dest_h ) { + return false; + } + } else { + if ( $orig_w < $dest_w && $orig_h < $dest_h ) { + return false; + } + } + if ( $crop ) { - // crop the largest possible portion of the original image that we can size to $dest_w x $dest_h + // Crop the largest possible portion of the original image that we can size to $dest_w x $dest_h. + // Note that the requested crop dimensions are used as a maximum bounding box for the original image. + // If the original image's width or height is less than the requested width or height + // only the greater one will be cropped. + // For example when the original image is 600x300, and the requested crop dimensions are 400x400, + // the resulting image will be 400x300. $aspect_ratio = $orig_w / $orig_h; $new_w = min( $dest_w, $orig_w ); $new_h = min( $dest_h, $orig_h ); @@ -584,7 +604,7 @@ function image_resize_dimensions( $orig_w, $orig_h, $dest_w, $dest_h, $crop = fa $s_y = floor( ( $orig_h - $crop_h ) / 2 ); } } else { - // don't crop, just resize using $dest_w x $dest_h as a maximum bounding box + // Resize using $dest_w x $dest_h as a maximum bounding box. $crop_w = $orig_w; $crop_h = $orig_h; @@ -594,15 +614,29 @@ function image_resize_dimensions( $orig_w, $orig_h, $dest_w, $dest_h, $crop = fa list( $new_w, $new_h ) = wp_constrain_dimensions( $orig_w, $orig_h, $dest_w, $dest_h ); } - // if the resulting image would be the same size or larger we don't want to resize it - if ( $new_w >= $orig_w && $new_h >= $orig_h && intval( $dest_w ) !== intval( $orig_w ) && intval( $dest_h ) !== intval( $orig_h ) ) { - return false; + if ( wp_fuzzy_number_match( $new_w, $orig_w ) && wp_fuzzy_number_match( $new_h, $orig_h ) ) { + // The new size has virtually the same dimensions as the original image. + + /** + * Filters whether to proceed with making an image sub-size with identical dimensions + * with the original/source image. Differences of 1px may be due to rounding and are ignored. + * + * @since 5.3.0 + * + * @param bool The filtered value. + * @param int Original image width. + * @param int Original image height. + */ + $proceed = (bool) apply_filters( 'wp_image_resize_identical_dimensions', false, $orig_w, $orig_h ); + + if ( ! $proceed ) { + return false; + } } - // the return array matches the parameters to imagecopyresampled() + // The return array matches the parameters to imagecopyresampled(). // int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h return array( 0, 0, (int) $s_x, (int) $s_y, (int) $new_w, (int) $new_h, (int) $crop_w, (int) $crop_h ); - } /** @@ -664,7 +698,7 @@ function wp_image_matches_ratio( $source_width, $source_height, $target_width, $ } // If the image dimensions are within 1px of the expected size, we consider it a match. - $matched = ( abs( $constrained_size[0] - $expected_size[0] ) <= 1 && abs( $constrained_size[1] - $expected_size[1] ) <= 1 ); + $matched = ( wp_fuzzy_number_match( $constrained_size[0], $expected_size[0] ) && wp_fuzzy_number_match( $constrained_size[1], $expected_size[1] ) ); return $matched; } diff --git a/wp-includes/version.php b/wp-includes/version.php index 255f367453..1f5b58de20 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.3-alpha-46076'; +$wp_version = '5.3-alpha-46077'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.