wpseek.com
A WordPress-centric search engine for devs and theme authors



wp_get_image_encode_quality › WordPress Function

Since7.1.0
Deprecatedn/a
wp_get_image_encode_quality ( $mime_type, $size = array(), $default_quality = null )
Parameters: (3)
  • (string) $mime_type The output image MIME type, e.g. 'image/jpeg'.
    Required: Yes
  • (array) $size { Optional. Dimensions of the image, passed to the 'wp_editor_set_quality' filter. @type int $width The image width in pixels. @type int $height The image height in pixels. }
    Required: No
    Default: array()
  • (int|null) $default_quality Optional. Starting quality before filters are applied. Defaults to the per-format default (86 for WebP, 82 otherwise).
    Required: No
    Default: null
Returns:
  • (int) Encode quality between 1 and 100.
Defined at:
Codex:

Determines the encode quality WordPress would use for an image.

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.


Source

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;
}