Skip to content

REST API: Expose size-aware encode quality on attachment responses#11856

Draft
adamsilverstein wants to merge 1 commit into
WordPress:trunkfrom
adamsilverstein:add/rest-image-quality-field
Draft

REST API: Expose size-aware encode quality on attachment responses#11856
adamsilverstein wants to merge 1 commit into
WordPress:trunkfrom
adamsilverstein:add/rest-image-quality-field

Conversation

@adamsilverstein
Copy link
Copy Markdown
Member

@adamsilverstein adamsilverstein commented May 18, 2026

Summary

Adds an image_quality field to the media attachment REST response. It reports the value of the wp_editor_set_quality filter, resolved against the output MIME type (after image_editor_output_format):

  • default — the filtered quality (1-100) for the full-size image
  • sizes — per-registered-size overrides, included only where the filtered value diverges from default (keeps the payload small)

wp_editor_set_quality has been size-aware since 6.8 (( int $quality, string $mime_type, array $size )), so the filter is re-applied per registered sub-size using that size's dimensions.

This lets client-side media processing (which encodes sub-sizes in the browser) honor a site's configured encode quality instead of a hardcoded default, mirroring the existing exif_orientation field on the same controller.

Trac ticket

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

Backports from Gutenberg

Testing

  • phpunit --filter test_image_quality — schema, default, and size-aware-filter cases
  • phpunit --filter test_get_item_schema — property count updated 32 → 33

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

Add an image_quality field to the media attachment REST response
reporting the wp_editor_set_quality value resolved against the
output MIME type. The default value applies to the full-size image;
per-registered-size overrides are listed under sizes only where the
filtered value diverges from the default.

This lets client-side media processing encode images at the quality
the site has configured, instead of a hardcoded default, mirroring
the existing exif_orientation field.

Props adamsilverstein.
@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.

@TimothyBJacobs
Copy link
Copy Markdown
Member

Should there be a helper function extracted? Looks like quite a bit of logic that will need to stay in sync.

@adamsilverstein adamsilverstein self-assigned this May 19, 2026
@adamsilverstein
Copy link
Copy Markdown
Member Author

adamsilverstein commented May 19, 2026

Should there be a helper function extracted? Looks like quite a bit of logic that will need to stay in sync.

My understanding is that the code will only live in Gutenberg only to support older WordPress versions where the helper wouldn't be available. I believe they only support back a few versions (2?) though, so it can get removed from Gutenberg after some time. Although a helper still might be useful to contain the logic, will consider.

Comment on lines +1075 to +1084
/** This filter is documented in wp-includes/class-wp-image-editor.php */
$full_quality = (int) apply_filters(
'wp_editor_set_quality',
$default_quality,
$output_mime,
array(
'width' => $full_width,
'height' => $full_height,
)
);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this also afterward apply the jpeg_quality filter for image/jpeg? This is what WP_Image_Editor::set_quality() does. Same goes for below.

'readonly' => true,
'properties' => array(
'default' => array(
'type' => 'integer',
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'type' => 'integer',
'type' => 'integer',
'minimum' => 1,
'maximum' => 100,

'sizes' => array(
'type' => 'object',
'additionalProperties' => array(
'type' => 'integer',
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'type' => 'integer',
'type' => 'integer',
'minimum' => 1,
'maximum' => 100,

),
'sizes' => array(
'type' => 'object',
'additionalProperties' => array(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, this could use patternProperties if we wanted to encode all of the image sizes in a regex. But since the image sizes are all known beforehand via wp_get_registered_image_subsizes(), could this not actually explicitly provide all of the sizes?

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.

3 participants