WordPress.org

Make WordPress Core

Ticket #37840: 37840.2.diff

File 37840.2.diff, 8.2 KB (added by enshrined, 10 months ago)

Refreshed patch for trunk

  • src/wp-admin/includes/image.php

    diff --git a/src/wp-admin/includes/image.php b/src/wp-admin/includes/image.php
    index 254ba7d90f..f69ef60397 100644
    a b function wp_generate_attachment_metadata( $attachment_id, $file ) { 
    105105                                $sizes[ $s ]['width'] = intval( $_wp_additional_image_sizes[ $s ]['width'] );
    106106                        } else {
    107107                                // For default sizes set in options
    108                                 $sizes[ $s ]['width'] = get_option( "{$s}_size_w" );
     108                                $sizes[ $s ]['width'] = get_option( "{$s}_size_w", $metadata['width'] );
    109109                        }
    110110
    111111                        if ( isset( $_wp_additional_image_sizes[ $s ]['height'] ) ) {
    function wp_generate_attachment_metadata( $attachment_id, $file ) { 
    113113                                $sizes[ $s ]['height'] = intval( $_wp_additional_image_sizes[ $s ]['height'] );
    114114                        } else {
    115115                                // For default sizes set in options
    116                                 $sizes[ $s ]['height'] = get_option( "{$s}_size_h" );
     116                                $sizes[ $s ]['height'] = get_option( "{$s}_size_h", $metadata['height'] );
    117117                        }
    118118
    119119                        if ( isset( $_wp_additional_image_sizes[ $s ]['crop'] ) ) {
    function wp_generate_attachment_metadata( $attachment_id, $file ) { 
    121121                                $sizes[ $s ]['crop'] = $_wp_additional_image_sizes[ $s ]['crop'];
    122122                        } else {
    123123                                // For default sizes set in options
    124                                 $sizes[ $s ]['crop'] = get_option( "{$s}_crop" );
     124                                $sizes[ $s ]['crop'] = get_option( "{$s}_crop", false );
    125125                        }
    126126                }
    127127
  • src/wp-includes/class-wp-image-editor-gd.php

    diff --git a/src/wp-includes/class-wp-image-editor-gd.php b/src/wp-includes/class-wp-image-editor-gd.php
    index 32f33dfa6e..e78dc15ccc 100644
    a b class WP_Image_Editor_GD extends WP_Image_Editor { 
    109109                        imagesavealpha( $this->image, true );
    110110                }
    111111
     112                $this->filesize = @filesize( $this->file );
    112113                $this->update_size( $size[0], $size[1] );
    113114                $this->mime_type = $size['mime'];
    114115
    class WP_Image_Editor_GD extends WP_Image_Editor { 
    239240                        $image     = $this->_resize( $size_data['width'], $size_data['height'], $size_data['crop'] );
    240241                        $duplicate = ( ( $orig_size['width'] == $size_data['width'] ) && ( $orig_size['height'] == $size_data['height'] ) );
    241242
     243                        if ( 'full' == $size ) {
     244                                if ( true === $this->can_save_optimised_full_size() ) {
     245                                        $duplicate = false;
     246                                }
     247                        }
     248
    242249                        if ( ! is_wp_error( $image ) && ! $duplicate ) {
    243250                                $resized = $this->_save( $image );
    244251
  • src/wp-includes/class-wp-image-editor-imagick.php

    diff --git a/src/wp-includes/class-wp-image-editor-imagick.php b/src/wp-includes/class-wp-image-editor-imagick.php
    index abb215038b..c38aa89ec5 100644
    a b class WP_Image_Editor_Imagick extends WP_Image_Editor { 
    168168                        return new WP_Error( 'invalid_image', $e->getMessage(), $this->file );
    169169                }
    170170
     171                $this->filesize = @filesize( $this->file );
    171172                $updated_size = $this->update_size();
    172173                if ( is_wp_error( $updated_size ) ) {
    173174                        return $updated_size;
    class WP_Image_Editor_Imagick extends WP_Image_Editor { 
    457458                        $resize_result = $this->resize( $size_data['width'], $size_data['height'], $size_data['crop'] );
    458459                        $duplicate     = ( ( $orig_size['width'] == $size_data['width'] ) && ( $orig_size['height'] == $size_data['height'] ) );
    459460
     461                        if ( 'full' == $size ) {
     462                                if ( true === $this->can_save_optimised_full_size() ) {
     463                                        $duplicate = false;
     464                                }
     465                        }
     466
    460467                        if ( ! is_wp_error( $resize_result ) && ! $duplicate ) {
    461468                                $resized = $this->_save( $this->image );
    462469
  • src/wp-includes/class-wp-image-editor.php

    diff --git a/src/wp-includes/class-wp-image-editor.php b/src/wp-includes/class-wp-image-editor.php
    index 26dd78d230..97485d6564 100644
    a b abstract class WP_Image_Editor { 
    1818        protected $default_mime_type = 'image/jpeg';
    1919        protected $quality           = false;
    2020        protected $default_quality   = 82;
     21        protected $filesize = null;
     22        protected $minimum_filesize_difference = false;
     23        protected $default_minimum_filesize_difference = 15;
     24
    2125
    2226        /**
    2327         * Each instance handles a single file.
    abstract class WP_Image_Editor { 
    466470
    467471                return $extensions[0];
    468472        }
     473
     474        /**
     475         * Gets minimum filesize difference for compressed full size images.
     476         *
     477         * @since 4.8.0
     478         * @access public
     479         *
     480         * @return int $minimum_filesize_difference Minimum filesize difference. Range [0-100]
     481         */
     482        public function get_minimum_filesize_difference() {
     483                if ( ! $this->minimum_filesize_difference ) {
     484                        $this->set_minimum_filesize_difference();
     485                }
     486
     487                return $this->minimum_filesize_difference;
     488        }
     489
     490        /**
     491         * Sets minimum filesize difference for compressed full images, 0-100% scale.
     492         *
     493         * @since 4.8.0
     494         * @access public
     495         *
     496         * @param int $minimum_filesize_difference Minimum filesize difference. Range [0-100]
     497         * @return true|WP_Error True if set successfully; WP_Error on failure.
     498         */
     499        public function set_minimum_filesize_difference( $minimum_filesize_difference = null ) {
     500                if ( null === $minimum_filesize_difference ) {
     501                        /**
     502                         * Filters the default minimum filesize difference. Range [0-100]
     503                         *
     504                         * Applies only during initial editor instantiation, or when set_minimum_filesize_difference() is run
     505                         * manually without the `$minimum_filesize_difference` argument.
     506                         *
     507                         * set_minimum_filesize_difference() has priority over the filter.
     508                         *
     509                         * @since 4.8.0
     510                         *
     511                         * @param int $default_minimum_filesize_difference Minimum filesize difference. Range [0-100]
     512                         * @param string $mime_type Image mime type.
     513                         */
     514                        $minimum_filesize_difference = apply_filters( 'wp_image_set_minimum_filesize_difference', $this->default_minimum_filesize_difference, $this->mime_type );
     515
     516
     517                        if ( $minimum_filesize_difference < 0 || $minimum_filesize_difference > 100 ) {
     518                                $minimum_filesize_difference = $this->default_minimum_filesize_difference;
     519                        }
     520                }
     521
     522                if ( ( $minimum_filesize_difference >= 0 ) && ( $minimum_filesize_difference <= 100 ) ) {
     523                        $this->minimum_filesize_difference = $minimum_filesize_difference;
     524                        return true;
     525                } else {
     526                        return new WP_Error( 'invalid_filesize_difference', __('Attempted to set filesize difference outside of the range [1,100].') );
     527                }
     528        }
     529
     530        /**
     531         * Should we save the optimised version of the 'full' image?
     532         *
     533         * If we're optimising the 'full' image size then we need to compare image sizes.
     534         * If the percentage of the image size decrease is greater than our set value
     535         * then we should store the image.
     536         *
     537         * See trac ticket #37840
     538         *
     539         * @since 4.8.0
     540         * @access protected
     541         *
     542         * @return bool True if we should save the image, otherwise False.
     543         */
     544        protected function can_save_optimised_full_size() {
     545                ob_start();
     546                $this->stream();
     547                $optimised_size = ob_get_length();
     548                ob_end_clean();
     549
     550                // Make sure we've got numbers to calculate with
     551                if( is_numeric( $optimised_size ) && is_numeric( $this->filesize ) ) {
     552                        $perc_decrease = ( ( $optimised_size - $this->filesize ) / ( $this->filesize * - 1 ) ) * 100;
     553
     554                        if ( $this->get_minimum_filesize_difference() <= $perc_decrease ) {
     555                                return true;
     556                        }
     557                }
     558
     559                return false;
     560        }
    469561}
    470562
  • src/wp-includes/media.php

    diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php
    index ec50703b33..be2588d660 100644
    a b function image_get_intermediate_size( $post_id, $size = 'thumbnail' ) { 
    787787 */
    788788function get_intermediate_image_sizes() {
    789789        $_wp_additional_image_sizes = wp_get_additional_image_sizes();
    790         $image_sizes                = array( 'thumbnail', 'medium', 'medium_large', 'large' ); // Standard sizes
     790        $image_sizes                = array( 'thumbnail', 'medium', 'medium_large', 'large', 'full' ); // Standard sizes
    791791        if ( ! empty( $_wp_additional_image_sizes ) ) {
    792792                $image_sizes = array_merge( $image_sizes, array_keys( $_wp_additional_image_sizes ) );
    793793        }
    function get_intermediate_image_sizes() { 
    798798         * @since 2.5.0
    799799         *
    800800         * @param array $image_sizes An array of intermediate image sizes. Defaults
    801          *                           are 'thumbnail', 'medium', 'medium_large', 'large'.
     801         *                           are 'thumbnail', 'medium', 'medium_large', 'large', 'full'.
    802802         */
    803803        return apply_filters( 'intermediate_image_sizes', $image_sizes );
    804804}