Ticket #33642: 33642.11.patch
File 33642.11.patch, 8.2 KB (added by , 9 years ago) |
---|
-
src/wp-includes/class-wp-image-editor-imagick.php
diff --git src/wp-includes/class-wp-image-editor-imagick.php src/wp-includes/class-wp-image-editor-imagick.php index 125ef0d..a589277 100644
class WP_Image_Editor_Imagick extends WP_Image_Editor { 62 62 'writeimage', 63 63 'getimageblob', 64 64 'getimagegeometry', 65 'getimagedepth',66 65 'getimageformat', 67 66 'setimageformat', 68 67 'setimagecompression', 69 68 'setimagecompressionquality', 70 'setimagedepth',71 69 'setimagepage', 72 'setimageproperty', 73 'setinterlacescheme', 70 'setoption', 74 71 'scaleimage', 75 72 'cropimage', 76 73 'rotateimage', 77 74 'flipimage', 78 75 'flopimage', 79 'unsharpmaskimage',80 76 ); 81 77 82 78 // Now, test for deep requirements within Imagick. 83 if ( ! ( defined( 'imagick::COMPRESSION_JPEG' ) && defined( 'imagick::FILTER_TRIANGLE' )) )79 if ( ! defined( 'imagick::COMPRESSION_JPEG' ) ) 84 80 return false; 85 81 86 82 if ( array_diff( $required_methods, get_class_methods( 'Imagick' ) ) ) … … class WP_Image_Editor_Imagick extends WP_Image_Editor { 304 300 if ( in_array( $filter_name, $allowed_filters ) && defined( 'Imagick::' . $filter_name ) ) { 305 301 $filter = constant( 'Imagick::' . $filter_name ); 306 302 } else { 307 $filter = Imagick::FILTER_TRIANGLE;303 $filter = defined( 'Imagick::FILTER_TRIANGLE' ) ? Imagick::FILTER_TRIANGLE : false; 308 304 } 309 305 310 306 /** … … class WP_Image_Editor_Imagick extends WP_Image_Editor { 317 313 * 318 314 * @param bool $strip_meta Whether to strip image metadata during resizing. Default true. 319 315 */ 320 $strip_meta = apply_filters( 'image_strip_meta', $strip_meta ); 321 322 // Strip image meta. 323 if ( $strip_meta ) { 324 $strip_result = $this->strip_meta(); 325 326 if ( is_wp_error( $strip_result ) ) { 327 return $strip_result; 328 } 316 if ( apply_filters( 'image_strip_meta', $strip_meta ) ) { 317 $this->strip_meta(); // Fail silently if not supported. 329 318 } 330 319 331 320 try { … … class WP_Image_Editor_Imagick extends WP_Image_Editor { 334 323 * whenever the output size is less that 1/3 of the original image size (1/3^2 ~= .111), 335 324 * unless we would be resampling to a scale smaller than 128x128. 336 325 */ 337 $resize_ratio = ( $dst_w / $this->size['width'] ) * ( $dst_h / $this->size['height'] ); 338 $sample_factor = 5; 326 if ( is_callable( array( $this->image, 'sampleImage' ) ) ) { 327 $resize_ratio = ( $dst_w / $this->size['width'] ) * ( $dst_h / $this->size['height'] ); 328 $sample_factor = 5; 339 329 340 if ( $resize_ratio < .111 && ( $dst_w * $sample_factor > 128 && $dst_h * $sample_factor > 128 ) ) { 341 $this->image->sampleImage( $dst_w * $sample_factor, $dst_h * $sample_factor ); 330 if ( $resize_ratio < .111 && ( $dst_w * $sample_factor > 128 && $dst_h * $sample_factor > 128 ) ) { 331 $this->image->sampleImage( $dst_w * $sample_factor, $dst_h * $sample_factor ); 332 } 342 333 } 343 334 344 // Resize to the final output size. 345 $this->image->setOption( 'filter:support', '2.0' ); 346 $this->image->resizeImage( $dst_w, $dst_h, $filter, 1 ); 335 /* 336 * Use resizeImage() when it's available and a valid filter value is set. 337 * Otherwise, fall back to the scaleImage() method for resizing, which 338 * results in better image quality over resizeImage() with default filter 339 * settings and retains backwards compatibility with pre 4.5 functionality. 340 */ 341 if ( is_callable( array( $this->image, 'resizeImage' ) ) && $filter ) { 342 $this->image->setOption( 'filter:support', '2.0' ); 343 $this->image->resizeImage( $dst_w, $dst_h, $filter, 1 ); 344 } else { 345 $this->image->scaleImage( $dst_w, $dst_h ); 346 } 347 347 348 348 // Set appropriate quality settings after resizing. 349 349 if ( 'image/jpeg' == $this->mime_type ) { 350 $this->image->unsharpMaskImage( 0.25, 0.25, 8, 0.065 ); 350 if ( is_callable( array( $this->image, 'unsharpMaskImage' ) ) ) { 351 $this->image->unsharpMaskImage( 0.25, 0.25, 8, 0.065 ); 352 } 353 351 354 $this->image->setOption( 'jpeg:fancy-upsampling', 'off' ); 352 355 } 353 356 … … class WP_Image_Editor_Imagick extends WP_Image_Editor { 358 361 $this->image->setOption( 'png:exclude-chunk', 'all' ); 359 362 } 360 363 361 /* *364 /* 362 365 * If alpha channel is not defined, set it opaque. 363 366 * 364 367 * Note that Imagick::getImageAlphaChannel() is only available if Imagick 365 368 * has been compiled against ImageMagick version 6.4.0 or newer. 366 369 */ 367 if ( method_exists( $this->image, 'getImageAlphaChannel') && $this->image->getImageAlphaChannel() === Imagick::ALPHACHANNEL_UNDEFINED ) { 368 $this->image->setImageAlphaChannel( Imagick::ALPHACHANNEL_OPAQUE ); 370 if ( is_callable( array( $this->image, 'getImageAlphaChannel' ) ) 371 && is_callable( array( $this->image, 'setImageAlphaChannel' ) ) 372 && defined( Imagick::ALPHACHANNEL_UNDEFINED ) 373 && defined( Imagick::ALPHACHANNEL_OPAQUE ) 374 ) { 375 if ( $this->image->getImageAlphaChannel() === Imagick::ALPHACHANNEL_UNDEFINED ) { 376 $this->image->setImageAlphaChannel( Imagick::ALPHACHANNEL_OPAQUE ); 377 } 369 378 } 370 379 371 // Limit the bit depth of resized images to 8 bits per channel. 372 if ( 8 < $this->image->getImageDepth() ) { 373 $this->image->setImageDepth( 8 ); 380 // Limit the bit depth of resized images to 8 bits per channel. 381 if ( is_callable( array( $this->image, 'getImageDepth' ) ) && is_callable( array( $this->image, 'setImageDepth' ) ) ) { 382 if ( 8 < $this->image->getImageDepth() ) { 383 $this->image->setImageDepth( 8 ); 384 } 374 385 } 375 386 376 $this->image->setInterlaceScheme( Imagick::INTERLACE_NO ); 387 if ( is_callable( array( $this->image, 'setInterlaceScheme' ) ) && defined( 'Imagick::INTERLACE_NO' ) ) { 388 $this->image->setInterlaceScheme( Imagick::INTERLACE_NO ); 389 } 377 390 378 391 } 379 392 catch ( Exception $e ) { … … class WP_Image_Editor_Imagick extends WP_Image_Editor { 662 675 * @return true|WP_Error True if stripping metadata was successful. WP_Error object on error. 663 676 */ 664 677 protected function strip_meta() { 678 679 if ( ! is_callable( array( $this->image, 'getImageProfiles' ) ) ) { 680 return new WP_Error( 'image_strip_meta_error', __('Imagick::getImageProfiles() is required to strip image meta.') ); 681 } 682 683 if ( ! is_callable( array( $this->image, 'removeImageProfile' ) ) ) { 684 return new WP_Error( 'image_strip_meta_error', __('Imagick::removeImageProfile() is required to strip image meta.') ); 685 } 686 687 /* 688 * Protect a few profiles from being stripped for the following reasons: 689 * 690 * - icc: Color profile information 691 * - icm: Color profile information 692 * - iptc: Copyright data 693 * - exif: Orientation data 694 * - xmp: Rights usage data 695 */ 696 $protected_profiles = array( 697 'icc', 698 'icm', 699 'iptc', 700 'exif', 701 'xmp' 702 ); 703 665 704 try { 666 705 // Strip profiles. 667 706 foreach ( $this->image->getImageProfiles( '*', true ) as $key => $value ) { 668 if ( $key != 'icc' && $key != 'icm') {707 if ( ! in_array( $key, $protected_profiles ) ) { 669 708 $this->image->removeImageProfile( $key ); 670 709 } 671 710 } 672 711 673 // Strip image properties.674 if ( method_exists( $this->image, 'deleteImageProperty' ) ) {675 $this->image->deleteImageProperty( 'comment' );676 $this->image->deleteImageProperty( 'Thumb::URI' );677 $this->image->deleteImageProperty( 'Thumb::MTime' );678 $this->image->deleteImageProperty( 'Thumb::Size' );679 $this->image->deleteImageProperty( 'Thumb::Mimetype' );680 $this->image->deleteImageProperty( 'software' );681 $this->image->deleteImageProperty( 'Thumb::Image::Width' );682 $this->image->deleteImageProperty( 'Thumb::Image::Height' );683 $this->image->deleteImageProperty( 'Thumb::Document::Pages' );684 } else {685 $this->image->setImageProperty( 'comment', '' );686 $this->image->setImageProperty( 'Thumb::URI', '' );687 $this->image->setImageProperty( 'Thumb::MTime', '' );688 $this->image->setImageProperty( 'Thumb::Size', '' );689 $this->image->setImageProperty( 'Thumb::Mimetype', '' );690 $this->image->setImageProperty( 'software', '' );691 $this->image->setImageProperty( 'Thumb::Image::Width', '' );692 $this->image->setImageProperty( 'Thumb::Image::Height', '' );693 $this->image->setImageProperty( 'Thumb::Document::Pages', '' );694 }695 712 } catch ( Exception $e ) { 696 713 return new WP_Error( 'image_strip_meta_error', $e->getMessage() ); 697 714 }