+ /*
+ * To be more efficient, resample large images to 5x the destination size before resizing
+ * whenever the output size is less that 1/3 of the original image size (1/3^2 ~= .111),
+ * unless we would be resampling to a scale smaller than 128x128.
+ */
+ 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.