Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
4d54810
Add function to convert palette PNGs to true color during upload
b1ink0 May 27, 2025
82cf154
Refactor to use `imagepalettetotruecolor()` to convert uploaded palet…
b1ink0 May 27, 2025
cf97730
Add test for converting palette PNG to truecolor
b1ink0 May 30, 2025
8ad675c
Add hooks for palette PNG to truecolor conversion and tests for those…
b1ink0 May 30, 2025
85482bc
Merge branch 'trunk' into fix/palette-pngs-to-avif-webp-conversion
b1ink0 Jun 2, 2025
ef9a9f9
Add type safety guard for palette PNG conversion filter
b1ink0 Jun 5, 2025
7443e68
Improve PNG type checking
b1ink0 Jun 10, 2025
731d3b0
Fix typo in function description "true color" to "truecolor"
b1ink0 Jun 11, 2025
7f757c7
Separate image creation failure and truecolor checks for clarity
b1ink0 Jun 11, 2025
e646941
Refactor test for palette PNG conversion to remove unnecessary setup …
b1ink0 Jun 16, 2025
56e8dac
Add test to assert converted image filesize does not exceed original
b1ink0 Jun 17, 2025
9ff40f2
Remove unused import
b1ink0 Jun 24, 2025
d930ea9
Improve test code coverage
b1ink0 Jul 10, 2025
2b1d612
Merge branch 'trunk' into fix/palette-pngs-to-avif-webp-conversion
adamsilverstein Jul 19, 2025
ca8599c
Remove redundant GD function checks
b1ink0 Jul 21, 2025
8244cae
Return non-array file parameter unchanged
b1ink0 Jul 23, 2025
62f767c
Fix PHPStan and test failures caused by returning a mixed value
b1ink0 Jul 23, 2025
e39b2c7
Improve file type check by using `wp_check_filetype_and_ext`
b1ink0 Jul 23, 2025
37b893b
Merge branch 'trunk' into fix/palette-pngs-to-avif-webp-conversion
b1ink0 Jul 23, 2025
70ad323
Merge branch 'trunk' into fix/palette-pngs-to-avif-webp-conversion
adamsilverstein Jul 30, 2025
8f8a614
Convert invalid input to empty array
b1ink0 Aug 19, 2025
aa4c927
Merge branch 'trunk' of https://github.com/WordPress/performance into…
westonruter Aug 19, 2025
7e6e70c
Skip test if GD extension is not loaded
westonruter Aug 19, 2025
15fd0ea
Address minor static analysis nits
westonruter Aug 19, 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
66 changes: 64 additions & 2 deletions plugins/webp-uploads/hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ function webp_uploads_remove_sources_files( int $attachment_id ): void {
}
}
}
add_action( 'delete_attachment', 'webp_uploads_remove_sources_files', 10, 1 );
add_action( 'delete_attachment', 'webp_uploads_remove_sources_files' );

/**
* Filters `wp_content_img_tag` to update images so that they use the preferred MIME type where possible.
Expand Down Expand Up @@ -662,7 +662,7 @@ function webp_uploads_img_tag_update_mime_type( string $original_image, string $
}

/**
* Updates the references of the featured image to the a new image format if available, in the same way it
* Updates the references of the featured image to the new image format if available, in the same way it
* occurs in the_content of a post.
*
* @since 1.0.0
Expand Down Expand Up @@ -901,3 +901,65 @@ function webp_uploads_enable_additional_mime_type_support_for_all_sizes( array $
return $allowed_sizes;
}
add_filter( 'webp_uploads_image_sizes_with_additional_mime_type_support', 'webp_uploads_enable_additional_mime_type_support_for_all_sizes' );

/**
* Converts palette PNG images to truecolor PNG images.
*
* GD cannot convert palette-based PNG to WebP/AVIF formats, causing conversion failures.
Comment thread
adamsilverstein marked this conversation as resolved.
Resolved
* This function detects and converts palette PNG to truecolor during upload.
*
* @since n.e.x.t
*
* @param array<string, mixed>|mixed $file The uploaded file data.
* @return array<string, mixed> The modified file data.
*/
function webp_uploads_convert_palette_png_to_truecolor( $file ): array {
// Because plugins do bad things.
if ( ! is_array( $file ) ) {
$file = array();
Comment thread
b1ink0 marked this conversation as resolved.
Resolved
}
if ( ! isset( $file['tmp_name'], $file['name'] ) ) {
return $file;
}
if ( isset( $file['type'] ) && is_string( $file['type'] ) ) {
if ( 'image/png' !== strtolower( $file['type'] ) ) {
Comment thread
adamsilverstein marked this conversation as resolved.
Resolved
return $file;
}
} elseif ( 'image/png' !== wp_check_filetype_and_ext( $file['tmp_name'], $file['name'] )['type'] ) {
return $file;
}

$editor = wp_get_image_editor( $file['tmp_name'] );

if ( is_wp_error( $editor ) || ! $editor instanceof WP_Image_Editor_GD ) {
Comment thread
adamsilverstein marked this conversation as resolved.
Resolved
return $file;
}

$image = imagecreatefrompng( $file['tmp_name'] );

// Check if the image was created successfully.
if ( false === $image ) {
return $file;
}

// Check if the image is already truecolor.
if ( imageistruecolor( $image ) ) {
imagedestroy( $image );
return $file;
}

// Preserve transparency.
imagealphablending( $image, false );
imagesavealpha( $image, true );

// Convert the palette to truecolor.
if ( imagepalettetotruecolor( $image ) ) {
// Overwrite the upload with the new truecolor PNG.
imagepng( $image, $file['tmp_name'] );
}
imagedestroy( $image );

return $file;
Comment thread
adamsilverstein marked this conversation as resolved.
Resolved
}
add_filter( 'wp_handle_upload_prefilter', 'webp_uploads_convert_palette_png_to_truecolor' );
Comment thread
adamsilverstein marked this conversation as resolved.
Resolved
add_filter( 'wp_handle_sideload_prefilter', 'webp_uploads_convert_palette_png_to_truecolor' );
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
154 changes: 153 additions & 1 deletion plugins/webp-uploads/tests/test-load.php
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ public function test_it_should_allow_the_upload_of_a_webp_image_if_at_least_one_
static function ( $editors ) {
// WP core does not choose the WP_Image_Editor instance based on MIME type support,
// therefore the one that does support modern images needs to be first in this list.
array_unshift( $editors, 'WP_Image_Doesnt_Support_Modern_Images' );
array_unshift( $editors, WP_Image_Doesnt_Support_Modern_Images::class );
return $editors;
}
);
Expand Down Expand Up @@ -1116,4 +1116,156 @@ public function test_that_it_should_convert_webp_to_avif_on_upload(): void {
}
wp_delete_attachment( $attachment_id );
}

/**
* Tests that the `webp_uploads_convert_palette_png_to_truecolor` function is hooked to the upload filters.
*/
public function test_webp_uploads_convert_palette_png_to_truecolor_hooks(): void {
$this->assertSame( 10, has_filter( 'wp_handle_upload_prefilter', 'webp_uploads_convert_palette_png_to_truecolor' ) );
$this->assertSame( 10, has_filter( 'wp_handle_sideload_prefilter', 'webp_uploads_convert_palette_png_to_truecolor' ) );
}

/**
* Tests converting a palette PNG to a truecolor PNG.
*
* @dataProvider data_to_test_webp_uploads_convert_palette_png_to_truecolor
*
* @covers ::webp_uploads_convert_palette_png_to_truecolor
*
* @param string|null $image_path The path to the image file to test.
* @param bool $expect_changed Whether the png should be converted to truecolor.
*/
public function test_webp_uploads_convert_palette_png_to_truecolor( ?string $image_path, bool $expect_changed ): void {
if ( ! extension_loaded( 'gd' ) ) {
$this->markTestSkipped( 'GD extension is not loaded' );
}

add_filter(
'wp_image_editors',
static function () {
return array( WP_Image_Editor_GD::class );
}
);

// Temp file will be copied and unlinked by WordPress core during sideload processing.
$tmp_file = wp_tempnam();
copy( $image_path, $tmp_file );
$file = array(
'name' => basename( $image_path ),
'tmp_name' => $tmp_file,
'type' => wp_check_filetype( $image_path )['type'],
'size' => filesize( $tmp_file ),
'error' => UPLOAD_ERR_OK,
);

// Store the original file hash and the original file size for later comparison.
$original_file_hash = isset( $file['tmp_name'] ) ? md5_file( $file['tmp_name'] ) : '';
$original_file_size = (int) filesize( $file['tmp_name'] );

// This will trigger the `wp_handle_sideload_prefilter` filter.
$attachment_id = media_handle_sideload( $file );

try {
$this->assertIsNumeric( $attachment_id );

// For getting an original image path for computation of the file hash.
$meta = wp_get_attachment_metadata( $attachment_id );
$upload_dir = wp_get_upload_dir();
$path = null;
if ( isset( $meta['original_image'], $meta['file'] ) ) {
$path = path_join(
$upload_dir['basedir'],
dirname( $meta['file'] ) . '/' . $meta['original_image']
);
}
$this->assertNotNull( $path );
$this->assertFileExists( $path );

// Hash will be modified if the image was converted to truecolor.
$modified_file_hash = md5_file( $path );

if ( ! $expect_changed ) {
$this->assertSame( $original_file_hash, $modified_file_hash );
} else {
$this->assertNotSame( $original_file_hash, $modified_file_hash );
$img = imagecreatefrompng( $path );
$this->assertTrue( imageistruecolor( $img ) );
imagedestroy( $img );

// Make sure the image converted to modern image format is not 0 bytes.
$modern_image_format_path = get_attached_file( $attachment_id );
$this->assertNotFalse( $modern_image_format_path );
$this->assertFileExists( $modern_image_format_path );
$modern_image_format_filesize = (int) filesize( $modern_image_format_path );
$this->assertGreaterThan( 0, $modern_image_format_filesize );

// Ensure the file size of the converted image is less than or equal to the original indexed PNG file size.
$this->assertLessThanOrEqual( $original_file_size, $modern_image_format_filesize );
}
} finally {
wp_delete_attachment( $attachment_id );
}
}

/**
* Data provider for `test_webp_uploads_convert_palette_png_to_truecolor`.
*
* @return array<string, mixed> Returns an array of test cases.
*/
public function data_to_test_webp_uploads_convert_palette_png_to_truecolor(): array {
$non_palette_png = TESTS_PLUGIN_DIR . '/tests/data/images/dice.png';
$palette_png = TESTS_PLUGIN_DIR . '/tests/data/images/dice-palette.png';
$test_jpg = TESTS_PLUGIN_DIR . '/tests/data/images/leaves.jpg';

return array(
'wrong_extension' => array(
'image_path' => $test_jpg,
'expected_changed' => false,
),
'non_palette_png' => array(
'image_path' => $non_palette_png,
'expected_changed' => false,
),
'palette_png' => array(
'image_path' => $palette_png,
'expected_changed' => true,
),
);
}

/**
* Tests the webp_uploads_convert_palette_png_to_truecolor function with various conditions.
*
* @covers ::webp_uploads_convert_palette_png_to_truecolor
*/
public function test_webp_uploads_convert_palette_png_to_truecolor_conditions(): void {
$this->assertSame( array(), webp_uploads_convert_palette_png_to_truecolor( 'test' ) );
$this->assertSameSets( array(), webp_uploads_convert_palette_png_to_truecolor( array() ) );

$file = array(
'tmp_name' => TESTS_PLUGIN_DIR . '/tests/data/images/leaves.jpg',
'name' => 'leaves.jpg',
'type' => 'image/jpeg',
);
$this->assertSameSets( $file, webp_uploads_convert_palette_png_to_truecolor( $file ) );

$file = array(
'tmp_name' => TESTS_PLUGIN_DIR . '/tests/data/images/leaves.jpg',
'name' => 'leaves.jpg',
);
$this->assertSameSets( $file, webp_uploads_convert_palette_png_to_truecolor( $file ) );

add_filter(
'wp_image_editors',
static function () {
return array();
}
);
$file = array(
'tmp_name' => TESTS_PLUGIN_DIR . '/tests/data/images/dice-palette.png',
'name' => 'dice-palette.png',
'type' => 'image/png',
);
$this->assertSameSets( $file, webp_uploads_convert_palette_png_to_truecolor( $file ) );
}
}