| | 257 | // Execute the resize |
| | 258 | $thumb_result = $this->thumbnail_image( $dst_w, $dst_h ); |
| | 259 | if ( is_wp_error( $thumb_result ) ) { |
| | 260 | return $thumb_result; |
| | 261 | } |
| | 262 | |
| | 263 | return $this->update_size( $dst_w, $dst_h ); |
| | 264 | } |
| | 265 | |
| | 266 | /** |
| | 267 | * Efficiently resize the current image |
| | 268 | * |
| | 269 | * This is a WordPress specific implementation of Imagick::thumbnailImage(), |
| | 270 | * which resizes an image to given dimentions and removes any associated profiles. |
| | 271 | * |
| | 272 | * @since 4.5.0 |
| | 273 | * |
| | 274 | * @param int $dst_w The destination width. |
| | 275 | * @param int $dst_h The destination height. |
| | 276 | * @param string $filter Optional. The Imagick filter to use when resizing. Default 'triangle'. |
| | 277 | * @param bool $strip_meta Optional. Strip all profiles, excluding color profiles, from the image. Default true. |
| | 278 | * @return bool|WP_Error |
| | 279 | */ |
| | 280 | protected function thumbnail_image( $dst_w, $dst_h, $filter = 'triangle', $strip_meta = true ) { |
| | 281 | $allowed_filters = array( |
| | 282 | 'point' => Imagick::FILTER_POINT, |
| | 283 | 'box' => Imagick::FILTER_BOX, |
| | 284 | 'triangle' => Imagick::FILTER_TRIANGLE, |
| | 285 | 'hermite' => Imagick::FILTER_HERMITE, |
| | 286 | 'hanning' => Imagick::FILTER_HANNING, |
| | 287 | 'hamming' => Imagick::FILTER_HAMMING, |
| | 288 | 'blackman' => Imagick::FILTER_BLACKMAN, |
| | 289 | 'gaussian' => Imagick::FILTER_GAUSSIAN, |
| | 290 | 'quadratic' => Imagick::FILTER_QUADRATIC, |
| | 291 | 'cubic' => Imagick::FILTER_CUBIC, |
| | 292 | 'catrom' => Imagick::FILTER_CATROM, |
| | 293 | 'mitchell' => Imagick::FILTER_MITCHELL, |
| | 294 | 'lanczos' => Imagick::FILTER_LANCZOS, |
| | 295 | 'bessel' => Imagick::FILTER_BESSEL, |
| | 296 | 'sinc' => Imagick::FILTER_SINC |
| | 297 | ); |
| | 298 | |
| | 299 | // Set a default filter if an acceptable one wasn’t passed. |
| | 300 | $filter = isset( $filter, $allowed_filters[$filter] ) ? $allowed_filters[$filter] : Imagick::FILTER_TRIANGLE; |
| | 301 | |
| | 302 | /** |
| | 303 | * Filter to override stripping metadata from images when they're resized. |
| | 304 | * |
| | 305 | * This filter only applies when resizing using the Imagick editor since GD |
| | 306 | * always strips profiles by default. |
| | 307 | * |
| | 308 | * @since 4.5.0 |
| | 309 | * |
| | 310 | * @param bool $strip_meta Whether to strip image metadata during resizing. Default true. |
| | 311 | */ |
| | 312 | $strip_meta = apply_filters( 'image_strip_meta', $strip_meta ); |
| | 313 | |
| | 314 | // Strip image meta. |
| | 315 | if ( $strip_meta ) { |
| | 316 | $strip_result = $this->strip_meta(); |
| | 317 | |
| | 318 | if ( is_wp_error( $strip_result ) ) { |
| | 319 | return $strip_result; |
| | 320 | } |
| | 321 | } |
| | 322 | |
| | 324 | /* |
| | 325 | * To be more efficient, resample large images to 5x the destination size before resizing |
| | 326 | * whenever the output size is less that 1/3 of the original image size (1/3^2 ~= .111), |
| | 327 | * unless we would be resampling to a scale smaller than 128x128. |
| | 328 | */ |
| | 329 | $resize_ratio = ( $dst_w / $this->size['width'] ) * ( $dst_h / $this->size['height'] ); |
| | 330 | $sample_factor = 5; |
| | 331 | |
| | 332 | if ( $resize_ratio < .111 && ( $dst_w * $sample_factor > 128 && $dst_h * $sample_factor > 128 ) ) { |
| | 333 | $this->image->sampleImage( $dst_w * $sample_factor, $dst_h * $sample_factor ); |
| | 334 | } |
| | 335 | |
| | 336 | // Resize to the final output size. |
| | 337 | $this->image->setOption( 'filter:support', '2.0' ); |
| | 338 | $this->image->resizeImage( $dst_w, $dst_h, $filter, 1 ); |
| | 339 | |
| | 340 | // Set appropriate quality settings after resizing. |
| | 341 | if ( 'image/jpeg' == $this->mime_type ) { |
| | 342 | $this->image->unsharpMaskImage( 0.25, 0.25, 8, 0.065 ); |
| | 343 | $this->image->setOption( 'jpeg:fancy-upsampling', 'off' ); |
| | 344 | } |
| | 345 | |
| | 346 | if ( 'image/png' === $this->mime_type ) { |
| | 347 | $this->image->setOption( 'png:compression-filter', '5' ); |
| | 348 | $this->image->setOption( 'png:compression-level', '9' ); |
| | 349 | $this->image->setOption( 'png:compression-strategy', '1' ); |
| | 350 | $this->image->setOption( 'png:exclude-chunk', 'all' ); |
| | 351 | } |
| | 352 | |
| | 647 | |
| | 648 | /** |
| | 649 | * Strip all image meta except color profiles from an image. |
| | 650 | * |
| | 651 | * @access public |
| | 652 | * @since 4.5.0 |
| | 653 | */ |
| | 654 | protected function strip_meta() { |
| | 655 | try { |
| | 656 | // Strip profiles. |
| | 657 | foreach ( $this->image->getImageProfiles( '*', true ) as $key => $value ) { |
| | 658 | if ( $key != 'icc' && $key != 'icm' ) { |
| | 659 | $this->image->removeImageProfile( $key ); |
| | 660 | } |
| | 661 | } |
| | 662 | |
| | 663 | // Strip image properties. |
| | 664 | if ( method_exists( $this->image, 'deleteImageProperty' ) ) { |
| | 665 | $this->image->deleteImageProperty( 'comment' ); |
| | 666 | $this->image->deleteImageProperty( 'Thumb::URI' ); |
| | 667 | $this->image->deleteImageProperty( 'Thumb::MTime' ); |
| | 668 | $this->image->deleteImageProperty( 'Thumb::Size' ); |
| | 669 | $this->image->deleteImageProperty( 'Thumb::Mimetype' ); |
| | 670 | $this->image->deleteImageProperty( 'software' ); |
| | 671 | $this->image->deleteImageProperty( 'Thumb::Image::Width' ); |
| | 672 | $this->image->deleteImageProperty( 'Thumb::Image::Height' ); |
| | 673 | $this->image->deleteImageProperty( 'Thumb::Document::Pages' ); |
| | 674 | } else { |
| | 675 | $this->image->setImageProperty( 'comment', '' ); |
| | 676 | $this->image->setImageProperty( 'Thumb::URI', '' ); |
| | 677 | $this->image->setImageProperty( 'Thumb::MTime', '' ); |
| | 678 | $this->image->setImageProperty( 'Thumb::Size', '' ); |
| | 679 | $this->image->setImageProperty( 'Thumb::Mimetype', '' ); |
| | 680 | $this->image->setImageProperty( 'software', '' ); |
| | 681 | $this->image->setImageProperty( 'Thumb::Image::Width', '' ); |
| | 682 | $this->image->setImageProperty( 'Thumb::Image::Height', '' ); |
| | 683 | $this->image->setImageProperty( 'Thumb::Document::Pages', '' ); |
| | 684 | } |
| | 685 | } catch ( Excpetion $e ) { |
| | 686 | return new WP_Error( 'image_strip_meta_error', $e->getMessage() ); |
| | 687 | } |
| | 688 | |
| | 689 | return true; |
| | 690 | } |
| | 691 | |