| 252 | // Execute the resize |
| 253 | $this->_resize( $dst_w, $dst_h ); |
| 254 | |
| 255 | return $this->update_size( $dst_w, $dst_h ); |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Private function to efficiently resize an image. |
| 260 | * |
| 261 | * @since 4.5.0 |
| 262 | * |
| 263 | * @param int $dst_w The destination width. |
| 264 | * @param int $dst_h The destination height. |
| 265 | * @param string $filter Optional. The Imagick filter to use when resizing. Default 'triangle'. |
| 266 | * @param bool $strip_meta Optional. Strip all profiles, exluding color profiles, from the image. Default true. |
| 267 | * @return bool|WP_Error |
| 268 | */ |
| 269 | protected function _resize( $dst_w, $dst_h, $filter = 'triangle', $strip_meta = true ) { |
| 270 | $allowed_filters = array( |
| 271 | 'point' => Imagick::FILTER_POINT, |
| 272 | 'box' => Imagick::FILTER_BOX, |
| 273 | 'triangle' => Imagick::FILTER_TRIANGLE, |
| 274 | 'hermite' => Imagick::FILTER_HERMITE, |
| 275 | 'hanning' => Imagick::FILTER_HANNING, |
| 276 | 'hamming' => Imagick::FILTER_HAMMING, |
| 277 | 'blackman' => Imagick::FILTER_BLACKMAN, |
| 278 | 'gaussian' => Imagick::FILTER_GAUSSIAN, |
| 279 | 'quadratic' => Imagick::FILTER_QUADRATIC, |
| 280 | 'cubic' => Imagick::FILTER_CUBIC, |
| 281 | 'catrom' => Imagick::FILTER_CATROM, |
| 282 | 'mitchell' => Imagick::FILTER_MITCHELL, |
| 283 | 'lanczos' => Imagick::FILTER_LANCZOS, |
| 284 | 'bessel' => Imagick::FILTER_BESSEL, |
| 285 | 'sinc' => Imagick::FILTER_SINC |
| 286 | ); |
| 287 | |
| 288 | // Set a default filter if an acceptable one wasn’t passed. |
| 289 | $filter = isset( $filter, $allowed_filters ) ? $allowed_filters[$filter] : Imagick::FILTER_TRIANGLE; |
| 290 | |
257 | | $this->image->scaleImage( $dst_w, $dst_h ); |
| 301 | $resize_ratio = ( $dst_w / $this->size['width'] ) * ( $dst_h / $this->size['height'] ); |
| 302 | $sample_factor = 5; |
| 303 | |
| 304 | if ( $resize_ratio < .111 && ( $dst_w * $sample_factor > 128 && $dst_h * $sample_factor > 128 ) ) { |
| 305 | $this->image->sampleImage( $dst_w * $sample_factor, $dst_h * $sample_factor ); |
| 306 | } |
| 307 | |
| 308 | // Resize to the final output size. |
| 309 | $this->image->setOption( 'filter:support', '2.0' ); |
| 310 | $this->image->resizeImage( $dst_w, $dst_h, $filter, 1 ); |
| 311 | |
| 312 | // Set appropriate quality settings after resizing. |
| 313 | if ( 'image/jpeg' == $this->mime_type ) { |
| 314 | $this->image->unsharpMaskImage( 0.25, 0.25, 8, 0.065 ); |
| 315 | $this->image->setOption( 'jpeg:fancy-upsampling', 'off' ); |
| 316 | } |
| 317 | |
| 318 | if ( 'image/png' === $this->mime_type ) { |
| 319 | $this->image->setOption( 'png:compression-filter', '5' ); |
| 320 | $this->image->setOption( 'png:compression-level', '9' ); |
| 321 | $this->image->setOption( 'png:compression-strategy', '1' ); |
| 322 | $this->image->setOption( 'png:exclude-chunk', 'all' ); |
| 323 | } |
| 324 | |
| 325 | // If the alpha channel is not defined, make it opaque. |
| 326 | if ( $this->image->getImageAlphaChannel() === Imagick::ALPHACHANNEL_UNDEFINED ) { |
| 327 | $this->image->setImageAlphaChannel( Imagick::ALPHACHANNEL_OPAQUE ); |
| 328 | } |
| 329 | |
| 330 | $this->image->setImageDepth( 8 ); |
| 331 | $this->image->setInterlaceScheme( Imagick::INTERLACE_NO ); |
| 332 | $this->image->setColorspace( Imagick::COLORSPACE_SRGB ); |
| 606 | |
| 607 | /** |
| 608 | * Strip all image meta except color profiles from an image. |
| 609 | * |
| 610 | * @access public |
| 611 | * @since 4.5.0 |
| 612 | */ |
| 613 | public function strip_meta() { |
| 614 | try { |
| 615 | // Strip profiles. |
| 616 | foreach ( $this->image->getImageProfiles( '*', true ) as $key => $value ) { |
| 617 | if ( $key != 'icc' && $key != 'icm' ) { |
| 618 | $this->image->removeImageProfile( $key ); |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | // Strip image properties. |
| 623 | if ( method_exists( $this->image, 'deleteImageProperty' ) ) { |
| 624 | $this->image->deleteImageProperty( 'comment' ); |
| 625 | $this->image->deleteImageProperty( 'Thumb::URI' ); |
| 626 | $this->image->deleteImageProperty( 'Thumb::MTime' ); |
| 627 | $this->image->deleteImageProperty( 'Thumb::Size' ); |
| 628 | $this->image->deleteImageProperty( 'Thumb::Mimetype' ); |
| 629 | $this->image->deleteImageProperty( 'software' ); |
| 630 | $this->image->deleteImageProperty( 'Thumb::Image::Width' ); |
| 631 | $this->image->deleteImageProperty( 'Thumb::Image::Height' ); |
| 632 | $this->image->deleteImageProperty( 'Thumb::Document::Pages' ); |
| 633 | } else { |
| 634 | $this->image->setImageProperty( 'comment', '' ); |
| 635 | $this->image->setImageProperty( 'Thumb::URI', '' ); |
| 636 | $this->image->setImageProperty( 'Thumb::MTime', '' ); |
| 637 | $this->image->setImageProperty( 'Thumb::Size', '' ); |
| 638 | $this->image->setImageProperty( 'Thumb::Mimetype', '' ); |
| 639 | $this->image->setImageProperty( 'software', '' ); |
| 640 | $this->image->setImageProperty( 'Thumb::Image::Width', '' ); |
| 641 | $this->image->setImageProperty( 'Thumb::Image::Height', '' ); |
| 642 | $this->image->setImageProperty( 'Thumb::Document::Pages', '' ); |
| 643 | } |
| 644 | } catch ( Excpetion $e ) { |
| 645 | return new WP_Error( 'image_strip_meta_error', $e->getMessage() ); |
| 646 | } |
| 647 | |
| 648 | return true; |
| 649 | } |
| 650 | |