Skip to content

Commit

Permalink
Media: enable high bit depth resized image output with Imagick.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
adamsilverstein committed Jan 7, 2025
1 parent 4cd96f7 commit 2bab211
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/wp-includes/class-wp-image-editor-imagick.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() );
Expand Down
67 changes: 67 additions & 0 deletions tests/phpunit/tests/image/editorImagick.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 2bab211

Please sign in to comment.