wppaste
WordPress

WP_Image_Editor::set_quality( int $quality = null, array $dims = array() ): true|WP_Error

Since
3.5.0, 6.8.0
Source
wp-includes/class-wp-image-editor.php:242
Sets Image Compression quality on a 1-100% scale.

Compatibility

WordPress
since 6.8.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$qualityintoptional
Compression Quality. Range: [1,100]Default: null
$dimsarrayoptional
Image dimensions array with 'width' and 'height' keys.Default: array()

Return value

true|WP_Error
True if set successfully; WP_Error on failure.

Performance profile

How much work a call to WP_Image_Editor::set_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
21–51

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

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
4

4 places 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

Further down the call graph this can also reach query, option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 6 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Image_Editor::set_quality() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
$quality !== null && $quality21–23->get_default_quality()
$quality !== null && !$quality24–28->get_default_quality(), __()
$quality === null && $mime_type !== "image/jpeg" && $quality35–40->get_default_quality(), apply_filters()
$quality === null && $mime_type !== "image/jpeg" && !$quality38–45->get_default_quality(), apply_filters(), __()
$quality === null && $mime_type === "image/jpeg" && $quality41–46->get_default_quality(), apply_filters(), apply_filters()
$quality === null && $mime_type === "image/jpeg" && !$quality44–51->get_default_quality(), apply_filters(), apply_filters(), __()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev5621–519
8.55621–519
8.45621–519
8.35621–519
8.25621–519
8.15621–5193 fewer instructions than PHP 7.4
7.45922–529

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_Image_Editor::set_quality() runs, in this order:

  1. apply_filters( wp_editor_set_quality )filterline 269 (+27 into the body)

    Filters the default image compression quality setting.

  2. apply_filters( jpeg_quality )filterline 288 (+46 into the body)

    Filters the JPEG compression quality for backward-compatibility.

Uses · 4

Used by · 4

Source code

	public function set_quality( $quality = null, $dims = array() ) {		// Use the output mime type if present. If not, fall back to the input/initial mime type.		$mime_type = ! empty( $this->output_mime_type ) ? $this->output_mime_type : $this->mime_type;		// Get the default quality setting for the mime type.		$default_quality = $this->get_default_quality( $mime_type ); 		if ( null === $quality ) {			/**			 * Filters the default image compression quality setting.			 *			 * Applies only during initial editor instantiation, or when set_quality() is run			 * manually without the `$quality` argument.			 *			 * The WP_Image_Editor::set_quality() method has priority over the filter.			 *			 * @since 3.5.0			 * @since 6.8.0 Added the size parameter.			 *			 * @param int    $quality   Quality level between 1 (low) and 100 (high).			 * @param string $mime_type Image mime type.			 * @param array $size {			 *     Dimensions of the image.			 *			 *     @type int $width  The image width.			 *     @type int $height The image height.			 * }			 */			$quality = apply_filters( 'wp_editor_set_quality', $default_quality, $mime_type, $dims ? $dims : $this->size ); 			if ( 'image/jpeg' === $mime_type ) {				/**				 * Filters the JPEG compression quality for backward-compatibility.				 *				 * Applies only during initial editor instantiation, or when set_quality() is run				 * manually without the `$quality` argument.				 *				 * The WP_Image_Editor::set_quality() method has priority over the filter.				 *				 * The filter is evaluated under two contexts: 'image_resize', and 'edit_image',				 * (when a JPEG image is saved to file).				 *				 * @since 2.5.0				 *				 * @param int    $quality Quality level between 0 (low) and 100 (high) of the JPEG.				 * @param string $context Context of the filter.				 */				$quality = apply_filters( 'jpeg_quality', $quality, 'image_resize' );			} 			if ( $quality < 0 || $quality > 100 ) {				$quality = $default_quality;			}		} 		// Allow 0, but squash to 1 due to identical images in GD, and for backward compatibility.		if ( 0 === $quality ) {			$quality = 1;		} 		if ( ( $quality >= 1 ) && ( $quality <= 100 ) ) {			$this->quality = $quality;			return true;		} else {			return new WP_Error( 'invalid_image_quality', __( 'Attempted to set image quality outside of the range [1,100].' ) );		}	}

Changelog

Introduced in 3.5.0. One change between 6.7.7 and 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

6.8.8
Parameter $dims added.verified against source
6.8.0
The $dims parameter was added.from the docblock
3.5.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.9.7 tag, from src/wp-includes/class-wp-image-editor.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.