[wp-trac] [WordPress Trac] #61614: Media: Ensure Imagick quality is set correctly

WordPress Trac noreply at wordpress.org
Tue Jul 9 20:16:48 UTC 2024


#61614: Media: Ensure Imagick quality is set correctly
-----------------------------+-----------------------------
 Reporter:  adamsilverstein  |      Owner:  (none)
     Type:  defect (bug)     |     Status:  new
 Priority:  normal           |  Milestone:  Awaiting Review
Component:  General          |    Version:
 Severity:  normal           |   Keywords:
  Focuses:                   |
-----------------------------+-----------------------------
 == 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 [https://make.wordpress.org/core/2024/02/23/wordpress-6-5-adds-
 avif-
 support/#:~:text=Q%3A%20How%20do%20I%20adjust%20the%20compression%20level%20used%20for%20generated%20AVIF%20images%3F
 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
 [https://kiwix.ounapuu.ee/content/stackoverflow.com_en_all_2023-11/questions/73549733
 /php-imagick-avif-image-quality-options-not-working 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.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/61614>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list