WordPress.org

Make WordPress Core


Ignore:
Timestamp:
09/07/2019 01:33:16 AM (5 weeks 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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;
Note: See TracChangeset for help on using the changeset viewer.