Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
11 changes: 11 additions & 0 deletions src/wp-includes/class-wp-image-editor-gd.php
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,17 @@ protected function _save( $image, $filename = null, $mime_type = null ) {
imageinterlace( $image, apply_filters( 'image_save_progressive', false, $mime_type ) );
}

// Ensure GD can handle WebP/AVIF output of palette based images.
if (
in_array( $mime_type, array( 'image/webp', 'image/avif' ), true ) &&
! imageistruecolor( $image )
) {
// Preserve transparence and prepare for conversion.
imagealphablending( $image, false );
imagesavealpha( $image, true );
imagepalettetotruecolor( $image );
}

if ( 'image/gif' === $mime_type ) {
if ( ! $this->make_image( $filename, 'imagegif', array( $image, $filename ) ) ) {
return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) );
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions tests/phpunit/tests/image/editorGd.php
Original file line number Diff line number Diff line change
Expand Up @@ -670,4 +670,39 @@ public function test_image_non_existent_extension() {

$this->assertTrue( $loaded );
}

/**
* Tests that GD can convert a palette image to WebP.
*
* @ticket 63773
*/
public function test_convert_palette_image_to_webp() {
if ( ! ( imagetypes() & IMG_WEBP ) ) {
$this->markTestSkipped( 'This test requires WEBP support.' );
}

$file = DIR_TESTDATA . '/images/png-tests/dice-palette.png';

// Use the `image_editor_output_format` filter to set the output to WebP.
add_filter(
'image_editor_output_format',
function () {
return array(
'image/png' => 'image/webp',
);
}
);

$gd_image_editor = new WP_Image_Editor_GD( $file );

$gd_image_editor->load();

$save_to_file = tempnam( get_temp_dir(), '' ) . '.webp';

$gd_image_editor->save( $save_to_file );

$this->assertFileExists( $save_to_file );

unlink( $save_to_file );
}
}
Loading