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
6 changes: 4 additions & 2 deletions src/wp-includes/class-wp-image-editor-gd.php
Original file line number Diff line number Diff line change
Expand Up @@ -612,13 +612,15 @@ public function set_quality( $quality = null, $dims = array() ) {
$quality = $this->get_quality();
}

// Use the output mime type if present, matching the fallback parent::set_quality() uses.
$mime_type = ! empty( $this->output_mime_type ) ? $this->output_mime_type : $this->mime_type;

// Handle setting the quality for WebP lossless images, see https://php.watch/versions/8.1/gd-webp-lossless.
try {
if ( 'image/webp' === $this->mime_type && defined( 'IMG_WEBP_LOSSLESS' ) ) {
if ( 'image/webp' === $mime_type && defined( 'IMG_WEBP_LOSSLESS' ) ) {
$webp_info = wp_get_webp_info( $this->file );
if ( ! empty( $webp_info['type'] ) && 'lossless' === $webp_info['type'] ) {
$quality = IMG_WEBP_LOSSLESS;
parent::set_quality( $quality, $dims );
}
}
} catch ( Exception $e ) {
Expand Down
22 changes: 22 additions & 0 deletions tests/phpunit/tests/image/editorGd.php
Original file line number Diff line number Diff line change
Expand Up @@ -674,4 +674,26 @@ public function test_image_non_existent_extension() {

$this->assertTrue( $loaded );
}

/**
* A lossless WebP source's quality should not leak into other output formats.
*
* @ticket 65977
*
* @requires function imagecreatefromwebp
* @requires function imagejpeg
*/
public function test_set_quality_does_not_leak_webp_lossless_quality_into_other_formats() {
$gd_image_editor = new WP_Image_Editor_GD( DIR_TESTDATA . '/images/webp-lossless.webp' );
$loaded = $gd_image_editor->load();

$this->assertNotWPError( $loaded );

$saved = $gd_image_editor->save( null, 'image/jpeg' );

$this->assertNotWPError( $saved );
$this->assertSame( 82, $gd_image_editor->get_quality(), 'Quality for a JPEG output should use the JPEG default, not the WebP lossless sentinel value.' );

unlink( $saved['path'] );
}
}
Loading