[wp-trac] [WordPress Trac] #54648: Improve filter to enable setting quality for each subsize image

WordPress Trac noreply at wordpress.org
Fri Oct 11 18:43:15 UTC 2024


#54648: Improve filter to enable setting quality for each subsize image
-------------------------------------+-------------------------------------
 Reporter:  Mte90                    |       Owner:  adamsilverstein
     Type:  enhancement              |      Status:  accepted
 Priority:  normal                   |   Milestone:  Future Release
Component:  Media                    |     Version:
 Severity:  normal                   |  Resolution:
 Keywords:  has-patch needs-unit-    |     Focuses:  performance,
  tests changes-requested            |  sustainability
-------------------------------------+-------------------------------------

Comment (by adamsilverstein):

 @joemcgill thanks for reviewing. Indeed when testing the previous PR it
 didn't work as expected.

 Instead, we need to apply the filter and the quality right before resizing
 the image, after we have calculated the final dimensions. This is in the
 resize method of both Imagick and GD.

 I have updated the [https://github.com/WordPress/wordpress-
 develop/pull/2189 PR] to make this change and verified the filter now
 passes each image size, and that the returned quality is applied when
 creating the sub sized images.

 The PR still needs unit tests - to test manually, follow these steps:

 **Test each size is passed to the filter:**
 1. Add some logging based on the filter to verify each size passed to the
 filter:

 {{{#!php
 <?php
 function log_calls_to_wp_editor_set_quality( $default_quality, $mime_type,
 $size ) {
         error_log( json_encode( $size, JSON_PRETTY_PRINT ) );
         return $default_quality;
 }
 add_filter( 'wp_editor_set_quality', 'log_calls_to_wp_editor_set_quality',
 10, 3 );
 }}}

 2. Upload an image and verify each size is logged.

 **Test quality can be applied per size**
 1. Add a filter on `wp_editor_set_quality` and return a very low quality
 for specific sizes.
 For example, this will output any image 1000 pixels or wider at a **very**
 low 5 quality.
 {{{#!php
 <?php
 function log_calls_to_wp_editor_set_quality( $default_quality, $mime_type,
 $size ) {
         if ( $size[0] >= 1000 ) {
                 return 5;
         }
         return $default_quality;
 }
 add_filter( 'wp_editor_set_quality', 'log_calls_to_wp_editor_set_quality',
 10, 3 );
 }}}
 2. Review the output images, note that all images 1000 pixels wide or
 larger are very low quality.


 Q: Right now I am applying the filter after the final image size is
 calculated, passing the calculated size which might include a crop. We
 could instead pass the "requested" size which might be better because it
 would always match the image size dimensions What do you think?

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/54648#comment:37>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list