wp_get_image_encode_quality( string $mime_type, array $size = array(), int|null $default_quality = null ): int
- Since
- 7.1.0
- Source
wp-includes/media.php:1006
Description
Resolves the quality the same way WP_Image_Editor::set_quality() does when no explicit quality is supplied: it starts from the per-format default, applies the 'wp_editor_set_quality' filter, then the 'jpeg_quality' filter for JPEG output, resets out-of-range values to the per-format default, and squashes 0 to 1.
This lets code outside of an image editor instance - such as the REST API, which reports the quality client-side processing should use - resolve the same value the server would apply, without loading the image into an editor.
Compatibility
- WordPress
- since 7.1.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in 1 of the 5 tracked releases, added in 7.1.0, and compiles on PHP 7.4 through 8.6-dev.
Parameters
$mime_typestring- The output image MIME type, e.g. 'image/jpeg'.
$sizearrayoptional- Dimensions of the image, passed to the 'wp_editor_set_quality' filter.Default:
array()$widthintThe image width in pixels.$heightintThe image height in pixels.
$default_qualityint|nulloptional- Starting quality before filters are applied.
Defaults to the per-format default (86 for WebP, 82 otherwise).Default:null
Return value
int- Encode quality between 1 and 100.
Performance profile
How much work a call to wp_get_image_encode_quality() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Trivial
- Scaling
- Constant
- Instructions
- 25–39
- Plugin surface
- 2 hooks
- Called by
- 1
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 42.
Third-party callbacks on 'wp_editor_set_quality', 'jpeg_quality' run inside this call, and their cost is not bounded by anything here.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly
What one call costs · 2 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_get_image_encode_quality() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
$mime_type !== "image/jpeg" | 25–33 | apply_filters() |
$mime_type === "image/jpeg" | 31–39 | apply_filters(), apply_filters() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 42 | 25–39 | 7 | |
| 8.5 | 42 | 25–39 | 7 | |
| 8.4 | 42 | 25–39 | 7 | 2 fewer instructions than PHP 8.3 |
| 8.3 | 44 | 27–41 | 7 | |
| 8.2 | 44 | 27–41 | 7 | |
| 8.1 | 44 | 27–41 | 7 | |
| 7.4 | 44 | 27–41 | 7 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 2
2 hooks fire while wp_get_image_encode_quality() runs, in this order:
- apply_filters( wp_editor_set_quality )filterline 1013 (+7 into the body)
Filters the default image compression quality setting.
- apply_filters( jpeg_quality )filterline 1017 (+11 into the body)
Filters the JPEG compression quality for backward-compatibility.
Uses · 1
- apply_filters()Calls the callback functions that have been added to a filter hook.
Used by · 1
- WP_REST_Attachments_Controller::prepare_item_for_response()Prepares a single attachment output for response.
Source code
function wp_get_image_encode_quality( string $mime_type, array $size = array(), ?int $default_quality = null ): int { if ( null === $default_quality ) { // Mirror WP_Image_Editor::get_default_quality(): WebP defaults to 86, everything else to 82. $default_quality = ( 'image/webp' === $mime_type ) ? 86 : 82; } /** This filter is documented in wp-includes/class-wp-image-editor.php */ $quality = apply_filters( 'wp_editor_set_quality', $default_quality, $mime_type, $size ); if ( 'image/jpeg' === $mime_type ) { /** This filter is documented in wp-includes/class-wp-image-editor.php */ $quality = apply_filters( 'jpeg_quality', $quality, 'image_resize' ); } if ( ! is_numeric( $quality ) ) { $quality = $default_quality; } else { $quality = (int) $quality; } // Reset out-of-range values to the default, matching WP_Image_Editor::set_quality(). if ( $quality < 0 || $quality > 100 ) { $quality = $default_quality; } // Allow 0, but squash to 1, matching WP_Image_Editor::set_quality(). if ( 0 === $quality ) { $quality = 1; } return $quality;}Changelog
Introduced in 7.1.0.
Signature, return type and hooks compared across 1 parsed release.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/media.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.