+ if ( is_callable( array( $this->image, 'sampleImage' ) ) ) {
+ $resize_ratio = ( $dst_w / $this->size['width'] ) * ( $dst_h / $this->size['height'] );
+ $sample_factor = 5;
+
+ if ( $resize_ratio < .111 && ( $dst_w * $sample_factor > 128 && $dst_h * $sample_factor > 128 ) ) {
+ $this->image->sampleImage( $dst_w * $sample_factor, $dst_h * $sample_factor );
+ }
+ }
+
+ /*
+ * Use resizeImage() when it's available and a valid filter value is set.
+ * Otherwise, fall back to the scaleImage() method for resizing, which
+ * results in better image quality over resizeImage() with default filter
+ * settings and retains backwards compatibility with pre 4.5 functionality.
+ */
+ if ( is_callable( array( $this->image, 'resizeImage' ) ) && $filter ) {
+ $this->image->setOption( 'filter:support', '2.0' );
+ $this->image->resizeImage( $dst_w, $dst_h, $filter, 1 );
+ } else {
+ $this->image->scaleImage( $dst_w, $dst_h );
+ }
+
+ // Set appropriate quality settings after resizing.
+ if ( 'image/jpeg' == $this->mime_type ) {
+ if ( is_callable( array( $this->image, 'unsharpMaskImage' ) ) ) {
+ $this->image->unsharpMaskImage( 0.25, 0.25, 8, 0.065 );
+ }
+
+ $this->image->setOption( 'jpeg:fancy-upsampling', 'off' );
+ }
+
+ if ( 'image/png' === $this->mime_type ) {
+ $this->image->setOption( 'png:compression-filter', '5' );
+ $this->image->setOption( 'png:compression-level', '9' );
+ $this->image->setOption( 'png:compression-strategy', '1' );
+ $this->image->setOption( 'png:exclude-chunk', 'all' );
+ }
+
+ /*
+ * If alpha channel is not defined, set it opaque.
+ *
+ * Note that Imagick::getImageAlphaChannel() is only available if Imagick
+ * has been compiled against ImageMagick version 6.4.0 or newer.
+ */
+ if ( is_callable( array( $this->image, 'getImageAlphaChannel' ) )
+ && is_callable( array( $this->image, 'setImageAlphaChannel' ) )
+ && defined( Imagick::ALPHACHANNEL_UNDEFINED )
+ && defined( Imagick::ALPHACHANNEL_OPAQUE )
+ ) {
+ if ( $this->image->getImageAlphaChannel() === Imagick::ALPHACHANNEL_UNDEFINED ) {
+ $this->image->setImageAlphaChannel( Imagick::ALPHACHANNEL_OPAQUE );
+ }
+ }
+
+ // Limit the bit depth of resized images to 8 bits per channel.
+ if ( is_callable( array( $this->image, 'getImageDepth' ) ) && is_callable( array( $this->image, 'setImageDepth' ) ) ) {
+ if ( 8 < $this->image->getImageDepth() ) {
+ $this->image->setImageDepth( 8 );
+ }
+ }
+
+ if ( is_callable( array( $this->image, 'setInterlaceScheme' ) ) && defined( 'Imagick::INTERLACE_NO' ) ) {
+ $this->image->setInterlaceScheme( Imagick::INTERLACE_NO );
+ }
+