From c08c2e28cc77324f17f23ac0fcbfc786fd98524a Mon Sep 17 00:00:00 2001 From: Adam Silverstein Date: Fri, 29 Nov 2024 23:48:15 +0000 Subject: [PATCH] Media: improve filter to enable setting output quality by image size. Add a new $size parameter to the wp_editor_set_quality filter. $size is an array with 'width' and 'height' keys. Developers can use this information to set image quality based on the image size. Props adamsilverstein, joemcgill, Mte90, codekraft, birgire, azaozz, sppramodh. Fixes #54648. Built from https://develop.svn.wordpress.org/trunk@59473 git-svn-id: http://core.svn.wordpress.org/trunk@58859 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/class-wp-image-editor-gd.php | 18 ++++++++++++++---- wp-includes/class-wp-image-editor-imagick.php | 16 +++++++++++++--- wp-includes/class-wp-image-editor.php | 16 +++++++++++++--- wp-includes/version.php | 2 +- 4 files changed, 41 insertions(+), 11 deletions(-) diff --git a/wp-includes/class-wp-image-editor-gd.php b/wp-includes/class-wp-image-editor-gd.php index e4f0655376..47ba91a367 100644 --- a/wp-includes/class-wp-image-editor-gd.php +++ b/wp-includes/class-wp-image-editor-gd.php @@ -221,6 +221,14 @@ class WP_Image_Editor_GD extends WP_Image_Editor { list( $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ) = $dims; + $this->set_quality( + null, + array( + 'width' => $dst_w, + 'height' => $dst_h, + ) + ); + $resized = wp_imagecreatetruecolor( $dst_w, $dst_h ); imagecopyresampled( $resized, $this->image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ); @@ -568,12 +576,14 @@ class WP_Image_Editor_GD extends WP_Image_Editor { * Sets Image Compression quality on a 1-100% scale. Handles WebP lossless images. * * @since 6.7.0 + * @since 6.8.0 The `$dims` parameter was added. * - * @param int $quality Compression Quality. Range: [1,100] + * @param int $quality Compression Quality. Range: [1,100] + * @param array $dims Optional. Image dimensions array with 'width' and 'height' keys. * @return true|WP_Error True if set successfully; WP_Error on failure. */ - public function set_quality( $quality = null ) { - $quality_result = parent::set_quality( $quality ); + public function set_quality( $quality = null, $dims = array() ) { + $quality_result = parent::set_quality( $quality, $dims ); if ( is_wp_error( $quality_result ) ) { return $quality_result; } else { @@ -586,7 +596,7 @@ class WP_Image_Editor_GD extends WP_Image_Editor { $webp_info = wp_get_webp_info( $this->file ); if ( ! empty( $webp_info['type'] ) && 'lossless' === $webp_info['type'] ) { $quality = IMG_WEBP_LOSSLESS; - parent::set_quality( $quality ); + parent::set_quality( $quality, $dims ); } } } catch ( Exception $e ) { diff --git a/wp-includes/class-wp-image-editor-imagick.php b/wp-includes/class-wp-image-editor-imagick.php index 45b0e3a5a4..dd8b9ad519 100644 --- a/wp-includes/class-wp-image-editor-imagick.php +++ b/wp-includes/class-wp-image-editor-imagick.php @@ -190,12 +190,14 @@ class WP_Image_Editor_Imagick extends WP_Image_Editor { * Sets Image Compression quality on a 1-100% scale. * * @since 3.5.0 + * @since 6.8.0 The `$dims` parameter was added. * - * @param int $quality Compression Quality. Range: [1,100] + * @param int $quality Compression Quality. Range: [1,100] + * @param array $dims Optional. Image dimensions array with 'width' and 'height' keys. * @return true|WP_Error True if set successfully; WP_Error on failure. */ - public function set_quality( $quality = null ) { - $quality_result = parent::set_quality( $quality ); + public function set_quality( $quality = null, $dims = array() ) { + $quality_result = parent::set_quality( $quality, $dims ); if ( is_wp_error( $quality_result ) ) { return $quality_result; } else { @@ -367,6 +369,14 @@ class WP_Image_Editor_Imagick extends WP_Image_Editor { return $this->crop( $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h ); } + $this->set_quality( + null, + array( + 'width' => $dst_w, + 'height' => $dst_h, + ) + ); + // Execute the resize. $thumb_result = $this->thumbnail_image( $dst_w, $dst_h ); if ( is_wp_error( $thumb_result ) ) { diff --git a/wp-includes/class-wp-image-editor.php b/wp-includes/class-wp-image-editor.php index dc24205078..f9f58fcd50 100644 --- a/wp-includes/class-wp-image-editor.php +++ b/wp-includes/class-wp-image-editor.php @@ -240,11 +240,14 @@ abstract class WP_Image_Editor { * Sets Image Compression quality on a 1-100% scale. * * @since 3.5.0 + * @since 6.8.0 The `$dims` parameter was added. * - * @param int $quality Compression Quality. Range: [1,100] + * @param int $quality Compression Quality. Range: [1,100] + * @param array $dims Optional. Image dimensions array with 'width' and 'height' keys. * @return true|WP_Error True if set successfully; WP_Error on failure. + */ - public function set_quality( $quality = null ) { + public function set_quality( $quality = null, $dims = array() ) { // Use the output mime type if present. If not, fall back to the input/initial mime type. $mime_type = ! empty( $this->output_mime_type ) ? $this->output_mime_type : $this->mime_type; // Get the default quality setting for the mime type. @@ -260,11 +263,18 @@ abstract class WP_Image_Editor { * The WP_Image_Editor::set_quality() method has priority over the filter. * * @since 3.5.0 + * @since 6.8.0 Added the size parameter. * * @param int $quality Quality level between 1 (low) and 100 (high). * @param string $mime_type Image mime type. + * @param array $size { + * Dimensions of the image. + * + * @type int $width The image width. + * @type int $height The image height. + * } */ - $quality = apply_filters( 'wp_editor_set_quality', $default_quality, $mime_type ); + $quality = apply_filters( 'wp_editor_set_quality', $default_quality, $mime_type, $dims ? $dims : $this->size ); if ( 'image/jpeg' === $mime_type ) { /** diff --git a/wp-includes/version.php b/wp-includes/version.php index 3162555269..314d4ae8d7 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.8-alpha-59472'; +$wp_version = '6.8-alpha-59473'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.