wppaste
WordPress

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
Determines the encode quality WordPress would use for an image.

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()
  • $widthint

    The image width in pixels.
  • $heightint

    The 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

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
25–39

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 42.

Plugin surface
2 hooks

Third-party callbacks on 'wp_editor_set_quality', 'jpeg_quality' run inside this call, and their cost is not bounded by anything here.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_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.

WhenInstructionsCalls it makes
$mime_type !== "image/jpeg"25–33apply_filters()
$mime_type === "image/jpeg"31–39apply_filters(), apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev4225–397
8.54225–397
8.44225–3972 fewer instructions than PHP 8.3
8.34427–417
8.24427–417
8.14427–417
7.44427–417

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:

  1. apply_filters( wp_editor_set_quality )filterline 1013 (+7 into the body)

    Filters the default image compression quality setting.

  2. 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

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.