Changeset 42343 for trunk/src/wp-includes/class-wp-image-editor-imagick.php
- Timestamp:
- 11/30/2017 11:09:33 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-image-editor-imagick.php
r41288 r42343 46 46 47 47 // First, test Imagick's extension and classes. 48 if ( ! extension_loaded( 'imagick' ) || ! class_exists( 'Imagick', false ) || ! class_exists( 'ImagickPixel', false ) ) 48 if ( ! extension_loaded( 'imagick' ) || ! class_exists( 'Imagick', false ) || ! class_exists( 'ImagickPixel', false ) ) { 49 49 return false; 50 51 if ( version_compare( phpversion( 'imagick' ), '2.2.0', '<' ) ) 50 } 51 52 if ( version_compare( phpversion( 'imagick' ), '2.2.0', '<' ) ) { 52 53 return false; 54 } 53 55 54 56 $required_methods = array( … … 75 77 76 78 // Now, test for deep requirements within Imagick. 77 if ( ! defined( 'imagick::COMPRESSION_JPEG' ) ) 79 if ( ! defined( 'imagick::COMPRESSION_JPEG' ) ) { 78 80 return false; 81 } 79 82 80 83 $class_methods = array_map( 'strtolower', get_class_methods( 'Imagick' ) ); … … 104 107 $imagick_extension = strtoupper( self::get_extension( $mime_type ) ); 105 108 106 if ( ! $imagick_extension ) 109 if ( ! $imagick_extension ) { 107 110 return false; 111 } 108 112 109 113 // setIteratorIndex is optional unless mime is an animated format. 110 114 // Here, we just say no if you are missing it and aren't loading a jpeg. 111 if ( ! method_exists( 'Imagick', 'setIteratorIndex' ) && $mime_type != 'image/jpeg' ) 115 if ( ! method_exists( 'Imagick', 'setIteratorIndex' ) && $mime_type != 'image/jpeg' ) { 112 116 return false; 117 } 113 118 114 119 try { 115 120 return ( (bool) @Imagick::queryFormats( $imagick_extension ) ); 116 } 117 catch ( Exception $e ) { 121 } catch ( Exception $e ) { 118 122 return false; 119 123 } … … 128 132 */ 129 133 public function load() { 130 if ( $this->image instanceof Imagick ) 134 if ( $this->image instanceof Imagick ) { 131 135 return true; 132 133 if ( ! is_file( $this->file ) && ! preg_match( '|^https?://|', $this->file ) ) 134 return new WP_Error( 'error_loading_image', __('File doesn’t exist?'), $this->file ); 136 } 137 138 if ( ! is_file( $this->file ) && ! preg_match( '|^https?://|', $this->file ) ) { 139 return new WP_Error( 'error_loading_image', __( 'File doesn’t exist?' ), $this->file ); 140 } 135 141 136 142 /* … … 141 147 142 148 try { 143 $this->image = new Imagick();149 $this->image = new Imagick(); 144 150 $file_extension = strtolower( pathinfo( $this->file, PATHINFO_EXTENSION ) ); 145 $filename = $this->file;151 $filename = $this->file; 146 152 147 153 if ( 'pdf' == $file_extension ) { … … 153 159 $this->image->readImage( $filename ); 154 160 155 if ( ! $this->image->valid() ) 156 return new WP_Error( 'invalid_image', __('File is not an image.'), $this->file); 161 if ( ! $this->image->valid() ) { 162 return new WP_Error( 'invalid_image', __( 'File is not an image.' ), $this->file ); 163 } 157 164 158 165 // Select the first frame to handle animated images properly 159 if ( is_callable( array( $this->image, 'setIteratorIndex' ) ) ) 160 $this->image->setIteratorIndex(0); 166 if ( is_callable( array( $this->image, 'setIteratorIndex' ) ) ) { 167 $this->image->setIteratorIndex( 0 ); 168 } 161 169 162 170 $this->mime_type = $this->get_mime_type( $this->image->getImageFormat() ); 163 } 164 catch ( Exception $e ) { 171 } catch ( Exception $e ) { 165 172 return new WP_Error( 'invalid_image', $e->getMessage(), $this->file ); 166 173 } … … 194 201 $this->image->setImageCompressionQuality( $quality ); 195 202 $this->image->setImageCompression( imagick::COMPRESSION_JPEG ); 196 } 197 else { 203 } else { 198 204 $this->image->setImageCompressionQuality( $quality ); 199 205 } 200 } 201 catch ( Exception $e ) { 206 } catch ( Exception $e ) { 202 207 return new WP_Error( 'image_quality_error', $e->getMessage() ); 203 208 } … … 218 223 protected function update_size( $width = null, $height = null ) { 219 224 $size = null; 220 if ( ! $width || !$height ) {225 if ( ! $width || ! $height ) { 221 226 try { 222 227 $size = $this->image->getImageGeometry(); 223 } 224 catch ( Exception $e ) { 228 } catch ( Exception $e ) { 225 229 return new WP_Error( 'invalid_image', __( 'Could not read image size.' ), $this->file ); 226 230 } 227 231 } 228 232 229 if ( ! $width ) 233 if ( ! $width ) { 230 234 $width = $size['width']; 231 232 if ( ! $height ) 235 } 236 237 if ( ! $height ) { 233 238 $height = $size['height']; 239 } 234 240 235 241 return parent::update_size( $width, $height ); … … 251 257 */ 252 258 public function resize( $max_w, $max_h, $crop = false ) { 253 if ( ( $this->size['width'] == $max_w ) && ( $this->size['height'] == $max_h ) ) 259 if ( ( $this->size['width'] == $max_w ) && ( $this->size['height'] == $max_h ) ) { 254 260 return true; 261 } 255 262 256 263 $dims = image_resize_dimensions( $this->size['width'], $this->size['height'], $max_w, $max_h, $crop ); 257 if ( ! $dims ) 258 return new WP_Error( 'error_getting_dimensions', __('Could not calculate resized image dimensions') ); 264 if ( ! $dims ) { 265 return new WP_Error( 'error_getting_dimensions', __( 'Could not calculate resized image dimensions' ) ); 266 } 259 267 list( $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ) = $dims; 260 268 … … 336 344 */ 337 345 if ( is_callable( array( $this->image, 'sampleImage' ) ) ) { 338 $resize_ratio = ( $dst_w / $this->size['width'] ) * ( $dst_h / $this->size['height'] );346 $resize_ratio = ( $dst_w / $this->size['width'] ) * ( $dst_h / $this->size['height'] ); 339 347 $sample_factor = 5; 340 348 … … 399 407 $this->image->setInterlaceScheme( Imagick::INTERLACE_NO ); 400 408 } 401 402 } 403 catch ( Exception $e ) { 409 } catch ( Exception $e ) { 404 410 return new WP_Error( 'image_resize_error', $e->getMessage() ); 405 411 } … … 429 435 */ 430 436 public function multi_resize( $sizes ) { 431 $metadata = array();432 $orig_size = $this->size;437 $metadata = array(); 438 $orig_size = $this->size; 433 439 $orig_image = $this->image->getImage(); 434 440 435 441 foreach ( $sizes as $size => $size_data ) { 436 if ( ! $this->image ) 442 if ( ! $this->image ) { 437 443 $this->image = $orig_image->getImage(); 444 } 438 445 439 446 if ( ! isset( $size_data['width'] ) && ! isset( $size_data['height'] ) ) { … … 453 460 454 461 $resize_result = $this->resize( $size_data['width'], $size_data['height'], $size_data['crop'] ); 455 $duplicate = ( ( $orig_size['width'] == $size_data['width'] ) && ( $orig_size['height'] == $size_data['height'] ) );462 $duplicate = ( ( $orig_size['width'] == $size_data['width'] ) && ( $orig_size['height'] == $size_data['height'] ) ); 456 463 457 464 if ( ! is_wp_error( $resize_result ) && ! $duplicate ) { … … 464 471 if ( ! is_wp_error( $resized ) && $resized ) { 465 472 unset( $resized['path'] ); 466 $metadata[ $size] = $resized;473 $metadata[ $size ] = $resized; 467 474 } 468 475 } … … 498 505 try { 499 506 $this->image->cropImage( $src_w, $src_h, $src_x, $src_y ); 500 $this->image->setImagePage( $src_w, $src_h, 0, 0 );507 $this->image->setImagePage( $src_w, $src_h, 0, 0 ); 501 508 502 509 if ( $dst_w || $dst_h ) { 503 510 // If destination width/height isn't specified, use same as 504 511 // width/height from source. 505 if ( ! $dst_w ) 512 if ( ! $dst_w ) { 506 513 $dst_w = $src_w; 507 if ( ! $dst_h ) 514 } 515 if ( ! $dst_h ) { 508 516 $dst_h = $src_h; 517 } 509 518 510 519 $thumb_result = $this->thumbnail_image( $dst_w, $dst_h ); … … 515 524 return $this->update_size(); 516 525 } 517 } 518 catch ( Exception $e ) { 526 } catch ( Exception $e ) { 519 527 return new WP_Error( 'image_crop_error', $e->getMessage() ); 520 528 } … … 536 544 */ 537 545 try { 538 $this->image->rotateImage( new ImagickPixel( 'none'), 360-$angle );546 $this->image->rotateImage( new ImagickPixel( 'none' ), 360 - $angle ); 539 547 540 548 // Normalise Exif orientation data so that display is consistent across devices. … … 545 553 // Since this changes the dimensions of the image, update the size. 546 554 $result = $this->update_size(); 547 if ( is_wp_error( $result ) ) 555 if ( is_wp_error( $result ) ) { 548 556 return $result; 557 } 549 558 550 559 $this->image->setImagePage( $this->size['width'], $this->size['height'], 0, 0 ); 551 } 552 catch ( Exception $e ) { 560 } catch ( Exception $e ) { 553 561 return new WP_Error( 'image_rotate_error', $e->getMessage() ); 554 562 } … … 567 575 public function flip( $horz, $vert ) { 568 576 try { 569 if ( $horz ) 577 if ( $horz ) { 570 578 $this->image->flipImage(); 571 572 if ( $vert ) 579 } 580 581 if ( $vert ) { 573 582 $this->image->flopImage(); 574 }575 catch ( Exception $e ) {583 } 584 } catch ( Exception $e ) { 576 585 return new WP_Error( 'image_flip_error', $e->getMessage() ); 577 586 } … … 592 601 593 602 if ( ! is_wp_error( $saved ) ) { 594 $this->file = $saved['path'];603 $this->file = $saved['path']; 595 604 $this->mime_type = $saved['mime-type']; 596 605 597 606 try { 598 607 $this->image->setImageFormat( strtoupper( $this->get_extension( $this->mime_type ) ) ); 599 } 600 catch ( Exception $e ) { 608 } catch ( Exception $e ) { 601 609 return new WP_Error( 'image_save_error', $e->getMessage(), $this->file ); 602 610 } … … 607 615 608 616 /** 609 *610 617 * @param Imagick $image 611 618 * @param string $filename … … 616 623 list( $filename, $extension, $mime_type ) = $this->get_output_format( $filename, $mime_type ); 617 624 618 if ( ! $filename ) 625 if ( ! $filename ) { 619 626 $filename = $this->generate_filename( null, null, $extension ); 627 } 620 628 621 629 try { … … 628 636 // Reset original Format 629 637 $this->image->setImageFormat( $orig_format ); 630 } 631 catch ( Exception $e ) { 638 } catch ( Exception $e ) { 632 639 return new WP_Error( 'image_save_error', $e->getMessage(), $filename ); 633 640 } 634 641 635 642 // Set correct file permissions 636 $stat = stat( dirname( $filename ) );643 $stat = stat( dirname( $filename ) ); 637 644 $perms = $stat['mode'] & 0000666; //same permissions as parent folder, strip off the executable bits 638 645 @ chmod( $filename, $perms ); … … 669 676 // Reset Image to original Format 670 677 $this->image->setImageFormat( $this->get_extension( $this->mime_type ) ); 671 } 672 catch ( Exception $e ) { 678 } catch ( Exception $e ) { 673 679 return new WP_Error( 'image_stream_error', $e->getMessage() ); 674 680 } … … 720 726 } 721 727 } 722 723 728 } catch ( Exception $e ) { 724 729 return new WP_Error( 'image_strip_meta_error', $e->getMessage() ); … … 744 749 // Only load the first page. 745 750 return $this->file . '[0]'; 746 } 747 catch ( Exception $e ) { 751 } catch ( Exception $e ) { 748 752 return new WP_Error( 'pdf_setup_failed', $e->getMessage(), $this->file ); 749 753 }
Note: See TracChangeset
for help on using the changeset viewer.