diff --git a/src/wp-includes/class-wp-image-editor.php b/src/wp-includes/class-wp-image-editor.php index df07c4229b623..c0712fabe8a87 100644 --- a/src/wp-includes/class-wp-image-editor.php +++ b/src/wp-includes/class-wp-image-editor.php @@ -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. @@ -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; diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index 3e84e97f40689..4fc9be780c732 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -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 { @@ -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 @@ -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 */ diff --git a/tests/phpunit/tests/image/editor.php b/tests/phpunit/tests/image/editor.php index 596788e99b1fe..a1646a9b177c0 100644 --- a/tests/phpunit/tests/image/editor.php +++ b/tests/phpunit/tests/image/editor.php @@ -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). diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php index af84065113267..838a253b028fe 100644 --- a/tests/phpunit/tests/media.php +++ b/tests/phpunit/tests/media.php @@ -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' ) ); } /**