Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
b394c6f
Add handling for indexed-color image type in WP Image Editor
Apr 26, 2024
71fdfba
Add handling for indexed-color image type in WP Image Editor
Apr 26, 2024
55bc78e
Update src/wp-includes/class-wp-image-editor-imagick.php
pbearne Apr 29, 2024
0e41582
Refactor image resize test to use data provider
May 13, 2024
40eb2d5
detect indexed color PNG images and quantize after resizing
nosilver4u May 22, 2024
e14b706
combine alpha code and make png chunks conditional
nosilver4u May 24, 2024
dbe06e4
fix palette image type condition
nosilver4u May 28, 2024
4759963
prevent grayscale conversion and only preserve tRNS for alpha
nosilver4u May 30, 2024
7f74d0d
reduce the max colors (no buffer)
nosilver4u May 30, 2024
18d3996
Merge branch 'refs/heads/trunk' into 36477-keep-color-depth
May 31, 2024
2c08b7c
Merge remote-tracking branch 'refs/remotes/nosilver4u/png8-resize' in…
May 31, 2024
000e5dd
end of day
Jun 5, 2024
cadb889
Merge branch 'refs/heads/trunk' into 36477-keep-color-depth
Jun 5, 2024
e961463
Refactor png color depth test and clean debug code
Jun 7, 2024
3baddf7
Merge branch 'png8-resize' into ticket/36477
adamsilverstein Oct 11, 2024
16db1b9
Merge branch '36477-keep-color-depth' into ticket/36477
adamsilverstein Oct 11, 2024
7e6559d
test cleanup
adamsilverstein Oct 11, 2024
3602b4b
phpcbf
adamsilverstein Oct 11, 2024
2d09025
Try 25% reduction in dimension
adamsilverstein Oct 11, 2024
889fb48
remove deskcat8
adamsilverstein Oct 11, 2024
cb09469
Merge branch 'trunk' into ticket/36477
adamsilverstein Oct 17, 2024
7d12395
Fix tests and restore removed files
adamsilverstein Oct 17, 2024
b4e4386
phpcbf
adamsilverstein Oct 17, 2024
dcb0691
rework to avoid custom file parsing
adamsilverstein Oct 17, 2024
de7a383
Simplify max_color calculations
adamsilverstein Oct 17, 2024
6eee9c0
fix conditional
adamsilverstein Oct 17, 2024
64d13cf
Set output image depth to 8 when input is 8 or below
adamsilverstein Oct 17, 2024
45e48f0
Docs cleanup, delay max_color calculation
adamsilverstein Oct 17, 2024
fd8fbbe
remove tests for get_png_color_depth
adamsilverstein Oct 17, 2024
2de2a3d
remove unused test files
adamsilverstein Oct 17, 2024
b349873
Remove some unused code
adamsilverstein Oct 17, 2024
6180ffc
cleanup
adamsilverstein Oct 17, 2024
fef66ca
Merge branch 'trunk' into ticket/36477
adamsilverstein Jan 7, 2025
024a9ee
Merge branch 'trunk' into ticket/36477
adamsilverstein Jan 7, 2025
fdba503
Restore test after trunk merge
adamsilverstein Jan 7, 2025
29b4502
remove unused test image
adamsilverstein Jan 7, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion src/wp-includes/class-wp-image-editor-imagick.php
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,38 @@ protected function thumbnail_image( $dst_w, $dst_h, $filter_name = 'FILTER_TRIAN
$this->image->setOption( 'png:compression-filter', '5' );
$this->image->setOption( 'png:compression-level', '9' );
$this->image->setOption( 'png:compression-strategy', '1' );
$this->image->setOption( 'png:exclude-chunk', 'all' );
// Check to see if a PNG is indexed, and find the pixel depth.
if ( is_callable( array( $this->image, 'getImageDepth' ) ) ) {
$indexed_pixel_depth = $this->image->getImageDepth();

// Indexed PNG files get some additional handling.
if ( 0 < $indexed_pixel_depth && 8 >= $indexed_pixel_depth ) {
// Check for an alpha channel.
if (
is_callable( array( $this->image, 'getImageAlphaChannel' ) )
&& $this->image->getImageAlphaChannel()
) {
$this->image->setOption( 'png:include-chunk', 'tRNS' );
} else {
$this->image->setOption( 'png:exclude-chunk', 'all' );
}

// Reduce colors in the images to maximum needed, using the global colorspace.
$max_colors = pow( 2, $indexed_pixel_depth );
if ( is_callable( array( $this->image, 'getImageColors' ) ) ) {
$current_colors = $this->image->getImageColors();
$max_colors = min( $max_colors, $current_colors );
}
$this->image->quantizeImage( $max_colors, $this->image->getColorspace(), 0, false, false );

/**
* If the colorspace is 'gray', use the png8 format to ensure it stays indexed.
*/
if ( Imagick::COLORSPACE_GRAY === $this->image->getImageColorspace() ) {
$this->image->setOption( 'png:format', 'png8' );
}
}
}
}

/*
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/phpunit/data/images/png-tests/deskcat8.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/phpunit/data/images/png-tests/test8.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
53 changes: 53 additions & 0 deletions tests/phpunit/tests/image/editorImagick.php
Original file line number Diff line number Diff line change
Expand Up @@ -758,4 +758,57 @@ public function test_image_max_bit_depth() {
public function __return_eight() {
return 8;
}

/**
* Test that resizes are smaller for 16 bit PNG images.
*
* @ticket 36477
*
* @dataProvider data_resizes_are_small_for_16bit_images
*/
public function test_resizes_are_small_for_16bit_images( $file ) {

$temp_file = DIR_TESTDATA . '/images/test-temp.png';

$imagick_image_editor = new WP_Image_Editor_Imagick( $file );
$imagick_image_editor->load();
$size = $imagick_image_editor->get_size();

$org_filesize = filesize( $file );

$imagick_image_editor->resize( $size['width'] * .5, $size['height'] * .5 );

$saved = $imagick_image_editor->save( $temp_file );

$new_filesize = filesize( $temp_file );

unlink( $temp_file );

$this->assertLessThan( $org_filesize, $new_filesize, 'The resized image file size is not smaller than the original file size.' );
}

/**
* data_test_resizes_are_small_for_16bit
*
* @return array[]
*/
public static function data_resizes_are_small_for_16bit_images() {
return array(
'cloudflare-status' => array(
DIR_TESTDATA . '/images/png-tests/cloudflare-status.png',
),
'deskcat8' => array(
DIR_TESTDATA . '/images/png-tests/deskcat8.png',
),
'17-c3-duplicate-entries' => array(
DIR_TESTDATA . '/images/png-tests/Palette_icon-or8.png',
),
'rabbit-time-paletted' => array(
DIR_TESTDATA . '/images/png-tests/rabbit-time-paletted-or8.png',
),
'test8' => array(
DIR_TESTDATA . '/images/png-tests/test8.png',
),
);
}
}
Loading