Make WordPress Core

Ticket #32437: 32437.diff

File 32437.diff, 4.4 KB (added by azaozz, 6 years ago)
  • src/wp-includes/functions.php

     
    71667166function is_php_version_compatible( $required ) {
    71677167        return empty( $required ) || version_compare( phpversion(), $required, '>=' );
    71687168}
     7169
     7170/**
     7171 * Check if two integers are nearly the same.
     7172 *
     7173 * This is similar to using `round()` but the precision is more fine-grained.
     7174 *
     7175 * @since 5.3.0
     7176 *
     7177 * @param int $expected  The expected value.
     7178 * @param int $actual    The actual number.
     7179 * @param int $precision The allowed variation.
     7180 * @return bool Whether the numbers match whithin the specified precision.
     7181 */
     7182function wp_numbers_match( $expected, $actual, $precision = 1 ) {
     7183        return abs( (int) $expected - (int) $actual ) <= $precision;
     7184}
  • src/wp-includes/media.php

     
    594594                list( $new_w, $new_h ) = wp_constrain_dimensions( $orig_w, $orig_h, $dest_w, $dest_h );
    595595        }
    596596
    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;
     597        // Stop if the destination size is larger than the original image dimensions.
     598        if ( empty( $dest_h ) ) {
     599                if ( $orig_w < $dest_w ) {
     600                        return false;
     601                }
     602        } elseif ( empty( $dest_w ) ) {
     603                if ( $orig_h < $dest_h ) {
     604                        return false;
     605                }
     606        } else {
     607                if ( $orig_w < $dest_w && $orig_h < $dest_h ) {
     608                        return false;
     609                }
    600610        }
    601611
    602         // the return array matches the parameters to imagecopyresampled()
     612        if ( wp_numbers_match( $new_w, $orig_w ) && wp_numbers_match( $new_h, $orig_h ) ) {
     613                // The new size has virtually identical dimensions as the original image.
     614
     615                /**
     616                 * Filters the threshold value (in pixels) above which an image sub-size with identical
     617                 * dimensions will be created. An image is considered larger if either its width or height
     618                 * is greater than the threshold.
     619                 *
     620                 * In this case it makes sense to still "resize" large images as the new size will be optimised,
     621                 * and the file size will be smaller. For example when the original image is a photo
     622                 * uploaded directly from a camera.
     623                 *
     624                 * @since 5.3.0
     625                 *
     626                 * @param int The filtered threshold value.
     627                 * @param int Original image width.
     628                 * @param int Original image height.
     629                 */
     630                $threshold = (int) apply_filters( 'wp_image_resize_identical_dimensions_threshold', 2560, $orig_w, $orig_h );
     631
     632                if ( $orig_w <= $threshold && $orig_h <= $threshold ) {
     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
    608642/**
  • tests/phpunit/tests/image/dimensions.php

     
    131131        function test_640x480() {
    132132                // crop 640x480 to fit 640x480 (no change)
    133133                $out = image_resize_dimensions( 640, 480, 640, 480, true );
    134                 // dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h
    135                 $this->assertEquals( array( 0, 0, 0, 0, 640, 480, 640, 480 ), $out );
     134                $this->assertFalse( $out );
    136135
    137136                // resize 640x480 to fit 640x480 (no change)
    138137                $out = image_resize_dimensions( 640, 480, 640, 480, false );
     138                $this->assertFalse( $out );
     139        }
     140
     141        function test_2570x600() {
     142                // 2560 is the default threshold for creating identical sub-sizes.
     143
     144                // crop 2570x1825 to fit 2570x600 (no change)
     145                $out = image_resize_dimensions( 2570, 600, 2570, 600, true );
    139146                // dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h
    140                 $this->assertEquals( array( 0, 0, 0, 0, 640, 480, 640, 480 ), $out );
     147                $this->assertEquals( array( 0, 0, 0, 0, 2570, 600, 2570, 600 ), $out );
     148
     149                // resize 2570x1825 to fit 2570x1825 (no change)
     150                $out = image_resize_dimensions( 2570, 600, 2570, 600, false );
     151                // dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h
     152                $this->assertEquals( array( 0, 0, 0, 0, 2570, 600, 2570, 600 ), $out );
    141153        }
    142154
    143155        /**