Skip to content

Media: Fix GD editor leaking WebP-lossless quality into other output formats - #13300

Open
irozum wants to merge 2 commits into
WordPress:trunkfrom
irozum:fix/65977-gd-editor-webp-quality-conversion
Open

Media: Fix GD editor leaking WebP-lossless quality into other output formats#13300
irozum wants to merge 2 commits into
WordPress:trunkfrom
irozum:fix/65977-gd-editor-webp-quality-conversion

Conversation

@irozum

@irozum irozum commented Aug 27, 2026

Copy link
Copy Markdown

WP_Image_Editor_GD::set_quality() checks $this->mime_type (the source image's mime type) instead of the output mime type when deciding whether to apply the WebP-lossless quality sentinel (IMG_WEBP_LOSSLESS, 101). Because of that, converting a lossless WebP source to any other format (AVIF, JPEG, etc.) still gets the quality forced to 101 — a value that is intentionally outside the normal [1,100] range and only valid for GD's WebP lossless encoder. On PHP 8.4+, passing 101 to imageavif()/imagejpeg() throws a ValueError, producing a fatal error, 0-byte sub-sizes, and broken wp media regenerate runs; on older PHP it silently applies the wrong quality to the non-WebP output.

This fixes the mime check to use $this->output_mime_type when it's set (falling back to $this->mime_type, matching the same fallback the parent class already uses in WP_Image_Editor::set_quality()), so the lossless override is only applied when the image is actually being encoded as WebP. It also removes the redundant parent::set_quality( $quality, $dims ) call for the 101 sentinel, since that value is always rejected by the parent's [1,100] validation and its WP_Error return was being silently discarded — the direct $this->quality assignment right below it was already the only thing doing anything.

Added a PHPUnit test (Tests_Image_Editor_GD::test_set_quality_does_not_leak_webp_lossless_quality_into_other_formats) that loads a lossless WebP fixture, saves it as JPEG, and asserts the quality used is the normal JPEG default (82) rather than the leaked WebP-lossless sentinel (101).

Trac ticket: https://core.trac.wordpress.org/ticket/65977

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Sonnet 5
Used for: Root-cause analysis, implementation, tests, and PR description. Reviewed by Igor Rozum.


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

@github-actions

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props irozum.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant