Changeset 10178
- Timestamp:
- 10/03/2015 10:42:02 PM (9 years ago)
- Location:
- trunk
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-core/classes/class-bp-attachment-avatar.php
r10145 r10178 120 120 * 121 121 * @uses bp_core_avatar_original_max_width() 122 * @uses wp_get_image_editor()123 122 * 124 123 * @param string $file the absolute path to the file. … … 128 127 public static function shrink( $file = '' ) { 129 128 // Get image size 130 $size = @getimagesize( $file ); 131 $retval = false; 132 133 // Check image size and shrink if too large 134 if ( $size[0] > bp_core_avatar_original_max_width() ) { 135 $editor = wp_get_image_editor( $file ); 136 137 if ( ! is_wp_error( $editor ) ) { 138 $editor->set_quality( 100 ); 139 140 $resized = $editor->resize( bp_core_avatar_original_max_width(), bp_core_avatar_original_max_width(), false ); 141 if ( ! is_wp_error( $resized ) ) { 142 $thumb = $editor->save( $editor->generate_filename() ); 143 } else { 144 $retval = $resized; 145 } 146 147 // Check for thumbnail creation errors 148 if ( ( false === $retval ) && is_wp_error( $thumb ) ) { 149 $retval = $thumb; 150 } 151 152 // Thumbnail is good so proceed 153 if ( false === $retval ) { 154 $retval = $thumb; 155 } 156 157 } else { 158 $retval = $editor; 159 } 160 } 161 162 return $retval; 129 $avatar_data = parent::get_image_data( $file ); 130 131 // Init the edit args 132 $edit_args = array(); 133 134 // Do we need to resize the image ? 135 if ( isset( $avatar_data['width'] ) && $avatar_data['width'] > bp_core_avatar_original_max_width() ) { 136 $edit_args = array( 137 'max_w' => bp_core_avatar_original_max_width(), 138 'max_h' => bp_core_avatar_original_max_width(), 139 ); 140 } 141 142 // Do we need to rotate the image ? 143 $angles = array( 144 3 => 180, 145 6 => -90, 146 8 => 90, 147 ); 148 149 if ( isset( $avatar_data['meta']['orientation'] ) && isset( $angles[ $avatar_data['meta']['orientation'] ] ) ) { 150 $edit_args['rotate'] = $angles[ $avatar_data['meta']['orientation'] ]; 151 } 152 153 // No need to edit the avatar, original file will be used 154 if ( empty( $edit_args ) ) { 155 return false; 156 157 // Add the file to the edit arguments 158 } else { 159 $edit_args['file'] = $file; 160 } 161 162 return parent::edit_image( 'avatar', $edit_args ); 163 163 } 164 164 -
trunk/src/bp-core/classes/class-bp-attachment-cover-image.php
r10156 r10178 139 139 140 140 // Get image size 141 $size = @getimagesize( $file ); 142 $retval = false; 143 144 // Check image size and shrink if too large 145 if ( $size[0] > $dimensions['width'] || $size[1] > $dimensions['height'] ) { 146 $editor = wp_get_image_editor( $file ); 147 148 if ( ! is_wp_error( $editor ) ) { 149 $editor->set_quality( 100 ); 150 151 $resized = $editor->resize( $dimensions['width'], $dimensions['height'], true ); 152 if ( ! is_wp_error( $resized ) ) { 153 $cover = $editor->save( $this->generate_filename( $file ) ); 154 } else { 155 $retval = $resized; 156 } 157 158 // Check for cover creation errors 159 if ( ( false === $retval ) && is_wp_error( $cover ) ) { 160 $retval = $cover; 161 } 162 163 // Cover is good so proceed 164 if ( false === $retval ) { 165 $retval = $cover; 166 } 167 168 } else { 169 $retval = $editor; 170 } 171 } 172 173 return $retval; 141 $cover_data = parent::get_image_data( $file ); 142 143 // Init the edit args 144 $edit_args = array(); 145 146 // Do we need to resize the image ? 147 if ( ( isset( $cover_data['width'] ) && $cover_data['width'] > $dimensions['width'] ) || 148 ( isset( $cover_data['height'] ) && $cover_data['height'] > $dimensions['height'] ) ) { 149 $edit_args = array( 150 'max_w' => $dimensions['width'], 151 'max_h' => $dimensions['height'], 152 'crop' => true, 153 ); 154 } 155 156 // Do we need to rotate the image ? 157 $angles = array( 158 3 => 180, 159 6 => -90, 160 8 => 90, 161 ); 162 163 if ( isset( $cover_data['meta']['orientation'] ) && isset( $angles[ $cover_data['meta']['orientation'] ] ) ) { 164 $edit_args['rotate'] = $angles[ $cover_data['meta']['orientation'] ]; 165 } 166 167 // No need to edit the avatar, original file will be used 168 if ( empty( $edit_args ) ) { 169 return false; 170 171 // Add the file to the edit arguments 172 } else { 173 $edit_args = array_merge( $edit_args, array( 'file' => $file, 'save' => false ) ); 174 } 175 176 // Get the editor so that we can use a specific save method 177 $editor = parent::edit_image( 'cover_image', $edit_args ); 178 179 if ( is_wp_error( $editor ) ) { 180 return $editor; 181 } elseif ( ! is_a( $editor, 'WP_Image_Editor' ) ) { 182 return false; 183 } 184 185 // Save the new image file 186 return $editor->save( $this->generate_filename( $file ) ); 174 187 } 175 188 -
trunk/src/bp-core/classes/class-bp-attachment.php
r10108 r10178 522 522 return $script_data; 523 523 } 524 525 /** 526 * Get full data for an image 527 * 528 * @since 2.4.0 529 * 530 * @param string $file Absolute path to the uploaded image. 531 * @return bool|array An associate array containing the width, height and metadatas. 532 * False in case an important image attribute is missing. 533 */ 534 public static function get_image_data( $file ) { 535 // Try to get image basic data 536 list( $width, $height, $sourceImageType ) = @getimagesize( $file ); 537 538 // No need to carry on if we couldn't get image's basic data. 539 if ( is_null( $width ) || is_null( $height ) || is_null( $sourceImageType ) ) { 540 return false; 541 } 542 543 // Initialize the image data 544 $image_data = array( 545 'width' => $width, 546 'height' => $height, 547 ); 548 549 /** 550 * Make sure the wp_read_image_metadata function is reachable for the old Avatar UI 551 * or if WordPress < 3.9 (New Avatar UI is not available in this case) 552 */ 553 if ( ! function_exists( 'wp_read_image_metadata' ) ) { 554 require_once( ABSPATH . 'wp-admin/includes/image.php' ); 555 } 556 557 // Now try to get image's meta data 558 $meta = wp_read_image_metadata( $file ); 559 560 if ( ! empty( $meta ) ) { 561 // Before 4.0 the Orientation wasn't included 562 if ( ! isset( $meta['orientation'] ) && 563 is_callable( 'exif_read_data' ) && 564 in_array( $sourceImageType, apply_filters( 'wp_read_image_metadata_types', array( IMAGETYPE_JPEG, IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM ) ) ) 565 ) { 566 $exif = exif_read_data( $file ); 567 568 if ( ! empty( $exif['Orientation'] ) ) { 569 $meta['orientation'] = $exif['Orientation']; 570 } 571 } 572 573 // Now add the metas to image data 574 $image_data['meta'] = $meta; 575 } 576 577 /** 578 * Filter here to add/remove/edit data to the image full data 579 * 580 * @since 2.4.0 581 * 582 * @param array $image_data An associate array containing the width, height and metadatas. 583 */ 584 return apply_filters( 'bp_attachments_get_image_data', $image_data ); 585 } 586 587 /** 588 * Edit an image file to resize it or rotate it 589 * 590 * @since 2.4.0 591 * 592 * @param string $attachment_type The attachment type (eg: avatar or cover_image). Required. 593 * @param array array $args { 594 * @type string $file Absolute path to the image file (required). 595 * @type int $max_w Max width attribute for the editor's resize method (optional). 596 * @type int $max_h Max height attribute for the editor's resize method (optional). 597 * @type bool $crop Crop attribute for the editor's resize method (optional). 598 * @type float $rotate Angle for the editor's rotate method (optional). 599 * @type int $quality Compression quality on a 1-100% scale (optional). 600 * @type bool $save Whether to use the editor's save method or not (optional). 601 * } 602 * 603 * @return string|WP_Image_Editor|WP_Error The edited image path or the WP_Image_Editor object in case of success, 604 * an WP_Error object otherwise. 605 */ 606 public static function edit_image( $attachment_type, $args = array() ) { 607 if ( empty( $attachment_type ) ) { 608 return new WP_Error( 'missing_parameter' ); 609 } 610 611 $r = bp_parse_args( $args, array( 612 'file' => '', 613 'max_w' => 0, 614 'max_h' => 0, 615 'crop' => false, 616 'rotate' => 0, 617 'quality' => 90, 618 'save' => true, 619 ), 'attachment_' . $attachment_type . '_edit_image' ); 620 621 // Make sure we have to edit the image. 622 if ( empty( $r['max_w'] ) && empty( $r['max_h'] ) && empty( $r['rotate'] ) && empty( $r['file'] ) ) { 623 return new WP_Error( 'missing_parameter' ); 624 } 625 626 // Get the image editor 627 $editor = wp_get_image_editor( $r['file'] ); 628 629 if ( is_wp_error( $editor ) ) { 630 return $editor; 631 } 632 633 $editor->set_quality( $r['quality'] ); 634 635 if ( ! empty( $r['rotate'] ) ) { 636 $rotated = $editor->rotate( $r['rotate'] ); 637 638 // Stop in case of error 639 if ( is_wp_error( $rotated ) ) { 640 return $rotated; 641 } 642 } 643 644 if ( ! empty( $r['max_w'] ) || ! empty( $r['max_h'] ) ) { 645 $resized = $editor->resize( $r['max_w'], $r['max_h'], $r['crop'] ); 646 647 // Stop in case of error 648 if ( is_wp_error( $resized ) ) { 649 return $resized; 650 } 651 } 652 653 // Use the editor save method to get a path to the edited image 654 if ( true === $r['save'] ) { 655 return $editor->save( $editor->generate_filename() ); 656 657 // Need to do some other edit actions or use a specific method to save file 658 } else { 659 return $editor; 660 } 661 } 524 662 } -
trunk/tests/phpunit/testcases/core/class-bp-attachment.php
r10177 r10178 444 444 $this->clean_files( 'buddypress' ); 445 445 } 446 447 /** 448 * @group avatars 449 * @group cover_images 450 */ 451 public function test_bp_attachment_get_image_data() { 452 $image_data = BP_Attachment::get_image_data( BP_TESTS_DIR . 'assets/upside-down.jpg' ); 453 454 $this->assertTrue( 3 === $image_data['meta']['orientation'] ); 455 } 446 456 }
Note: See TracChangeset
for help on using the changeset viewer.