Make WordPress Core


Ignore:
Timestamp:
09/01/2021 12:43:39 AM (5 years ago)
Author:
desrosj
Message:

Media: apply the wp_editor_set_quality filter not only when loading an image in the editor but also when saving an converted image, after the mime-type of the output image has changed.

Props mikeschroder, desrosj, azaozz.
Merges [51704] to the 5.8 branch.
Fixes #53667.

Location:
branches/5.8
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/5.8

  • branches/5.8/src/wp-includes/class-wp-image-editor.php

    r51706 r51712  
    1616        protected $size              = null;
    1717        protected $mime_type         = null;
     18        protected $output_mime_type  = null;
    1819        protected $default_mime_type = 'image/jpeg';
    1920        protected $quality           = false;
    20         protected $default_quality   = 82;
     21
     22        // Deprecated since 5.8.1. See get_default_quality() below.
     23        protected $default_quality = 82;
    2124
    2225        /**
    … …  
    225228         */
    226229        public function set_quality( $quality = null ) {
     230                // Use the output mime type if present. If not, fall back to the input/initial mime type.
     231                $mime_type = ! empty( $this->output_mime_type ) ? $this->output_mime_type : $this->mime_type;
     232                // Get the default quality setting for the mime type.
     233                $default_quality = $this->get_default_quality( $mime_type );
     234
    227235                if ( null === $quality ) {
    228236                        /**
    … …  
    239247                         * @param string $mime_type Image mime type.
    240248                         */
    241                         $quality = apply_filters( 'wp_editor_set_quality', $this->default_quality, $this->mime_type );
    242 
    243                         if ( 'image/jpeg' === $this->mime_type ) {
     249                        $quality = apply_filters( 'wp_editor_set_quality', $default_quality, $mime_type );
     250
     251                        if ( 'image/jpeg' === $mime_type ) {
    244252                                /**
    245253                                 * Filters the JPEG compression quality for backward-compatibility.
    … …  
    262270
    263271                        if ( $quality < 0 || $quality > 100 ) {
    264                                 $quality = $this->default_quality;
     272                                $quality = $default_quality;
    265273                        }
    266274                }
    … …  
    277285                        return new WP_Error( 'invalid_image_quality', __( 'Attempted to set image quality outside of the range [1,100].' ) );
    278286                }
     287        }
     288
     289        /**
     290         * Returns the default compression quality setting for the mime type.
     291         *
     292         * @since 5.8.1
     293         *
     294         * @param string $mime_type
     295         * @return int The default quality setting for the mime type.
     296         */
     297        protected function get_default_quality( $mime_type ) {
     298                switch ( $mime_type ) {
     299                        case 'image/webp':
     300                                $quality = 86;
     301                                break;
     302                        case 'image/jpeg':
     303                        default:
     304                                $quality = $this->default_quality;
     305                }
     306
     307                return $quality;
    279308        }
    280309
    … …  
    375404                }
    376405
    377                 if ( $filename ) {
     406                // Ensure both $filename and $new_ext are not empty.
     407                // $this->get_extension() returns false on error which would effectively remove the extension
     408                // from $filename. That shouldn't happen, files without extensions are not supported.
     409                if ( $filename && $new_ext ) {
    378410                        $dir = pathinfo( $filename, PATHINFO_DIRNAME );
    379411                        $ext = pathinfo( $filename, PATHINFO_EXTENSION );
    380412
    381413                        $filename = trailingslashit( $dir ) . wp_basename( $filename, ".$ext" ) . ".{$new_ext}";
     414                }
     415
     416                if ( $mime_type && ( $mime_type !== $this->mime_type ) ) {
     417                        // The image will be converted when saving. Set the quality for the new mime-type if not already set.
     418                        if ( $mime_type !== $this->output_mime_type ) {
     419                                $this->output_mime_type = $mime_type;
     420                                $this->set_quality();
     421                        }
     422                } elseif ( ! empty( $this->output_mime_type ) ) {
     423                        // Reset output_mime_type and quality.
     424                        $this->output_mime_type = null;
     425                        $this->set_quality();
    382426                }
    383427
Note: See TracChangeset for help on using the changeset viewer.