[wp-trac] [WordPress Trac] #40415: Imagick resize filter hook

WordPress Trac noreply at wordpress.org
Fri Jul 24 01:05:17 UTC 2020


#40415: Imagick resize filter hook
-------------------------------------------------+-------------------------
 Reporter:  virgodesign                          |       Owner:  (none)
     Type:  enhancement                          |      Status:  new
 Priority:  normal                               |   Milestone:  Future
                                                 |  Release
Component:  Media                                |     Version:  4.7.3
 Severity:  normal                               |  Resolution:
 Keywords:  has-patch needs-testing reporter-    |     Focuses:
  feedback                                       |
-------------------------------------------------+-------------------------

Comment (by pjmatos):

 As a recent WordPress user, I was also very surprised to see that the
 generated images are so blurry. I searched for some plugins, but as I
 started to understand the code, I realized that these had to make an
 unsharpMask over WP's own unsharpMask, in addition to having to work on a
 resized image with the much worse TRIANGLE filter.

 So, besides the **imagick_resize_filter** patch some people mentioned here
 (thanks for sharing it), I also added a new filter named
 **imagick_unsharp_settings** in ''class-wp-image-editor-imagick.php''
 (near the unsharpMaskImage function):
 {{{#!php
 <?php
             $unsharp_settings = [];
             $unsharp_settings[9999] = ['radius' => 0.25, 'sigma' => 0.25,
 'amount' => 8, 'threshold' => 0.065];
             $unsharp_settings = apply_filters( 'imagick_unsharp_settings',
 $unsharp_settings );

             // Set appropriate quality settings after resizing.
             if ( 'image/jpeg' === $this->mime_type ) {
                 if ( is_callable( array( $this->image, 'unsharpMaskImage'
 ) ) ) {
                     foreach( $unsharp_settings as $width => $unsharp_set )
 {
                         if ( $dst_w < $width ) break;
                     }
                     $this->image->unsharpMaskImage(
 $unsharp_set['radius'], $unsharp_set['sigma'], $unsharp_set['amount'],
 $unsharp_set['threshold']  );
                 }

                 $this->image->setOption( 'jpeg:fancy-upsampling', 'off' );
             }
 }}}

 This allows to set several unsharp settings in ''functions.php'':
 {{{#!php
 <?php
 add_filter ('imagick_unsharp_settings', function() {
     $sets = [];
     $sets[200] = ['radius' => 0, 'sigma' => 0.5, 'amount' => 1,
 'threshold' => 0];
     $sets[500] = ['radius' => 0, 'sigma' => 0.75, 'amount' => 0.75,
 'threshold' => 0.008];
     $sets[9999] = ['radius' => 1.5, 'sigma' => 1, 'amount' => 0.7,
 'threshold' => 0.02];
     return $sets;
 } );
 }}}
 Meaning that for images below the width indicated in the key of the array
 (200, 500 and 9999) the corresponding unsharp settings will be applied. It
 seems that different settings might be a good approach for images of
 different sizes (http://www.imagemagick.org/Usage/resize/#resize_unsharp)

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


More information about the wp-trac mailing list