Ticket #5089: 5089.04.patch
| File 5089.04.patch, 11.6 KB (added by , 11 years ago) |
|---|
-
src/bp-core/classes/class-bp-attachment-avatar.php
diff --git src/bp-core/classes/class-bp-attachment-avatar.php src/bp-core/classes/class-bp-attachment-avatar.php index 91cd15c..604ee23 100644
class BP_Attachment_Avatar extends BP_Attachment { 119 119 * @since 2.3.0 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. 125 124 * … … class BP_Attachment_Avatar extends BP_Attachment { 127 126 */ 128 127 public static function shrink( $file = '' ) { 129 128 // Get image size 130 $size = @getimagesize( $file ); 131 $retval = false; 129 $avatar_data = parent::get_image_data( $file ); 132 130 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 ); 131 // Init the edit args 132 $edit_args = array(); 136 133 137 if ( ! is_wp_error( $editor ) ) { 138 $editor->set_quality( 100 ); 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 } 139 141 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 }142 // Do we need to rotate the image ? 143 $angles = array( 144 3 => 180, 145 6 => -90, 146 8 => 90, 147 ); 146 148 147 // Check for thumbnail creation errors 148 if ( ( false === $retval ) && is_wp_error( $thumb ) ) { 149 $retval = $thumb; 150 } 149 if ( isset( $avatar_data['meta']['orientation'] ) && isset( $angles[ $avatar_data['meta']['orientation'] ] ) ) { 150 $edit_args['rotate'] = $angles[ $avatar_data['meta']['orientation'] ]; 151 } 151 152 152 // Thumbnail is good so proceed 153 if ( false === $retval ) { 154 $retval = $thumb; 155 } 153 // No need to edit the avatar, original file will be used 154 if ( empty( $edit_args ) ) { 155 return false; 156 156 157 } else {158 $retval = $editor;159 }157 // Add the file to the edit arguments 158 } else { 159 $edit_args['file'] = $file; 160 160 } 161 161 162 return $retval;162 return parent::edit_image( 'avatar', $edit_args ); 163 163 } 164 164 165 165 /** -
src/bp-core/classes/class-bp-attachment-cover-image.php
diff --git src/bp-core/classes/class-bp-attachment-cover-image.php src/bp-core/classes/class-bp-attachment-cover-image.php index fc700f9..ed32cbc 100644
class BP_Attachment_Cover_Image extends BP_Attachment { 138 138 } 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 } 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; 171 183 } 172 184 173 return $retval; 185 // Save the new image file 186 return $editor->save( $this->generate_filename( $file ) ); 174 187 } 175 188 176 189 /** -
src/bp-core/classes/class-bp-attachment.php
diff --git src/bp-core/classes/class-bp-attachment.php src/bp-core/classes/class-bp-attachment.php index 7ea8c50..dbfdf56 100644
abstract class BP_Attachment { 521 521 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 array An associate array containing the width, height and metadatas. 532 */ 533 public static function get_image_data( $file ) { 534 // Try to get image basic data 535 list( $width, $height, $sourceImageType ) = @getimagesize( $file ); 536 537 // No need to carry on if we couldn't get image's basic data. 538 if ( is_null( $width ) || is_null( $height ) || is_null( $sourceImageType ) ) { 539 return false; 540 } 541 542 // Initialize the image data 543 $image_data = array( 544 'width' => $width, 545 'height' => $height, 546 ); 547 548 /** 549 * Make sure the wp_read_image_metadata function is reachable for the old Avatar UI 550 * or if WordPress < 3.9 (New Avatar UI is not available in this case) 551 */ 552 if ( ! function_exists( 'wp_read_image_metadata' ) ) { 553 require_once( ABSPATH . '/wp-admin/includes/image.php' ); 554 } 555 556 // Now try to get image's meta data 557 $meta = wp_read_image_metadata( $file ); 558 559 if ( ! empty( $meta ) ) { 560 // Before 4.0 the Orientation wasn't included 561 if ( ! isset( $meta['orientation'] ) && 562 is_callable( 'exif_read_data' ) && 563 in_array( $sourceImageType, apply_filters( 'wp_read_image_metadata_types', array( IMAGETYPE_JPEG, IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM ) ) ) 564 ) { 565 $exif = @exif_read_data( $file ); 566 567 if ( ! empty( $exif['Orientation'] ) ) { 568 $meta['orientation'] = $exif['Orientation']; 569 } 570 } 571 572 // Now add the metas to image data 573 $image_data['meta'] = $meta; 574 } 575 576 /** 577 * Filter here to add/remove/edit data to the image full data 578 * 579 * @since 2.4.0 580 * 581 * @param array $image_data An associate array containing the width, height and metadatas. 582 */ 583 return apply_filters( 'bp_attachments_get_image_data', $image_data ); 584 } 585 586 /** 587 * Edit an image file to resize it or rotate it 588 * 589 * @since 2.4.0 590 * 591 * @param string $attachment_type The attachment type (eg: avatar or cover_image). Required. 592 * @param array array $args { 593 * @type string $file Absolute path to the image file (required). 594 * @type int $max_w Max width attribute for the editor's resize method (optional). 595 * @type int $max_h Max height attribute for the editor's resize method (optional). 596 * @type bool $crop Crop attribute for the editor's resize method (optional). 597 * @type float $rotate Angle for the editor's rotate method (optional). 598 * @type int $quality Compression quality on a 1-100% scale (optional). 599 * @type bool $save Whether to use the editor's save method or not (optional). 600 * } 601 * 602 * @return string|WP_Image_Editor|WP_Error The edited image path or the WP_Image_Editor object in case of success, 603 * an WP_Error object otherwise. 604 */ 605 public static function edit_image( $attachment_type, $args = array() ) { 606 if ( empty( $attachment_type ) ) { 607 return new WP_Error( 'missing_parameter' ); 608 } 609 610 $r = bp_parse_args( $args, array( 611 'file' => '', 612 'max_w' => 0, 613 'max_h' => 0, 614 'crop' => false, 615 'rotate' => 0, 616 'quality' => 90, 617 'save' => true, 618 ), 'attachment_' . $attachment_type . '_edit_image' ); 619 620 // Make sure we have to edit the image. 621 if ( empty( $r['max_w'] ) && empty( $r['max_h'] ) && empty( $r['rotate'] ) && empty( $r['file'] ) ) { 622 return new WP_Error( 'missing_parameter' ); 623 } 624 625 // Get the image editor 626 $editor = wp_get_image_editor( $r['file'] ); 627 628 if ( is_wp_error( $editor ) ) { 629 return $editor; 630 } 631 632 $editor->set_quality( $r['quality'] ); 633 634 if ( ! empty( $r['rotate'] ) ) { 635 $rotated = $editor->rotate( $r['rotate'] ); 636 637 // Stop in case of error 638 if ( is_wp_error( $rotated ) ) { 639 return $rotated; 640 } 641 } 642 643 if ( ! empty( $r['max_w'] ) || ! empty( $r['max_h'] ) ) { 644 $resized = $editor->resize( $r['max_w'], $r['max_h'], $r['crop'] ); 645 646 // Stop in case of error 647 if ( is_wp_error( $resized ) ) { 648 return $resized; 649 } 650 } 651 652 // Use the editor save method to get a path to the edited image 653 if ( true === $r['save'] ) { 654 return $editor->save( $editor->generate_filename() ); 655 656 // Need to do some other edit actions or use a specific method to save file 657 } else { 658 return $editor; 659 } 660 } 524 661 } -
tests/phpunit/testcases/core/class-bp-attachment.php
diff --git tests/phpunit/testcases/core/class-bp-attachment.php tests/phpunit/testcases/core/class-bp-attachment.php index 35d0476..adacf38 100644
class BP_Tests_BP_Attachment_TestCases extends BP_UnitTestCase { 364 364 $_FILES = $reset_files; 365 365 $_POST = $reset_post; 366 366 } 367 368 /** 369 * @ticket BP5089 370 */ 371 public function test_bp_attachment_avatar_shrink() { 372 $image = BP_TESTS_DIR . 'assets/upside-down.jpg'; 373 374 $dir_copy = bp_upload_dir(); 375 376 // in case cleaning files fails 377 if ( ! is_dir( $dir_copy['basedir'] . '/shrink' ) ) { 378 mkdir( $dir_copy['basedir'] . '/shrink' ); 379 } 380 381 $abs_path_copy = $dir_copy['basedir'] . '/shrink/upside-down.jpg'; 382 383 copy( $image, $abs_path_copy ); 384 385 add_filter( 'bp_core_avatar_original_max_width', array( $this, 'limit_to_50px' ) ); 386 387 $shrink = BP_Attachment_Avatar::shrink( $abs_path_copy ); 388 389 remove_filter( 'bp_core_avatar_original_max_width', array( $this, 'limit_to_50px' ) ); 390 391 $this->assertTrue( 50 === $shrink['width'] && 50 === $shrink['height'] ); 392 393 // Cleanup 394 $this->clean_files( 'shrink' ); 395 } 396 397 public function limit_to_50px( $max_width ) { 398 return 50; 399 } 400 401 /** 402 * @ticket BP5089 403 */ 404 public function test_bp_attachment_cover_image_fit() { 405 $image = BP_TESTS_DIR . 'assets/upside-down.jpg'; 406 407 $cover_image_class = new BP_Attachment_Cover_Image(); 408 409 $abs_path_copy = $cover_image_class->upload_path . '/upside-down.jpg'; 410 411 copy( $image, $abs_path_copy ); 412 413 $fit = $cover_image_class->fit( $abs_path_copy, array( 'width' => 50, 'height' => 50 ) ); 414 415 $this->assertTrue( 50 === $fit['width'] && 50 === $fit['height'] ); 416 417 // Cleanup 418 $this->clean_files( 'buddypress' ); 419 } 420 421 /** 422 * @ticket BP5089 423 */ 424 public function test_bp_attachment_get_image_data() { 425 $image_data = BP_Attachment::get_image_data( BP_TESTS_DIR . 'assets/upside-down.jpg' ); 426 427 $this->assertTrue( 3 === $image_data['meta']['orientation'] ); 428 } 367 429 }