From 2bab211bc76f7070dc986ee4ebce28ee6a7fa05b Mon Sep 17 00:00:00 2001 From: Adam Silverstein Date: Tue, 7 Jan 2025 21:04:35 +0000 Subject: [PATCH] Media: enable high bit depth resized image output with Imagick. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix an issue where uploaded HDR images were resized and output as SDR and thus significantly degraded from the original. When using Imagick, output images will now match the bit depth of the uploaded image. Add a new filter ‘image_max_bit_depth’ which developers can use to control the maximum bit depth for resized images. Props adamsilverstein, kirasong, gregbenz, apermo. Fixes #62285. git-svn-id: https://develop.svn.wordpress.org/trunk@59588 602fd350-edb4-49c9-b593-d223f7449a82 --- .../class-wp-image-editor-imagick.php | 20 ++++-- tests/phpunit/tests/image/editorImagick.php | 67 +++++++++++++++++++ 2 files changed, 83 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/class-wp-image-editor-imagick.php b/src/wp-includes/class-wp-image-editor-imagick.php index dd8b9ad5191ee..9abb631847544 100644 --- a/src/wp-includes/class-wp-image-editor-imagick.php +++ b/src/wp-includes/class-wp-image-editor-imagick.php @@ -503,11 +503,23 @@ protected function thumbnail_image( $dst_w, $dst_h, $filter_name = 'FILTER_TRIAN } } - // Limit the bit depth of resized images to 8 bits per channel. + // Limit the bit depth of resized images. if ( is_callable( array( $this->image, 'getImageDepth' ) ) && is_callable( array( $this->image, 'setImageDepth' ) ) ) { - if ( 8 < $this->image->getImageDepth() ) { - $this->image->setImageDepth( 8 ); - } + /** + * Filters the maximum bit depth of resized images. + * + * This filter only applies when resizing using the Imagick editor since GD + * does not support getting or setting bit depth. + * + * Use this to adjust the maximum bit depth of resized images. + * + * @since 6.8.0 + * + * @param int $max_depth The maximum bit depth. Default is the input depth. + * @param int $image_depth The bit depth of the original image. + */ + $max_depth = apply_filters( 'image_max_bit_depth', $this->image->getImageDepth(), $this->image->getImageDepth() ); + $this->image->setImageDepth( $max_depth ); } } catch ( Exception $e ) { return new WP_Error( 'image_resize_error', $e->getMessage() ); diff --git a/tests/phpunit/tests/image/editorImagick.php b/tests/phpunit/tests/image/editorImagick.php index 30747f69f1b62..cd424a8856cc5 100644 --- a/tests/phpunit/tests/image/editorImagick.php +++ b/tests/phpunit/tests/image/editorImagick.php @@ -691,4 +691,71 @@ static function ( $value ) { $imagick->destroy(); $this->assertSame( $expected, $output, 'The image color of the generated thumb does not match expected opaque background.' ); // Allow for floating point equivalence. } + + /** + * Test filter `image_max_bit_depth` correctly sets the maximum bit depth of resized images. + * + * @ticket 62285 + */ + public function test_image_max_bit_depth() { + $file = DIR_TESTDATA . '/images/colors_hdr_p3.avif'; + $imagick_image_editor = new WP_Image_Editor_Imagick( $file ); + + // Skip if AVIF not supported. + if ( ! $imagick_image_editor->supports_mime_type( 'image/avif' ) ) { + $this->markTestSkipped( 'The image editor does not support the AVIF mime type.' ); + } + + // Skip if depth methods not available. + if ( ! method_exists( 'Imagick', 'getImageDepth' ) || ! method_exists( 'Imagick', 'setImageDepth' ) ) { + $this->markTestSkipped( 'The image editor does not support get or setImageDepth.' ); + } + + // Verify source image has 10-bit depth. + $imagick = new Imagick( $file ); + $this->assertSame( 10, $imagick->getImageDepth() ); + + // Test ability to save 10-bit image. + $imagick->setImageDepth( 10 ); + $test_file = tempnam( get_temp_dir(), '' ) . 'test10.avif'; + $imagick->writeImage( $test_file ); + $im = new Imagick( $test_file ); + + if ( $im->getImageDepth() !== 10 ) { + $this->markTestSkipped( 'Imagick is unable to save a 10 bit image.' ); + } + $im->destroy(); + unlink( $test_file ); + + // Test default behavior preserves 10-bit depth. + $imagick_image_editor->load(); + $imagick_image_editor->resize( 100, 50 ); + $test_file = tempnam( get_temp_dir(), '' ) . 'test1.avif'; + $imagick_image_editor->save( $test_file ); + $im = new Imagick( $test_file ); + $this->assertSame( 10, $im->getImageDepth() ); + unlink( $test_file ); + $im->destroy(); + + // Test filter can set 8-bit depth + add_filter( 'image_max_bit_depth', array( $this, '__return_eight' ) ); + $imagick_image_editor = new WP_Image_Editor_Imagick( $file ); + $imagick_image_editor->load(); + $imagick_image_editor->resize( 100, 50 ); + $test_file = tempnam( get_temp_dir(), '' ) . 'test2.avif'; + $imagick_image_editor->save( $test_file ); + $im = new Imagick( $test_file ); + $this->assertSame( 8, $im->getImageDepth() ); + unlink( $test_file ); + $im->destroy(); + } + + /** + * Helper function to return 8 for the `image_max_bit_depth` filter. + * + * @return int + */ + public function __return_eight() { + return 8; + } }