Ticket #34384: 34384.2.diff
| File 34384.2.diff, 8.9 KB (added by , 10 years ago) |
|---|
-
src/wp-includes/media.php
diff --git src/wp-includes/media.php src/wp-includes/media.php index df96a17..f8d2b0b 100644
function image_make_intermediate_size( $file, $width, $height, $crop = false ) { 592 592 } 593 593 594 594 /** 595 * Helper function to tests if aspect ratios for two images match. 596 * 597 * @since 4.6.0 598 * 599 * @param int $img_a_width Width of the first image in pixels. 600 * @param int $img_a_height Height of the first image in pixels. 601 * @param int $img_b_width Width of the second image in pixels. 602 * @param int $img_b_height Height of the second image in pixels. 603 * @return bool True if aspect ratios match within 1px. False if not. 604 */ 605 function wp_image_matches_ratio( $img_a_width, $img_a_height, $img_b_width, $img_b_height ) { 606 /* 607 * To check for varying crops, we calculate the expected size of the smaller 608 * image if the larger were constrained by the width of the smaller and then 609 * see if it matches what we're expecting. 610 */ 611 if ( $img_a_width > $img_b_width ) { 612 $constrained_size = wp_constrain_dimensions( $img_a_width, $img_a_height, $img_b_width ); 613 $expected_size = array( $img_b_width, $img_b_height ); 614 } else { 615 $constrained_size = wp_constrain_dimensions( $img_b_width, $img_b_height, $img_a_width ); 616 $expected_size = array( $img_a_width, $img_a_height ); 617 } 618 619 // If the image dimensions are within 1px of the expected size, use it. 620 return ( abs( $constrained_size[0] - $expected_size[0] ) <= 1 && abs( $constrained_size[1] - $expected_size[1] ) <= 1 ) ? true : false; 621 } 622 623 624 625 /** 595 626 * Retrieves the image's intermediate size (resized) path, width, and height. 596 627 * 597 628 * The $size parameter can be an array with the width and height respectively. … … function image_make_intermediate_size( $file, $width, $height, $crop = false ) { 623 654 * @type string $file Image's path relative to uploads directory 624 655 * @type int $width Width of image 625 656 * @type int $height Height of image 626 * @type string $path Optional. Image's absolute filesystem path. Only returned if registered 627 * size is passed to `$size` parameter. 628 * @type string $url Optional. Image's URL. Only returned if registered size is passed to `$size` 629 * parameter. 657 * @type string $path Image's absolute filesystem path. 658 * @type string $url Image's URL. 630 659 * } 631 660 */ 632 661 function image_get_intermediate_size( $post_id, $size = 'thumbnail' ) { 633 if ( ! is_array( $imagedata = wp_get_attachment_metadata( $post_id ) ) )662 if ( ! $size || ! is_array( $imagedata = wp_get_attachment_metadata( $post_id ) ) || empty( $imagedata['sizes'] ) ) { 634 663 return false; 664 } 665 666 $data = array(); 635 667 636 // get the best one for a specified set of dimensions637 if ( is_array( $size) && !empty($imagedata['sizes']) ) {668 // Find the best match when '$size' is an array. 669 if ( is_array( $size ) ) { 638 670 $candidates = array(); 639 671 640 672 foreach ( $imagedata['sizes'] as $_size => $data ) { 641 673 // If there's an exact match to an existing image size, short circuit. 642 674 if ( $data['width'] == $size[0] && $data['height'] == $size[1] ) { 643 list( $data['width'], $data['height'] ) = image_constrain_size_for_editor( $data['width'], $data['height'], $size ); 644 645 /** This filter is documented in wp-includes/media.php */ 646 return apply_filters( 'image_get_intermediate_size', $data, $post_id, $size ); 675 $candidates[ $data['width'] * $data['height'] ] = $data; 676 break; 647 677 } 648 // If it's not an exact match but it's at least the dimensions requested. 678 679 // If it's not an exact match, match any larger sizes with the same aspect ratio. 649 680 if ( $data['width'] >= $size[0] && $data['height'] >= $size[1] ) { 650 $candidates[ $data['width'] * $data['height'] ] = $_size; 681 // If '0' is passed to either size, we test ratios agains the original file. 682 if ( 0 === $size[0] || 0 === $size[1] ) { 683 $same_ratio = wp_image_matches_ratio( $data['width'], $data['height'], $imagedata['width'], $imagedata['height'] ); 684 } else { 685 $same_ratio = wp_image_matches_ratio( $data['width'], $data['height'], $size[0], $size[1] ); 686 } 687 688 if ( $same_ratio ) { 689 $candidates[ $data['width'] * $data['height'] ] = $data; 690 } 651 691 } 652 692 } 653 693 654 694 if ( ! empty( $candidates ) ) { 655 // find for the smallest image not smaller than the desired size 656 ksort( $candidates ); 657 foreach ( $candidates as $_size ) { 658 $data = $imagedata['sizes'][$_size]; 659 660 // Skip images with unexpectedly divergent aspect ratios (crops) 661 // First, we calculate what size the original image would be if constrained to a box the size of the current image in the loop 662 $maybe_cropped = image_resize_dimensions($imagedata['width'], $imagedata['height'], $data['width'], $data['height'], false ); 663 // If the size doesn't match within one pixel, then it is of a different aspect ratio, so we skip it, unless it's the thumbnail size 664 if ( 'thumbnail' != $_size && 665 ( ! $maybe_cropped 666 || ( $maybe_cropped[4] != $data['width'] && $maybe_cropped[4] + 1 != $data['width'] ) 667 || ( $maybe_cropped[5] != $data['height'] && $maybe_cropped[5] + 1 != $data['height'] ) 668 ) ) { 669 continue; 670 } 671 // If we're still here, then we're going to use this size. 672 list( $data['width'], $data['height'] ) = image_constrain_size_for_editor( $data['width'], $data['height'], $size ); 673 674 /** This filter is documented in wp-includes/media.php */ 675 return apply_filters( 'image_get_intermediate_size', $data, $post_id, $size ); 695 // Sort the array by size if we have more than one candidate. 696 if ( 1 < count( $candidates ) ) { 697 ksort( $candidates ); 676 698 } 699 700 $data = array_shift( $candidates ); 701 // Constrain the width and height attributes to the requested values. 702 list( $data['width'], $data['height'] ) = image_constrain_size_for_editor( $data['width'], $data['height'], $size ); 703 } elseif ( ! empty( $imagedata['sizes']['thumnbnail'] ) ) { 704 // If we still have not candidates, we fall back to the thumbnail. 705 $data = $imagedata['sizes']['thumnbnail']; 706 // Constrain the width and height attributes to the requested values. 707 list( $data['width'], $data['height'] ) = image_constrain_size_for_editor( $data['width'], $data['height'], $size ); 708 } else { 709 return false; 677 710 } 711 } elseif ( ! empty( $imagedata['sizes'][ $size ] ) ) { 712 $data = $imagedata['sizes'][ $size ]; 678 713 } 679 714 680 if ( is_array($size) || empty($size) || empty($imagedata['sizes'][$size]) ) 715 // If we still don't have a match at this point, return false. 716 if ( empty( $data ) ) { 681 717 return false; 718 } 682 719 683 $data = $imagedata['sizes'][$size];684 720 // include the full filesystem path of the intermediate file 685 721 if ( empty($data['path']) && !empty($data['file']) ) { 686 722 $file_url = wp_get_attachment_url($post_id); … … function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attac 1092 1128 continue; 1093 1129 } 1094 1130 1095 /**1096 * To check for varying crops, we calculate the expected size of the smaller1097 * image if the larger were constrained by the width of the smaller and then1098 * see if it matches what we're expecting.1099 */1100 if ( $image_width > $image['width'] ) {1101 $constrained_size = wp_constrain_dimensions( $image_width, $image_height, $image['width'] );1102 $expected_size = array( $image['width'], $image['height'] );1103 } else {1104 $constrained_size = wp_constrain_dimensions( $image['width'], $image['height'], $image_width );1105 $expected_size = array( $image_width, $image_height );1106 }1107 1108 1131 // If the image dimensions are within 1px of the expected size, use it. 1109 if ( abs( $constrained_size[0] - $expected_size[0] ) <= 1 && abs( $constrained_size[1] - $expected_size[1] ) <= 1) {1132 if ( wp_image_matches_ratio( $image_width, $image_height, $image['width'], $image['height'] ) ) { 1110 1133 // Add the URL, descriptor, and value to the sources array to be returned. 1111 1134 $source = array( 1112 1135 'url' => $image_baseurl . $image['file'], -
tests/phpunit/tests/image/intermediate_size.php
diff --git tests/phpunit/tests/image/intermediate_size.php tests/phpunit/tests/image/intermediate_size.php index 3808655..92ff2c7 100644
class Tests_Image_Intermediate_Size extends WP_UnitTestCase { 224 224 225 225 $this->assertTrue( strpos( $image['file'], $width . 'x' . $height ) > 0 ); 226 226 } 227 228 /** 229 * @ticket 34384 230 */ 231 public function test_get_intermediate_size_with_small_size_array() { 232 // If an exact size is not found, it should be returned 233 add_image_size( 'test-size', 100, 50, true ); 234 235 $file = DIR_TESTDATA . '/images/waffles.jpg'; 236 $id = $this->_make_attachment( $file, 0 ); 237 238 // Look for a small size by array that doesn't exist. 239 $image = image_get_intermediate_size( $id, array( 50, 25 ) ); 240 241 // We should get the small test size and not the thumbnail. 242 $this->assertTrue( strpos( $image['file'], '100x50' ) > 0 ); 243 } 227 244 }
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)