Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/wp-includes/class-wp-image-editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ public function set_quality( $quality = null, $dims = array() ) {
* Returns the default compression quality setting for the mime type.
*
* @since 5.8.1
* @since 7.3.0 Added a default quality of 74 for AVIF.
*
* @param string $mime_type
* @return int The default quality setting for the mime type.
Expand All @@ -319,6 +320,9 @@ protected function get_default_quality( $mime_type ) {
case 'image/webp':
$quality = 86;
break;
case 'image/avif':
$quality = 74;
break;
case 'image/jpeg':
default:
$quality = $this->default_quality;
Expand Down
17 changes: 14 additions & 3 deletions src/wp-includes/media.php
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,7 @@ function wp_get_registered_image_subsizes(): array {
* server would apply, without loading the image into an editor.
*
* @since 7.1.0
* @since 7.3.0 Added a default quality of 74 for AVIF.
*
* @param string $mime_type The output image MIME type, e.g. 'image/jpeg'.
* @param array $size {
Expand All @@ -995,7 +996,8 @@ function wp_get_registered_image_subsizes(): array {
* @type int $height The image height in pixels.
* }
* @param int|null $default_quality Optional. Starting quality before filters are applied.
* Defaults to the per-format default (86 for WebP, 82 otherwise).
* Defaults to the per-format default (86 for WebP, 74 for AVIF,
* 82 otherwise).
* @return int Encode quality between 1 and 100.
*
* @phpstan-param non-empty-string $mime_type
Expand All @@ -1005,8 +1007,17 @@ function wp_get_registered_image_subsizes(): array {
*/
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;
// Mirror WP_Image_Editor::get_default_quality(): WebP defaults to 86, AVIF to 74, everything else to 82.
switch ( $mime_type ) {
case 'image/webp':
$default_quality = 86;
break;
case 'image/avif':
$default_quality = 74;
break;
default:
$default_quality = 82;
}
}

/** This filter is documented in wp-includes/class-wp-image-editor.php */
Expand Down
31 changes: 31 additions & 0 deletions tests/phpunit/tests/image/editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,37 @@ public function test_set_quality_with_image_conversion() {
);
}

/**
* Tests that the default quality for AVIF is 74.
*
* @ticket 66677
*/
public function test_set_quality_default_for_avif() {
$editor = wp_get_image_editor( DIR_TESTDATA . '/images/avif-lossy.avif' );
$editor->set_mime_type( 'image/avif' );

$this->assertSame( 74, $editor->get_quality(), 'Default quality setting for AVIF should be 74.' );

unset( $editor );

add_filter(
'image_editor_output_format',
static function ( $formats ) {
$formats['image/jpeg'] = 'image/avif';
return $formats;
}
);

$editor = wp_get_image_editor( DIR_TESTDATA . '/images/test-image.jpg' );
$editor->set_mime_type( 'image/jpeg' );

$this->assertSame( 82, $editor->get_quality(), 'Default quality setting for JPEG should be 82.' );

// A JPEG image will be converted to AVIF whose quality should be 74.
$editor->save();
$this->assertSame( 74, $editor->get_quality(), 'Image conversion from JPEG to AVIF. Quality setting for it should be 74.' );
}

/**
* Changes the output format when editing images. PNG and JPEG files
* will be converted to WEBP (if the image editor in PHP supports it).
Expand Down
4 changes: 3 additions & 1 deletion tests/phpunit/tests/media.php
Original file line number Diff line number Diff line change
Expand Up @@ -7898,13 +7898,15 @@ private static function get_insufficient_width_height_for_high_priority(): array

/**
* @ticket 65262
* @ticket 66677
* @covers ::wp_get_image_encode_quality
*/
public function test_wp_get_image_encode_quality_defaults() {
// JPEG (and any non-WebP) defaults to 82, WebP to 86.
// JPEG (and any other format) defaults to 82, WebP to 86, AVIF to 74.
$this->assertSame( 82, wp_get_image_encode_quality( 'image/jpeg' ) );
$this->assertSame( 82, wp_get_image_encode_quality( 'image/png' ) );
$this->assertSame( 86, wp_get_image_encode_quality( 'image/webp' ) );
$this->assertSame( 74, wp_get_image_encode_quality( 'image/avif' ) );
}

/**
Expand Down
Loading