Changeset 30660
- Timestamp:
- 11/30/2014 07:53:18 PM (10 years ago)
- Location:
- trunk
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/media.php
r30656 r30660 380 380 $larger_ratio = max( $width_ratio, $height_ratio ); 381 381 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 ) { 383 383 // The larger ratio is too big. It would result in an overflow. 384 384 $ratio = $smaller_ratio; 385 else385 } else { 386 386 // The larger ratio fits, and is likely to be a more "snug" fit. 387 387 $ratio = $larger_ratio; 388 } 388 389 389 390 // 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 ) ); 392 393 393 394 // Sometimes, due to rounding, we'll end up with a result like this: 465x700 in a 177x177 box is 117x176... a pixel short 394 395 // 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. 395 396 // 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 ) { 397 400 $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 ) { 399 405 $h = $max_height; // Round it up 400 401 return array( $w, $h ); 406 } 407 408 return apply_filters( 'wp_constrain_dimensions', array( $w, $h ), $current_width, $current_height, $max_width, $max_height ); 402 409 } 403 410 … … 460 467 $new_h = min($dest_h, $orig_h); 461 468 462 if ( ! $new_w ) {463 $new_w = intval($new_h * $aspect_ratio);464 } 465 466 if ( ! $new_h ) {467 $new_h = intval($new_w / $aspect_ratio);469 if ( ! $new_w ) { 470 $new_w = (int) round( $new_h * $aspect_ratio ); 471 } 472 473 if ( ! $new_h ) { 474 $new_h = (int) round( $new_w / $aspect_ratio ); 468 475 } 469 476 -
trunk/tests/phpunit/tests/image/editor_gd.php
r30549 r30660 283 283 // #0 284 284 array( 285 'file' => 'waffles-10x 6.jpg',285 'file' => 'waffles-10x7.jpg', 286 286 'width' => 10, 287 'height' => 6,287 'height' => 7, 288 288 'mime-type' => 'image/jpeg', 289 289 ), … … 323 323 // #5 324 324 array( 325 'file' => 'waffles-55x3 6.jpg',325 'file' => 'waffles-55x37.jpg', 326 326 'width' => 55, 327 'height' => 3 6,327 'height' => 37, 328 328 'mime-type' => 'image/jpeg', 329 329 ), … … 331 331 // #6 332 332 array( 333 'file' => 'waffles-8 2x55.jpg',334 'width' => 8 2,333 'file' => 'waffles-83x55.jpg', 334 'width' => 83, 335 335 'height' => 55, 336 336 'mime-type' => 'image/jpeg', -
trunk/tests/phpunit/tests/image/editor_imagick.php
r30549 r30660 283 283 // #0 284 284 array( 285 'file' => 'waffles-10x 6.jpg',285 'file' => 'waffles-10x7.jpg', 286 286 'width' => 10, 287 'height' => 6,287 'height' => 7, 288 288 'mime-type' => 'image/jpeg', 289 289 ), … … 323 323 // #5 324 324 array( 325 'file' => 'waffles-55x3 6.jpg',325 'file' => 'waffles-55x37.jpg', 326 326 'width' => 55, 327 'height' => 3 6,327 'height' => 37, 328 328 'mime-type' => 'image/jpeg', 329 329 ), … … 331 331 // #6 332 332 array( 333 'file' => 'waffles-8 2x55.jpg',334 'width' => 8 2,333 'file' => 'waffles-83x55.jpg', 334 'width' => 83, 335 335 'height' => 55, 336 336 'mime-type' => 'image/jpeg', -
trunk/tests/phpunit/tests/image/resize.php
r29120 r30660 14 14 $image = $this->resize_helper( DIR_TESTDATA.'/images/test-image.jpg', 25, 25 ); 15 15 16 $this->assertEquals( 'test-image-25x25.jpg', basename( $image) );16 $this->assertEquals( 'test-image-25x25.jpg', basename( $image ) ); 17 17 list($w, $h, $type) = getimagesize($image); 18 18 $this->assertEquals( 25, $w ); … … 26 26 $image = $this->resize_helper( DIR_TESTDATA.'/images/test-image.png', 25, 25 ); 27 27 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 ) ); 29 34 list($w, $h, $type) = getimagesize($image); 30 35 $this->assertEquals( 25, $w ); … … 38 43 $image = $this->resize_helper( DIR_TESTDATA.'/images/test-image.gif', 25, 25 ); 39 44 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 ) ); 41 51 list($w, $h, $type) = getimagesize($image); 42 52 $this->assertEquals( 25, $w ); … … 58 68 $image = $this->resize_helper( DIR_TESTDATA.'/images/2007-06-17DSC_4173.JPG', 128, 96 ); 59 69 60 $this->assertEquals( '2007-06-17DSC_4173-6 3x96.jpg', basename($image) );70 $this->assertEquals( '2007-06-17DSC_4173-64x96.jpg', basename( $image ) ); 61 71 list($w, $h, $type) = getimagesize($image); 62 $this->assertEquals( 6 3, $w );72 $this->assertEquals( 64, $w ); 63 73 $this->assertEquals( 96, $h ); 64 74 $this->assertEquals( IMAGETYPE_JPEG, $type ); … … 70 80 $image = $this->resize_helper( DIR_TESTDATA.'/images/2007-06-17DSC_4173.JPG', 128, 0 ); 71 81 72 $this->assertEquals( '2007-06-17DSC_4173-128x19 2.jpg', basename($image) );82 $this->assertEquals( '2007-06-17DSC_4173-128x193.jpg', basename( $image ) ); 73 83 list($w, $h, $type) = getimagesize($image); 74 84 $this->assertEquals( 128, $w ); 75 $this->assertEquals( 19 2, $h );85 $this->assertEquals( 193, $h ); 76 86 $this->assertEquals( IMAGETYPE_JPEG, $type ); 77 87 … … 82 92 $image = $this->resize_helper( DIR_TESTDATA.'/images/2007-06-17DSC_4173.JPG', 0, 96 ); 83 93 84 $this->assertEquals( '2007-06-17DSC_4173-6 3x96.jpg', basename($image) );94 $this->assertEquals( '2007-06-17DSC_4173-64x96.jpg', basename( $image ) ); 85 95 list($w, $h, $type) = getimagesize($image); 86 $this->assertEquals( 6 3, $w );96 $this->assertEquals( 64, $w ); 87 97 $this->assertEquals( 96, $h ); 88 98 $this->assertEquals( IMAGETYPE_JPEG, $type ); … … 94 104 $image = $this->resize_helper( DIR_TESTDATA.'/images/2007-06-17DSC_4173.JPG', 150, 150, true ); 95 105 96 $this->assertEquals( '2007-06-17DSC_4173-150x150.jpg', basename( $image) );106 $this->assertEquals( '2007-06-17DSC_4173-150x150.jpg', basename( $image ) ); 97 107 list($w, $h, $type) = getimagesize($image); 98 108 $this->assertEquals( 150, $w ); … … 106 116 $image = $this->resize_helper( DIR_TESTDATA.'/images/2007-06-17DSC_4173.JPG', 150, 100, true ); 107 117 108 $this->assertEquals( '2007-06-17DSC_4173-150x100.jpg', basename( $image) );118 $this->assertEquals( '2007-06-17DSC_4173-150x100.jpg', basename( $image ) ); 109 119 list($w, $h, $type) = getimagesize($image); 110 120 $this->assertEquals( 150, $w ); … … 118 128 $image = $this->resize_helper( DIR_TESTDATA.'/images/2007-06-17DSC_4173.JPG', 50, 150, true ); 119 129 120 $this->assertEquals( '2007-06-17DSC_4173-50x150.jpg', basename( $image) );130 $this->assertEquals( '2007-06-17DSC_4173-50x150.jpg', basename( $image ) ); 121 131 list($w, $h, $type) = getimagesize($image); 122 132 $this->assertEquals( 50, $w ); … … 143 153 */ 144 154 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 145 161 $image = $this->resize_helper( DIR_TESTDATA.'/export/crazy-cdata.xml', 25, 25 ); 146 162 $this->assertInstanceOf( 'WP_Error', $image ); -
trunk/tests/phpunit/tests/image/size.php
r27358 r30660 14 14 // no constraint - should have no effect 15 15 $out = wp_constrain_dimensions(640, 480, 0, 0); 16 $this->assert Equals(array(640, 480), $out);16 $this->assertSame( array( 640, 480 ), $out ); 17 17 18 18 $out = wp_constrain_dimensions(640, 480); 19 $this->assert Equals(array(640, 480), $out);19 $this->assertSame( array( 640, 480 ), $out ); 20 20 21 21 $out = wp_constrain_dimensions(0, 0, 0, 0); 22 $this->assertEquals(array(0, 0), $out); 22 $this->assertSame( array( 0, 0 ), $out ); 23 24 $out = wp_constrain_dimensions(465, 700, 177, 177); 25 $this->assertSame( array( 118, 177 ), $out ); 23 26 } 24 27 … … 29 32 // image size is smaller than the constraint - no effect 30 33 $out = wp_constrain_dimensions(500, 600, 1024, 768); 31 $this->assert Equals(array(500, 600), $out);34 $this->assertSame( array( 500, 600 ), $out ); 32 35 33 36 $out = wp_constrain_dimensions(500, 600, 0, 768); 34 $this->assert Equals(array(500, 600), $out);37 $this->assertSame( array( 500, 600 ), $out ); 35 38 36 39 $out = wp_constrain_dimensions(500, 600, 1024, 0); 37 $this->assert Equals(array(500, 600), $out);40 $this->assertSame( array( 500, 600 ), $out ); 38 41 } 39 42 … … 44 47 // image size is equal to the constraint - no effect 45 48 $out = wp_constrain_dimensions(1024, 768, 1024, 768); 46 $this->assert equals(array(1024, 768), $out);49 $this->assertSame( array( 1024, 768 ), $out ); 47 50 48 51 $out = wp_constrain_dimensions(1024, 768, 0, 768); 49 $this->assert equals(array(1024, 768), $out);52 $this->assertSame( array( 1024, 768 ), $out ); 50 53 51 54 $out = wp_constrain_dimensions(1024, 768, 1024, 0); 52 $this->assert equals(array(1024, 768), $out);55 $this->assertSame( array( 1024, 768 ), $out ); 53 56 } 54 57 … … 59 62 // image size is larger than the constraint - result should be constrained 60 63 $out = wp_constrain_dimensions(1024, 768, 500, 600); 61 $this->assert equals(array(500, 375), $out);64 $this->assertSame( array( 500, 375 ), $out ); 62 65 63 66 $out = wp_constrain_dimensions(1024, 768, 0, 600); 64 $this->assert equals(array(800, 600), $out);67 $this->assertSame( array( 800, 600 ), $out ); 65 68 66 69 $out = wp_constrain_dimensions(1024, 768, 500, 0); 67 $this->assert equals(array(500, 375), $out);70 $this->assertSame( array( 500, 375 ), $out ); 68 71 69 72 // also try a portrait oriented image 70 73 $out = wp_constrain_dimensions(300, 800, 500, 600); 71 $this->assert equals(array(225, 600), $out);74 $this->assertSame( array( 225, 600 ), $out ); 72 75 73 76 $out = wp_constrain_dimensions(300, 800, 0, 600); 74 $this->assert equals(array(225, 600), $out);77 $this->assertSame( array( 225, 600 ), $out ); 75 78 76 79 $out = wp_constrain_dimensions(300, 800, 200, 0); 77 $this->assert equals(array(200, 533), $out);80 $this->assertSame( array( 200, 533 ), $out ); 78 81 } 79 82 … … 84 87 // one dimension is larger than the constraint, one smaller - result should be constrained 85 88 $out = wp_constrain_dimensions(1024, 768, 500, 800); 86 $this->assert equals(array(500, 375), $out);89 $this->assertSame( array( 500, 375 ), $out ); 87 90 88 91 $out = wp_constrain_dimensions(1024, 768, 2000, 700); 89 $this->assert equals(array(933, 700), $out);92 $this->assertSame( array( 933, 700 ), $out ); 90 93 91 94 // portrait 92 95 $out = wp_constrain_dimensions(768, 1024, 800, 500); 93 $this->assert equals(array(375, 500), $out);96 $this->assertSame( array( 375, 500 ), $out ); 94 97 95 98 $out = wp_constrain_dimensions(768, 1024, 2000, 700); 96 $this->assert equals(array(525, 700), $out);99 $this->assertSame( array( 525, 700 ), $out ); 97 100 } 98 101 … … 102 105 function test_shrink_dimensions_default() { 103 106 $out = wp_shrink_dimensions(640, 480); 104 $this->assert Equals(array(128, 96), $out);107 $this->assertSame( array( 128, 96 ), $out ); 105 108 106 109 $out = wp_shrink_dimensions(480, 640); 107 $this->assert Equals(array(72, 96), $out);110 $this->assertSame( array( 72, 96 ), $out ); 108 111 } 109 112 … … 114 117 // image size is smaller than the constraint - no effect 115 118 $out = wp_shrink_dimensions(500, 600, 1024, 768); 116 $this->assert Equals(array(500, 600), $out);119 $this->assertSame( array( 500, 600 ), $out ); 117 120 118 121 $out = wp_shrink_dimensions(600, 500, 1024, 768); 119 $this->assert Equals(array(600, 500), $out);122 $this->assertSame( array( 600, 500 ), $out ); 120 123 } 121 124 … … 126 129 // image size is equal to the constraint - no effect 127 130 $out = wp_shrink_dimensions(500, 600, 500, 600); 128 $this->assert Equals(array(500, 600), $out);131 $this->assertSame( array( 500, 600 ), $out ); 129 132 130 133 $out = wp_shrink_dimensions(600, 500, 600, 500); 131 $this->assert Equals(array(600, 500), $out);134 $this->assertSame( array( 600, 500 ), $out ); 132 135 } 133 136 … … 138 141 // image size is larger than the constraint - result should be constrained 139 142 $out = wp_shrink_dimensions(1024, 768, 500, 600); 140 $this->assert equals(array(500, 375), $out);143 $this->assertSame( array( 500, 375 ), $out ); 141 144 142 145 $out = wp_shrink_dimensions(300, 800, 500, 600); 143 $this->assert equals(array(225, 600), $out);146 $this->assertSame( array( 225, 600 ), $out ); 144 147 } 145 148 … … 150 153 // one dimension is larger than the constraint, one smaller - result should be constrained 151 154 $out = wp_shrink_dimensions(1024, 768, 500, 800); 152 $this->assert equals(array(500, 375), $out);155 $this->assertSame( array( 500, 375 ), $out ); 153 156 154 157 $out = wp_shrink_dimensions(1024, 768, 2000, 700); 155 $this->assert equals(array(933, 700), $out);158 $this->assertSame( array( 933, 700 ), $out ); 156 159 157 160 // portrait 158 161 $out = wp_shrink_dimensions(768, 1024, 800, 500); 159 $this->assert equals(array(375, 500), $out);162 $this->assertSame( array( 375, 500 ), $out ); 160 163 161 164 $out = wp_shrink_dimensions(768, 1024, 2000, 700); 162 $this->assert equals(array(525, 700), $out);165 $this->assertSame( array( 525, 700 ), $out ); 163 166 } 164 167 165 168 function test_constrain_size_for_editor_thumb() { 166 169 $out = image_constrain_size_for_editor(600, 400, 'thumb'); 167 $this->assert Equals(array(150, 100), $out);170 $this->assertSame( array( 150, 100 ), $out ); 168 171 169 172 $out = image_constrain_size_for_editor(64, 64, 'thumb'); 170 $this->assert Equals(array(64, 64), $out);173 $this->assertSame( array( 64, 64 ), $out ); 171 174 } 172 175 … … 182 185 183 186 $out = image_constrain_size_for_editor(600, 400, 'medium'); 184 $this->assert Equals(array(500, 333), $out);187 $this->assertSame( array( 500, 333 ), $out ); 185 188 186 189 $out = image_constrain_size_for_editor(400, 600, 'medium'); 187 $this->assert Equals(array(400, 600), $out);190 $this->assertSame( array( 400, 600 ), $out ); 188 191 189 192 $out = image_constrain_size_for_editor(64, 64, 'medium'); 190 $this->assert Equals(array(64, 64), $out);193 $this->assertSame( array( 64, 64 ), $out ); 191 194 192 195 // content_width should be ignored 193 196 $content_width = 350; 194 197 $out = image_constrain_size_for_editor(600, 400, 'medium'); 195 $this->assert Equals(array(500, 333), $out);198 $this->assertSame( array( 500, 333 ), $out ); 196 199 197 200 $content_width = $_content_width; … … 205 208 $content_width = 400; 206 209 $out = image_constrain_size_for_editor(600, 400, 'full'); 207 $this->assert Equals(array(600, 400), $out);210 $this->assertSame( array( 600, 400 ), $out ); 208 211 209 212 $out = image_constrain_size_for_editor(64, 64, 'full'); 210 $this->assert Equals(array(64, 64), $out);213 $this->assertSame( array( 64, 64 ), $out ); 211 214 212 215 // content_width default is 500 … … 214 217 215 218 $out = image_constrain_size_for_editor(600, 400, 'full'); 216 $this->assert Equals(array(600, 400), $out);219 $this->assertSame( array( 600, 400 ), $out ); 217 220 218 221 $out = image_constrain_size_for_editor(64, 64, 'full'); 219 $this->assert Equals(array(64, 64), $out);222 $this->assertSame( array( 64, 64 ), $out ); 220 223 221 224 $content_width = $_content_width;
Note: See TracChangeset
for help on using the changeset viewer.