Skip to content

Commit 35708f6

Browse files
committed
Use round() instead of floor() when resizing image dimensions.
Updates unit tests. Props SergeyBiryukov, kitchin. Fixes #18532. git-svn-id: https://develop.svn.wordpress.org/trunk@30660 602fd350-edb4-49c9-b593-d223f7449a82
1 parent dad4408 commit 35708f6

5 files changed

Lines changed: 102 additions & 76 deletions

File tree

src/wp-includes/media.php

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -379,26 +379,33 @@ function wp_constrain_dimensions( $current_width, $current_height, $max_width=0,
379379
$smaller_ratio = min( $width_ratio, $height_ratio );
380380
$larger_ratio = max( $width_ratio, $height_ratio );
381381

382-
if ( intval( $current_width * $larger_ratio ) > $max_width || intval( $current_height * $larger_ratio ) > $max_height )
382+
if ( (int) round( $current_width * $larger_ratio ) > $max_width || (int) round( $current_height * $larger_ratio ) > $max_height ) {
383383
// The larger ratio is too big. It would result in an overflow.
384384
$ratio = $smaller_ratio;
385-
else
385+
} else {
386386
// The larger ratio fits, and is likely to be a more "snug" fit.
387387
$ratio = $larger_ratio;
388+
}
388389

389390
// Very small dimensions may result in 0, 1 should be the minimum.
390-
$w = max ( 1, intval( $current_width * $ratio ) );
391-
$h = max ( 1, intval( $current_height * $ratio ) );
391+
$w = max ( 1, (int) round( $current_width * $ratio ) );
392+
$h = max ( 1, (int) round( $current_height * $ratio ) );
392393

393394
// Sometimes, due to rounding, we'll end up with a result like this: 465x700 in a 177x177 box is 117x176... a pixel short
394395
// We also have issues with recursive calls resulting in an ever-changing result. Constraining to the result of a constraint should yield the original result.
395396
// Thus we look for dimensions that are one pixel shy of the max value and bump them up
396-
if ( $did_width && $w == $max_width - 1 )
397+
398+
// Note: $did_width means it is possible $smaller_ratio == $width_ratio.
399+
if ( $did_width && $w == $max_width - 1 ) {
397400
$w = $max_width; // Round it up
398-
if ( $did_height && $h == $max_height - 1 )
401+
}
402+
403+
// Note: $did_height means it is possible $smaller_ratio == $height_ratio.
404+
if ( $did_height && $h == $max_height - 1 ) {
399405
$h = $max_height; // Round it up
406+
}
400407

401-
return array( $w, $h );
408+
return apply_filters( 'wp_constrain_dimensions', array( $w, $h ), $current_width, $current_height, $max_width, $max_height );
402409
}
403410

404411
/**
@@ -459,12 +466,12 @@ function image_resize_dimensions($orig_w, $orig_h, $dest_w, $dest_h, $crop = fal
459466
$new_w = min($dest_w, $orig_w);
460467
$new_h = min($dest_h, $orig_h);
461468

462-
if ( !$new_w ) {
463-
$new_w = intval($new_h * $aspect_ratio);
469+
if ( ! $new_w ) {
470+
$new_w = (int) round( $new_h * $aspect_ratio );
464471
}
465472

466-
if ( !$new_h ) {
467-
$new_h = intval($new_w / $aspect_ratio);
473+
if ( ! $new_h ) {
474+
$new_h = (int) round( $new_w / $aspect_ratio );
468475
}
469476

470477
$size_ratio = max($new_w / $orig_w, $new_h / $orig_h);

tests/phpunit/tests/image/editor_gd.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,9 @@ public function test_multi_resize() {
282282

283283
// #0
284284
array(
285-
'file' => 'waffles-10x6.jpg',
285+
'file' => 'waffles-10x7.jpg',
286286
'width' => 10,
287-
'height' => 6,
287+
'height' => 7,
288288
'mime-type' => 'image/jpeg',
289289
),
290290

@@ -322,16 +322,16 @@ public function test_multi_resize() {
322322

323323
// #5
324324
array(
325-
'file' => 'waffles-55x36.jpg',
325+
'file' => 'waffles-55x37.jpg',
326326
'width' => 55,
327-
'height' => 36,
327+
'height' => 37,
328328
'mime-type' => 'image/jpeg',
329329
),
330330

331331
// #6
332332
array(
333-
'file' => 'waffles-82x55.jpg',
334-
'width' => 82,
333+
'file' => 'waffles-83x55.jpg',
334+
'width' => 83,
335335
'height' => 55,
336336
'mime-type' => 'image/jpeg',
337337
),

tests/phpunit/tests/image/editor_imagick.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,9 @@ public function test_multi_resize() {
282282

283283
// #0
284284
array(
285-
'file' => 'waffles-10x6.jpg',
285+
'file' => 'waffles-10x7.jpg',
286286
'width' => 10,
287-
'height' => 6,
287+
'height' => 7,
288288
'mime-type' => 'image/jpeg',
289289
),
290290

@@ -322,16 +322,16 @@ public function test_multi_resize() {
322322

323323
// #5
324324
array(
325-
'file' => 'waffles-55x36.jpg',
325+
'file' => 'waffles-55x37.jpg',
326326
'width' => 55,
327-
'height' => 36,
327+
'height' => 37,
328328
'mime-type' => 'image/jpeg',
329329
),
330330

331331
// #6
332332
array(
333-
'file' => 'waffles-82x55.jpg',
334-
'width' => 82,
333+
'file' => 'waffles-83x55.jpg',
334+
'width' => 83,
335335
'height' => 55,
336336
'mime-type' => 'image/jpeg',
337337
),

tests/phpunit/tests/image/resize.php

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ abstract class WP_Tests_Image_Resize_UnitTestCase extends WP_Image_UnitTestCase
1313
function test_resize_jpg() {
1414
$image = $this->resize_helper( DIR_TESTDATA.'/images/test-image.jpg', 25, 25 );
1515

16-
$this->assertEquals( 'test-image-25x25.jpg', basename($image) );
16+
$this->assertEquals( 'test-image-25x25.jpg', basename( $image ) );
1717
list($w, $h, $type) = getimagesize($image);
1818
$this->assertEquals( 25, $w );
1919
$this->assertEquals( 25, $h );
@@ -25,7 +25,12 @@ function test_resize_jpg() {
2525
function test_resize_png() {
2626
$image = $this->resize_helper( DIR_TESTDATA.'/images/test-image.png', 25, 25 );
2727

28-
$this->assertEquals( 'test-image-25x25.png', basename($image) );
28+
if ( ! is_string( $image ) ) { // WP_Error, stop GLib-GObject-CRITICAL assertion
29+
$this->markTestSkipped( sprintf( 'No PNG support in the editor engine %s on this system', $this->editor_engine ) );
30+
return;
31+
}
32+
33+
$this->assertEquals( 'test-image-25x25.png', basename( $image ) );
2934
list($w, $h, $type) = getimagesize($image);
3035
$this->assertEquals( 25, $w );
3136
$this->assertEquals( 25, $h );
@@ -37,7 +42,12 @@ function test_resize_png() {
3742
function test_resize_gif() {
3843
$image = $this->resize_helper( DIR_TESTDATA.'/images/test-image.gif', 25, 25 );
3944

40-
$this->assertEquals( 'test-image-25x25.gif', basename($image) );
45+
if ( ! is_string( $image ) ) { // WP_Error, stop GLib-GObject-CRITICAL assertion
46+
$this->markTestSkipped( sprintf( 'No GIF support in the editor engine %s on this system', $this->editor_engine ) );
47+
return;
48+
}
49+
50+
$this->assertEquals( 'test-image-25x25.gif', basename( $image ) );
4151
list($w, $h, $type) = getimagesize($image);
4252
$this->assertEquals( 25, $w );
4353
$this->assertEquals( 25, $h );
@@ -57,9 +67,9 @@ function test_resize_larger() {
5767
function test_resize_thumb_128x96() {
5868
$image = $this->resize_helper( DIR_TESTDATA.'/images/2007-06-17DSC_4173.JPG', 128, 96 );
5969

60-
$this->assertEquals( '2007-06-17DSC_4173-63x96.jpg', basename($image) );
70+
$this->assertEquals( '2007-06-17DSC_4173-64x96.jpg', basename( $image ) );
6171
list($w, $h, $type) = getimagesize($image);
62-
$this->assertEquals( 63, $w );
72+
$this->assertEquals( 64, $w );
6373
$this->assertEquals( 96, $h );
6474
$this->assertEquals( IMAGETYPE_JPEG, $type );
6575

@@ -69,10 +79,10 @@ function test_resize_thumb_128x96() {
6979
function test_resize_thumb_128x0() {
7080
$image = $this->resize_helper( DIR_TESTDATA.'/images/2007-06-17DSC_4173.JPG', 128, 0 );
7181

72-
$this->assertEquals( '2007-06-17DSC_4173-128x192.jpg', basename($image) );
82+
$this->assertEquals( '2007-06-17DSC_4173-128x193.jpg', basename( $image ) );
7383
list($w, $h, $type) = getimagesize($image);
7484
$this->assertEquals( 128, $w );
75-
$this->assertEquals( 192, $h );
85+
$this->assertEquals( 193, $h );
7686
$this->assertEquals( IMAGETYPE_JPEG, $type );
7787

7888
unlink( $image );
@@ -81,9 +91,9 @@ function test_resize_thumb_128x0() {
8191
function test_resize_thumb_0x96() {
8292
$image = $this->resize_helper( DIR_TESTDATA.'/images/2007-06-17DSC_4173.JPG', 0, 96 );
8393

84-
$this->assertEquals( '2007-06-17DSC_4173-63x96.jpg', basename($image) );
94+
$this->assertEquals( '2007-06-17DSC_4173-64x96.jpg', basename( $image ) );
8595
list($w, $h, $type) = getimagesize($image);
86-
$this->assertEquals( 63, $w );
96+
$this->assertEquals( 64, $w );
8797
$this->assertEquals( 96, $h );
8898
$this->assertEquals( IMAGETYPE_JPEG, $type );
8999

@@ -93,7 +103,7 @@ function test_resize_thumb_0x96() {
93103
function test_resize_thumb_150x150_crop() {
94104
$image = $this->resize_helper( DIR_TESTDATA.'/images/2007-06-17DSC_4173.JPG', 150, 150, true );
95105

96-
$this->assertEquals( '2007-06-17DSC_4173-150x150.jpg', basename($image) );
106+
$this->assertEquals( '2007-06-17DSC_4173-150x150.jpg', basename( $image ) );
97107
list($w, $h, $type) = getimagesize($image);
98108
$this->assertEquals( 150, $w );
99109
$this->assertEquals( 150, $h );
@@ -105,7 +115,7 @@ function test_resize_thumb_150x150_crop() {
105115
function test_resize_thumb_150x100_crop() {
106116
$image = $this->resize_helper( DIR_TESTDATA.'/images/2007-06-17DSC_4173.JPG', 150, 100, true );
107117

108-
$this->assertEquals( '2007-06-17DSC_4173-150x100.jpg', basename($image) );
118+
$this->assertEquals( '2007-06-17DSC_4173-150x100.jpg', basename( $image ) );
109119
list($w, $h, $type) = getimagesize($image);
110120
$this->assertEquals( 150, $w );
111121
$this->assertEquals( 100, $h );
@@ -117,7 +127,7 @@ function test_resize_thumb_150x100_crop() {
117127
function test_resize_thumb_50x150_crop() {
118128
$image = $this->resize_helper( DIR_TESTDATA.'/images/2007-06-17DSC_4173.JPG', 50, 150, true );
119129

120-
$this->assertEquals( '2007-06-17DSC_4173-50x150.jpg', basename($image) );
130+
$this->assertEquals( '2007-06-17DSC_4173-50x150.jpg', basename( $image ) );
121131
list($w, $h, $type) = getimagesize($image);
122132
$this->assertEquals( 50, $w );
123133
$this->assertEquals( 150, $h );
@@ -142,6 +152,12 @@ public function test_resize_non_existent_image() {
142152
* @ticket 6821
143153
*/
144154
public function test_resize_bad_image() {
155+
156+
if ( $this->editor_engine == 'WP_Image_Editor_Imagick' ) {
157+
$this->markTestSkipped( sprintf( 'Avoid GLib-GObject-CRITICAL assertion in %s', $this->editor_engine ) );
158+
return;
159+
}
160+
145161
$image = $this->resize_helper( DIR_TESTDATA.'/export/crazy-cdata.xml', 25, 25 );
146162
$this->assertInstanceOf( 'WP_Error', $image );
147163
$this->assertEquals( 'invalid_image', $image->get_error_code() );

0 commit comments

Comments
 (0)