Ticket #32437: 32437.2.diff
File 32437.2.diff, 5.4 KB (added by , 5 years ago) |
---|
-
src/wp-includes/functions.php
7296 7296 function is_php_version_compatible( $required ) { 7297 7297 return empty( $required ) || version_compare( phpversion(), $required, '>=' ); 7298 7298 } 7299 7300 /** 7301 * Check if two numbers are nearly the same. 7302 * 7303 * This is similar to using `round()` but the precision is more fine-grained. 7304 * 7305 * @since 5.3.0 7306 * 7307 * @param int|float $expected The expected value. 7308 * @param int|float $actual The actual number. 7309 * @param int|float $precision The allowed variation. 7310 * @return bool Whether the numbers match whithin the specified precision. 7311 */ 7312 function wp_fuzzy_number_match( $expected, $actual, $precision = 1 ) { 7313 return abs( (float) $expected - (float) $actual ) <= $precision; 7314 } -
src/wp-includes/media.php
543 543 return $output; 544 544 } 545 545 546 // Stop if the destination size is larger than the original image dimensions. 547 if ( empty( $dest_h ) ) { 548 if ( $orig_w < $dest_w ) { 549 return false; 550 } 551 } elseif ( empty( $dest_w ) ) { 552 if ( $orig_h < $dest_h ) { 553 return false; 554 } 555 } else { 556 if ( $orig_w < $dest_w && $orig_h < $dest_h ) { 557 return false; 558 } 559 } 560 546 561 if ( $crop ) { 547 // crop the largest possible portion of the original image that we can size to $dest_w x $dest_h 562 // Crop the largest possible portion of the original image that we can size to $dest_w x $dest_h. 563 // Note that the requested crop dimensions are used as a maximum bounding box for the original image. 564 // If the original image's width or height is less than the requested width or height 565 // only the greater one will be cropped. 566 // For example when the original image is 600x300, and the requested crop dimensions are 400x400, 567 // the resulting image will be 400x300. 548 568 $aspect_ratio = $orig_w / $orig_h; 549 569 $new_w = min( $dest_w, $orig_w ); 550 570 $new_h = min( $dest_h, $orig_h ); … … 584 604 $s_y = floor( ( $orig_h - $crop_h ) / 2 ); 585 605 } 586 606 } else { 587 // don't crop, just resize using $dest_w x $dest_h as a maximum bounding box607 // Resize using $dest_w x $dest_h as a maximum bounding box. 588 608 $crop_w = $orig_w; 589 609 $crop_h = $orig_h; 590 610 … … 594 614 list( $new_w, $new_h ) = wp_constrain_dimensions( $orig_w, $orig_h, $dest_w, $dest_h ); 595 615 } 596 616 597 // if the resulting image would be the same size or larger we don't want to resize it 598 if ( $new_w >= $orig_w && $new_h >= $orig_h && intval( $dest_w ) !== intval( $orig_w ) && intval( $dest_h ) !== intval( $orig_h ) ) { 599 return false; 617 if ( wp_fuzzy_number_match( $new_w, $orig_w ) && wp_fuzzy_number_match( $new_h, $orig_h ) ) { 618 // The new size has virtually the same dimensions as the original image. 619 620 /** 621 * Filters whether to proceed with making an image sub-size with identical dimensions 622 * with the original/source image. Differences of 1px may be due to rounding and are ignored. 623 * 624 * @since 5.3.0 625 * 626 * @param bool The filtered value. 627 * @param int Original image width. 628 * @param int Original image height. 629 */ 630 $proceed = (bool) apply_filters( 'wp_image_resize_identical_dimensions', false, $orig_w, $orig_h ); 631 632 if ( ! $proceed ) { 633 return false; 634 } 600 635 } 601 636 602 // the return array matches the parameters to imagecopyresampled()637 // The return array matches the parameters to imagecopyresampled(). 603 638 // int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h 604 639 return array( 0, 0, (int) $s_x, (int) $s_y, (int) $new_w, (int) $new_h, (int) $crop_w, (int) $crop_h ); 605 606 640 } 607 641 608 642 /** … … 664 698 } 665 699 666 700 // If the image dimensions are within 1px of the expected size, we consider it a match. 667 $matched = ( abs( $constrained_size[0] - $expected_size[0] ) <= 1 && abs( $constrained_size[1] - $expected_size[1] ) <= 1);701 $matched = ( wp_fuzzy_number_match( $constrained_size[0], $expected_size[0] ) && wp_fuzzy_number_match( $constrained_size[1], $expected_size[1] ) ); 668 702 669 703 return $matched; 670 704 } -
tests/phpunit/tests/image/dimensions.php
131 131 function test_640x480() { 132 132 // crop 640x480 to fit 640x480 (no change) 133 133 $out = image_resize_dimensions( 640, 480, 640, 480, true ); 134 $this->assertFalse( $out ); 135 136 // resize 640x480 to fit 640x480 (no change) 137 $out = image_resize_dimensions( 640, 480, 640, 480, false ); 138 $this->assertFalse( $out ); 139 140 // Test with the filter override. 141 add_filter( 'wp_image_resize_identical_dimensions', '__return_true' ); 142 143 // crop 640x480 to fit 640x480 (no change) 144 $out = image_resize_dimensions( 640, 480, 640, 480, true ); 134 145 // dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h 135 146 $this->assertEquals( array( 0, 0, 0, 0, 640, 480, 640, 480 ), $out ); 136 147 … … 138 149 $out = image_resize_dimensions( 640, 480, 640, 480, false ); 139 150 // dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h 140 151 $this->assertEquals( array( 0, 0, 0, 0, 640, 480, 640, 480 ), $out ); 152 153 remove_filter( 'wp_image_resize_identical_dimensions', '__return_true' ); 141 154 } 142 155 143 156 /**