Make WordPress Core

Opened 2 weeks ago

Last modified 2 weeks ago

#61614 new defect (bug)

Media: Ensure Imagick quality is set correctly

Reported by: adamsilverstein's profile adamsilverstein Owned by:
Milestone: 6.7 Priority: normal
Severity: normal Version: 6.5
Component: General Keywords: has-patch needs-testing
Focuses: Cc:

Description

Summary

Imagick does not properly set image quality for AVIF images.

A user reported an issue in the Performance Lab plugin "wp_editor_set_quality not changing image quality for AVIF images". Link: https://github.com/WordPress/performance/issues/1331. When investigating this issue I found that the quality setting being applied in core doesn't work correctly when outputting AVIF images. When we added AVIF support in 6.5 we suggested using the wp_editor_set_quality filter control the AVIF quality setting, but this currently doesn't work (at least with Imagick).

Steps to reproduce

Here are the testing instructions:

  1. Test on a server with Imagick enabled AVIF support. Check under Tools->Siet Health->Info to verify Imagick has AVIF in list of supported types.
  2. Upload an AVIF image, verify all of the sub-sized (AVIF) images are created.
  3. Add the following code snippet in your theme or an mu plugin:
    function filter_avif_quality( $quality, $mime_type ) {
    	if ( 'image/avif' === $mime_type ) {
    		return 15;
    	}
    	return $quality;
    }
    add_filter( 'wp_editor_set_quality', 'filter_avif_quality', 10, 2 );
    
  4. Upload the same image as in step 2. the filter should be applied and the very low quality (15) should be used.
  5. Expected result: the generated AVIF images are much smaller sized and lower quality looking (noticeable artifacts).Actual result: the generated images from step 2 are the same as the images generated in step 4 - the quality setting has no effect.

Previous Discussion

In this post the author encounters the same issue. The answer that resolves the issue is below:

I found the solution, Thanks to Danack from Imagick https://github.com/Imagick/imagick/.
There are two functions to set the compression quality:
setImageCompressionQuality
setCompressionQuality
According to the online documentation
https://www.php.net/manual/en/imagick.setcompressionquality.php ... setCompressionQuality is for new images, and setImageCompressionQuality for already
existing ones.

Solution

I discovered that adding
$this->image->setCompressionQuality( $quality ); where we currently call setImageCompressionQuality already fixes the issue.

I am not clear if this issue affects other formats in any way (it didn't in my initial testing). However adding the additional quality setting call seems like a safe change to add and this will fix the issue for AVIFs.

Attachments (1)

61614.diff (1.3 KB) - added by adamsilverstein 2 weeks ago.

Download all attachments as: .zip

Change History (5)

This ticket was mentioned in PR #7004 on WordPress/wordpress-develop by @adamsilverstein.


2 weeks ago
#1

  • Keywords has-patch added

#2 @adamsilverstein
2 weeks ago

  • Milestone changed from Awaiting Review to 6.6.1
  • Version set to trunk

#3 @adamsilverstein
2 weeks ago

  • Keywords needs-testing added

#4 @adamsilverstein
2 weeks ago

  • Milestone changed from 6.6.1 to 6.7
  • Version changed from trunk to 6.5
Note: See TracTickets for help on using tickets.