From 51c5572f94f4b92573d36e1d7bba0de09e598b0e Mon Sep 17 00:00:00 2001 From: Adam Silverstein Date: Mon, 30 Sep 2024 18:39:11 +0000 Subject: [PATCH] Media: improve support for lossless WebP. When uploading lossless WebP images, WordPress now correctly outputs lossless WebP with both the Imagick and GD image editors. Props: adamsilverstein, martinkrcho. Fixes #60291. Built from https://develop.svn.wordpress.org/branches/6.5@59136 git-svn-id: http://core.svn.wordpress.org/branches/6.5@58532 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/class-wp-image-editor-gd.php | 32 +++++++++++++++++++ wp-includes/class-wp-image-editor-imagick.php | 1 + wp-includes/version.php | 2 +- 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/wp-includes/class-wp-image-editor-gd.php b/wp-includes/class-wp-image-editor-gd.php index cbfa6dba38..cbc33007b9 100644 --- a/wp-includes/class-wp-image-editor-gd.php +++ b/wp-includes/class-wp-image-editor-gd.php @@ -567,6 +567,38 @@ 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 + * + * @param int $quality Compression Quality. Range: [1,100] + * @return true|WP_Error True if set successfully; WP_Error on failure. + */ + public function set_quality( $quality = null ) { + $quality_result = parent::set_quality( $quality ); + if ( is_wp_error( $quality_result ) ) { + return $quality_result; + } else { + $quality = $this->get_quality(); + } + + // Handle setting the quality for WebP lossless images, see https://php.watch/versions/8.1/gd-webp-lossless. + try { + if ( 'image/webp' === $this->mime_type && defined( 'IMG_WEBP_LOSSLESS' ) ) { + $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 ); + } + } + } catch ( Exception $e ) { + return new WP_Error( 'image_quality_error', $e->getMessage() ); + } + $this->quality = $quality; + return true; + } + /** * Returns stream of current image. * diff --git a/wp-includes/class-wp-image-editor-imagick.php b/wp-includes/class-wp-image-editor-imagick.php index 5de01d7322..bbac4c8fca 100644 --- a/wp-includes/class-wp-image-editor-imagick.php +++ b/wp-includes/class-wp-image-editor-imagick.php @@ -215,6 +215,7 @@ class WP_Image_Editor_Imagick extends WP_Image_Editor { // Use WebP lossless settings. $this->image->setImageCompressionQuality( 100 ); $this->image->setOption( 'webp:lossless', 'true' ); + parent::set_quality( 100 ); } else { $this->image->setImageCompressionQuality( $quality ); } diff --git a/wp-includes/version.php b/wp-includes/version.php index 6b611a1c03..c722e169cb 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.5.6-alpha-58808'; +$wp_version = '6.5.6-alpha-59136'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.