| | 256 | // Execute the resize |
| | 257 | $this->thumbnail_image( $dst_w, $dst_h ); |
| | 258 | |
| | 259 | return $this->update_size( $dst_w, $dst_h ); |
| | 260 | } |
| | 261 | |
| | 262 | /** |
| | 263 | * Efficiently resize the current image |
| | 264 | * |
| | 265 | * This is a WordPress specific implementation of Imagick::thumbnailImage(), |
| | 266 | * which resizes an image to given dimentions and removes any associated profiles. |
| | 267 | * |
| | 268 | * @since 4.5.0 |
| | 269 | * |
| | 270 | * @param int $dst_w The destination width. |
| | 271 | * @param int $dst_h The destination height. |
| | 272 | * @param string $filter Optional. The Imagick filter to use when resizing. Default 'triangle'. |
| | 273 | * @param bool $strip_meta Optional. Strip all profiles, exluding color profiles, from the image. Default true. |
| | 274 | * @return bool|WP_Error |
| | 275 | */ |
| | 276 | protected function thumbnail_image( $dst_w, $dst_h, $filter = 'triangle', $strip_meta = true ) { |
| | 277 | $allowed_filters = array( |
| | 278 | 'point' => Imagick::FILTER_POINT, |
| | 279 | 'box' => Imagick::FILTER_BOX, |
| | 280 | 'triangle' => Imagick::FILTER_TRIANGLE, |
| | 281 | 'hermite' => Imagick::FILTER_HERMITE, |
| | 282 | 'hanning' => Imagick::FILTER_HANNING, |
| | 283 | 'hamming' => Imagick::FILTER_HAMMING, |
| | 284 | 'blackman' => Imagick::FILTER_BLACKMAN, |
| | 285 | 'gaussian' => Imagick::FILTER_GAUSSIAN, |
| | 286 | 'quadratic' => Imagick::FILTER_QUADRATIC, |
| | 287 | 'cubic' => Imagick::FILTER_CUBIC, |
| | 288 | 'catrom' => Imagick::FILTER_CATROM, |
| | 289 | 'mitchell' => Imagick::FILTER_MITCHELL, |
| | 290 | 'lanczos' => Imagick::FILTER_LANCZOS, |
| | 291 | 'bessel' => Imagick::FILTER_BESSEL, |
| | 292 | 'sinc' => Imagick::FILTER_SINC |
| | 293 | ); |
| | 294 | |
| | 295 | // Set a default filter if an acceptable one wasn’t passed. |
| | 296 | $filter = isset( $filter, $allowed_filters ) ? $allowed_filters[$filter] : Imagick::FILTER_TRIANGLE; |
| | 297 | |
| 257 | | $this->image->scaleImage( $dst_w, $dst_h ); |
| | 310 | $strip_meta = apply_filters( 'image_strip_meta', $strip_meta ); |
| | 311 | |
| | 312 | // Strip image meta. |
| | 313 | if ( $strip_meta ) { |
| | 314 | $this->strip_meta(); |
| | 315 | } |
| | 316 | |
| | 317 | /* |
| | 318 | * To be more efficient, resample large images to 5x the destination size before resizing |
| | 319 | * whenever the output size is less that 1/3 of the original image size (1/3^2 ~= .111), |
| | 320 | * unless we would be resampling to a scale smaller than 128x128. |
| | 321 | */ |
| | 322 | $resize_ratio = ( $dst_w / $this->size['width'] ) * ( $dst_h / $this->size['height'] ); |
| | 323 | $sample_factor = 5; |
| | 324 | |
| | 325 | if ( $resize_ratio < .111 && ( $dst_w * $sample_factor > 128 && $dst_h * $sample_factor > 128 ) ) { |
| | 326 | $this->image->sampleImage( $dst_w * $sample_factor, $dst_h * $sample_factor ); |
| | 327 | } |
| | 328 | |
| | 329 | // Resize to the final output size. |
| | 330 | $this->image->setOption( 'filter:support', '2.0' ); |
| | 331 | $this->image->resizeImage( $dst_w, $dst_h, $filter, 1 ); |
| | 332 | |
| | 333 | // Set appropriate quality settings after resizing. |
| | 334 | if ( 'image/jpeg' == $this->mime_type ) { |
| | 335 | $this->image->unsharpMaskImage( 0.25, 0.25, 8, 0.065 ); |
| | 336 | $this->image->setOption( 'jpeg:fancy-upsampling', 'off' ); |
| | 337 | } |
| | 338 | |
| | 339 | if ( 'image/png' === $this->mime_type ) { |
| | 340 | $this->image->setOption( 'png:compression-filter', '5' ); |
| | 341 | $this->image->setOption( 'png:compression-level', '9' ); |
| | 342 | $this->image->setOption( 'png:compression-strategy', '1' ); |
| | 343 | $this->image->setOption( 'png:exclude-chunk', 'all' ); |
| | 344 | } |
| | 345 | |
| | 346 | /** |
| | 347 | * Imagick::getImageAlphaChannel() is only available if Imagick has been |
| | 348 | * compiled against ImageMagick version 6.4.0 or newer. |
| | 349 | */ |
| | 350 | if ( method_exists( $this->image, 'getImageAlphaChannel') && $this->image->getImageAlphaChannel() === Imagick::ALPHACHANNEL_UNDEFINED ) { |
| | 351 | $this->image->setImageAlphaChannel( Imagick::ALPHACHANNEL_OPAQUE ); |
| | 352 | } |
| | 353 | |
| | 354 | // Limit the bit depth of resized images to 8 bits per channel. |
| | 355 | if ( 8 < $this->image->getImageDepth() ) { |
| | 356 | $this->image->setImageDepth( 8 ); |
| | 357 | } |
| | 358 | |
| | 359 | $this->image->setInterlaceScheme( Imagick::INTERLACE_NO ); |
| | 360 | |
| | 634 | |
| | 635 | /** |
| | 636 | * Strip all image meta except color profiles from an image. |
| | 637 | * |
| | 638 | * @access public |
| | 639 | * @since 4.5.0 |
| | 640 | */ |
| | 641 | protected function strip_meta() { |
| | 642 | try { |
| | 643 | // Strip profiles. |
| | 644 | foreach ( $this->image->getImageProfiles( '*', true ) as $key => $value ) { |
| | 645 | if ( $key != 'icc' && $key != 'icm' ) { |
| | 646 | $this->image->removeImageProfile( $key ); |
| | 647 | } |
| | 648 | } |
| | 649 | |
| | 650 | // Strip image properties. |
| | 651 | if ( method_exists( $this->image, 'deleteImageProperty' ) ) { |
| | 652 | $this->image->deleteImageProperty( 'comment' ); |
| | 653 | $this->image->deleteImageProperty( 'Thumb::URI' ); |
| | 654 | $this->image->deleteImageProperty( 'Thumb::MTime' ); |
| | 655 | $this->image->deleteImageProperty( 'Thumb::Size' ); |
| | 656 | $this->image->deleteImageProperty( 'Thumb::Mimetype' ); |
| | 657 | $this->image->deleteImageProperty( 'software' ); |
| | 658 | $this->image->deleteImageProperty( 'Thumb::Image::Width' ); |
| | 659 | $this->image->deleteImageProperty( 'Thumb::Image::Height' ); |
| | 660 | $this->image->deleteImageProperty( 'Thumb::Document::Pages' ); |
| | 661 | } else { |
| | 662 | $this->image->setImageProperty( 'comment', '' ); |
| | 663 | $this->image->setImageProperty( 'Thumb::URI', '' ); |
| | 664 | $this->image->setImageProperty( 'Thumb::MTime', '' ); |
| | 665 | $this->image->setImageProperty( 'Thumb::Size', '' ); |
| | 666 | $this->image->setImageProperty( 'Thumb::Mimetype', '' ); |
| | 667 | $this->image->setImageProperty( 'software', '' ); |
| | 668 | $this->image->setImageProperty( 'Thumb::Image::Width', '' ); |
| | 669 | $this->image->setImageProperty( 'Thumb::Image::Height', '' ); |
| | 670 | $this->image->setImageProperty( 'Thumb::Document::Pages', '' ); |
| | 671 | } |
| | 672 | } catch ( Excpetion $e ) { |
| | 673 | return new WP_Error( 'image_strip_meta_error', $e->getMessage() ); |
| | 674 | } |
| | 675 | |
| | 676 | return true; |
| | 677 | } |
| | 678 | |