Ticket #26823: 26823.2.diff
File 26823.2.diff, 20.7 KB (added by , 11 years ago) |
---|
-
src/wp-includes/class-wp-image-editor-gd.php
diff --git src/wp-includes/class-wp-image-editor-gd.php src/wp-includes/class-wp-image-editor-gd.php index fdd5586..0b13ad0 100644
class WP_Image_Editor_GD extends WP_Image_Editor { 140 140 * Resizes current image. 141 141 * Wraps _resize, since _resize returns a GD Resource. 142 142 * 143 * At minimum, either a height or width must be provided. 144 * If one of the two is set to null, the resize will 145 * maintain aspect ratio according to the provided dimension. 146 * 143 147 * @since 3.5.0 144 148 * @access public 145 149 * 146 * @param int $max_w147 * @param int $max_h150 * @param int|null $max_w Image Width 151 * @param int|null $max_h Image Height 148 152 * @param boolean $crop 149 153 * @return boolean|WP_Error 150 154 */ … … class WP_Image_Editor_GD extends WP_Image_Editor { 192 196 * @param array $sizes { 193 197 * An array of image size arrays. Default sizes are 'small', 'medium', 'large'. 194 198 * 199 * Either a height or width must be provided. 200 * If one of the two is set to null, the resize will 201 * maintain aspect ratio according to the provided dimension. 202 * 195 203 * @type array $size { 196 * @type int $widthImage width.197 * @type int $heightImage height.198 * @type bool $cropOptional. Whether to crop the image. Default false.204 * @type int ['width'] Optional. Image width. 205 * @type int ['height'] Optional. Image height. 206 * @type bool ['crop'] Optional. Whether to crop the image. Default false. 199 207 * } 200 208 * } 201 * @return array An array of resized images metadata by size.209 * @return array An array of resized images' metadata by size. 202 210 */ 203 211 public function multi_resize( $sizes ) { 204 212 $metadata = array(); 205 213 $orig_size = $this->size; 206 214 207 215 foreach ( $sizes as $size => $size_data ) { 208 if ( ! ( isset( $size_data['width'] ) && isset( $size_data['height'] ) ) )216 if ( ! isset( $size_data['width'] ) && ! isset( $size_data['height'] ) ) { 209 217 continue; 218 } 210 219 211 if ( ! isset( $size_data['crop'] ) ) 220 if ( ! isset( $size_data['width'] ) ) { 221 $size_data['width'] = null; 222 } 223 if ( ! isset( $size_data['height'] ) ) { 224 $size_data['height'] = null; 225 } 226 227 if ( ! isset( $size_data['crop'] ) ) { 212 228 $size_data['crop'] = false; 229 } 213 230 214 231 $image = $this->_resize( $size_data['width'], $size_data['height'], $size_data['crop'] ); 215 232 -
src/wp-includes/class-wp-image-editor-imagick.php
diff --git src/wp-includes/class-wp-image-editor-imagick.php src/wp-includes/class-wp-image-editor-imagick.php index f3c9451..be98447 100644
class WP_Image_Editor_Imagick extends WP_Image_Editor { 211 211 /** 212 212 * Resizes current image. 213 213 * 214 * At minimum, either a height or width must be provided. 215 * If one of the two is set to null, the resize will 216 * maintain aspect ratio according to the provided dimension. 217 * 214 218 * @since 3.5.0 215 219 * @access public 216 220 * 217 * @param int $max_w218 * @param int $max_h221 * @param int|null $max_w Image Width 222 * @param int|null $max_h Image Height 219 223 * @param boolean $crop 220 224 * @return boolean|WP_Error 221 225 */ … … class WP_Image_Editor_Imagick extends WP_Image_Editor { 255 259 * @param array $sizes { 256 260 * An array of image size arrays. Default sizes are 'small', 'medium', 'large'. 257 261 * 262 * Either a height or width must be provided. 263 * If one of the two is set to null, the resize will 264 * maintain aspect ratio according to the provided dimension. 265 * 258 266 * @type array $size { 259 * @type int $widthImage width.260 * @type int $heightImage height.267 * @type int ['width'] Optional. Image width. 268 * @type int ['height'] Optional. Image height. 261 269 * @type bool $crop Optional. Whether to crop the image. Default false. 262 270 * } 263 271 * } 264 * @return array An array of resized images metadata by size.272 * @return array An array of resized images' metadata by size. 265 273 */ 266 274 public function multi_resize( $sizes ) { 267 275 $metadata = array(); … … class WP_Image_Editor_Imagick extends WP_Image_Editor { 272 280 if ( ! $this->image ) 273 281 $this->image = $orig_image->getImage(); 274 282 275 if ( ! ( isset( $size_data['width'] ) && isset( $size_data['height'] ) ) )283 if ( ! isset( $size_data['width'] ) && ! isset( $size_data['height'] ) ) { 276 284 continue; 285 } 277 286 278 if ( ! isset( $size_data['crop'] ) ) 287 if ( ! isset( $size_data['width'] ) ) { 288 $size_data['width'] = null; 289 } 290 if ( ! isset( $size_data['height'] ) ) { 291 $size_data['height'] = null; 292 } 293 294 if ( ! isset( $size_data['crop'] ) ) { 279 295 $size_data['crop'] = false; 296 } 280 297 281 298 $resize_result = $this->resize( $size_data['width'], $size_data['height'], $size_data['crop'] ); 282 299 -
tests/phpunit/tests/image/editor_gd.php
diff --git tests/phpunit/tests/image/editor_gd.php tests/phpunit/tests/image/editor_gd.php index f5fdd99..c699012 100644
class Tests_Image_Editor_GD extends WP_Image_UnitTestCase { 17 17 } 18 18 19 19 /** 20 * Helper function to check actual image dimensions on disk 21 * 22 * @access private 23 * 24 * @param string Image Filename 25 * @param int Width to Check 26 * @param int Height to Check 27 */ 28 private function check_image_dimensions( $filename, $width, $height ) { 29 # Now, verify real dimensions are as expected 30 $image = new WP_Image_Editor_GD( $filename ); 31 $image->load(); 32 33 $this->assertEquals( 34 array( 35 'width' => $width, 36 'height' => $height, 37 ), 38 $image->get_size() 39 ); 40 } 41 42 /** 20 43 * Check support for GD compatible mime types. 21 *22 44 */ 23 45 public function test_supports_mime_type() { 24 46 $gd_image_editor = new WP_Image_Editor_GD( null ); … … class Tests_Image_Editor_GD extends WP_Image_UnitTestCase { 30 52 31 53 /** 32 54 * Test resizing an image, not using crop 33 *34 55 */ 35 56 public function test_resize() { 36 37 $file = DIR_TESTDATA . '/images/gradient-square.jpg'; 57 $file = DIR_TESTDATA . '/images/waffles.jpg'; 38 58 39 59 $gd_image_editor = new WP_Image_Editor_GD( $file ); 40 60 $gd_image_editor->load(); 41 61 42 62 $gd_image_editor->resize( 100, 50 ); 43 63 44 $this->assertEquals( array( 'width' => 50, 'height' => 50 ), $gd_image_editor->get_size() ); 64 $this->assertEquals( 65 array( 66 'width' => 75, 67 'height' => 50, 68 ), 69 $gd_image_editor->get_size() 70 ); 45 71 } 46 72 47 73 /** 48 * Test resizing an image including cropping 49 * 74 * Test multi_resize with single image resize and no crop 50 75 */ 51 public function test_resize_and_crop() { 76 public function test_single_multi_resize() { 77 $file = DIR_TESTDATA . '/images/waffles.jpg'; 78 79 $gd_image_editor = new WP_Image_Editor_GD( $file ); 80 $gd_image_editor->load(); 81 82 $sizes_array = array( 83 array( 84 'width' => 50, 85 'height' => 50, 86 ), 87 ); 88 89 $resized = $gd_image_editor->multi_resize( $sizes_array ); 90 91 # First, check to see if returned array is as expected 92 $expected_array = array( 93 array( 94 'file' => 'waffles-50x33.jpg', 95 'width' => 50, 96 'height' => 33, 97 'mime-type' => 'image/jpeg', 98 ), 99 ); 100 101 // Now, verify real dimensions are as expected 102 $image_path = DIR_TESTDATA . '/images/'. $resized[0]['file']; 103 $this->check_image_dimensions( 104 $image_path, 105 $expected_array[0]['width'], 106 $expected_array[0]['height'] 107 ); 108 109 unlink( $image_path ); 110 } 111 52 112 113 /** 114 * Test multi_resize with multiple sizes 115 * 116 * @ticket 26823 117 */ 118 public function test_multi_resize() { 119 $file = DIR_TESTDATA . '/images/waffles.jpg'; 120 121 $gd_image_editor = new WP_Image_Editor_GD( $file ); 122 $gd_image_editor->load(); 123 124 $sizes_array = array( 125 126 /** 127 * #0 - 10x10 resize, no cropping. 128 * By aspect, should be 10x6 output. 129 */ 130 array( 131 'width' => 10, 132 'height' => 10, 133 'crop' => false, 134 ), 135 136 /** 137 * #1 - 75x50 resize, with cropping. 138 * Output dimensions should be 75x50 139 */ 140 array( 141 'width' => 75, 142 'height' => 50, 143 'crop' => true, 144 ), 145 146 /** 147 * #2 - 20 pixel max height, no cropping. 148 * By aspect, should be 30x20 output. 149 */ 150 array( 151 'width' => 9999, # Arbitrary High Value 152 'height' => 20, 153 'crop' => false, 154 ), 155 156 /** 157 * #3 - 45 pixel max height, with cropping. 158 * By aspect, should be 45x400 output. 159 */ 160 array( 161 'width' => 45, 162 'height' => 9999, # Arbitrary High Value 163 'crop' => true, 164 ), 165 166 /** 167 * #4 - 50 pixel max width, no cropping. 168 * By aspect, should be 50x33 output. 169 */ 170 array( 171 'width' => 50, 172 ), 173 174 /** 175 * #5 - 55 pixel max width, no cropping, null height 176 * By aspect, should be 55x36 output. 177 */ 178 array( 179 'width' => 55, 180 'height' => null, 181 ), 182 183 /** 184 * #6 - 55 pixel max height, no cropping, no width specified. 185 * By aspect, should be 82x55 output. 186 */ 187 array( 188 'height' => 55, 189 ), 190 191 /** 192 * #7 - 60 pixel max height, no cropping, null width. 193 * By aspect, should be 90x60 output. 194 */ 195 array( 196 'width' => null, 197 'height' => 60, 198 ), 199 200 /** 201 * #8 - 70 pixel max height, no cropping, negative width. 202 * By aspect, should be 105x70 output. 203 */ 204 array( 205 'width' => -9999, # Arbitrary Negative Value 206 'height' => 70, 207 ), 208 209 /** 210 * #9 - 200 pixel max width, no cropping, negative height. 211 * By aspect, should be 200x133 output. 212 */ 213 array( 214 'width' => 200, 215 'height' => -9999, # Arbitrary Negative Value 216 ), 217 ); 218 219 $resized = $gd_image_editor->multi_resize( $sizes_array ); 220 221 $expected_array = array( 222 223 // #0 224 array( 225 'file' => 'waffles-10x6.jpg', 226 'width' => 10, 227 'height' => 6, 228 'mime-type' => 'image/jpeg', 229 ), 230 231 // #1 232 array( 233 'file' => 'waffles-75x50.jpg', 234 'width' => 75, 235 'height' => 50, 236 'mime-type' => 'image/jpeg', 237 ), 238 239 // #2 240 array( 241 'file' => 'waffles-30x20.jpg', 242 'width' => 30, 243 'height' => 20, 244 'mime-type' => 'image/jpeg', 245 ), 246 247 // #3 248 array( 249 'file' => 'waffles-45x400.jpg', 250 'width' => 45, 251 'height' => 400, 252 'mime-type' => 'image/jpeg', 253 ), 254 255 // #4 256 array( 257 'file' => 'waffles-50x33.jpg', 258 'width' => 50, 259 'height' => 33, 260 'mime-type' => 'image/jpeg', 261 ), 262 263 // #5 264 array( 265 'file' => 'waffles-55x36.jpg', 266 'width' => 55, 267 'height' => 36, 268 'mime-type' => 'image/jpeg', 269 ), 270 271 // #6 272 array( 273 'file' => 'waffles-82x55.jpg', 274 'width' => 82, 275 'height' => 55, 276 'mime-type' => 'image/jpeg', 277 ), 278 279 // #7 280 array( 281 'file' => 'waffles-90x60.jpg', 282 'width' => 90, 283 'height' => 60, 284 'mime-type' => 'image/jpeg', 285 ), 286 287 // #8 288 array( 289 'file' => 'waffles-105x70.jpg', 290 'width' => 105, 291 'height' => 70, 292 'mime-type' => 'image/jpeg', 293 ), 294 295 // #9 296 array( 297 'file' => 'waffles-200x133.jpg', 298 'width' => 200, 299 'height' => 133, 300 'mime-type' => 'image/jpeg', 301 ), 302 ); 303 304 $this->assertNotNull( $resized ); 305 $this->assertEquals( $expected_array, $resized ); 306 307 foreach( $resized as $key => $image_data ){ 308 $image_path = DIR_TESTDATA . '/images/' . $image_data['file']; 309 310 // Now, verify real dimensions are as expected 311 $this->check_image_dimensions( 312 $image_path, 313 $expected_array[$key]['width'], 314 $expected_array[$key]['height'] 315 ); 316 317 // Remove temporary file 318 unlink( $image_path ); 319 } 320 } 321 322 /** 323 * Test resizing an image with cropping 324 */ 325 public function test_resize_and_crop() { 53 326 $file = DIR_TESTDATA . '/images/gradient-square.jpg'; 54 327 55 328 $gd_image_editor = new WP_Image_Editor_GD( $file ); … … class Tests_Image_Editor_GD extends WP_Image_UnitTestCase { 57 330 58 331 $gd_image_editor->resize( 100, 50, true ); 59 332 60 $this->assertEquals( array( 'width' => 100, 'height' => 50 ), $gd_image_editor->get_size() ); 333 $this->assertEquals( 334 array( 335 'width' => 100, 336 'height' => 50, 337 ), 338 $gd_image_editor->get_size() 339 ); 61 340 } 62 341 63 342 /** 64 343 * Test cropping an image 65 344 */ 66 345 public function test_crop() { 67 68 346 $file = DIR_TESTDATA . '/images/gradient-square.jpg'; 69 347 70 348 $gd_image_editor = new WP_Image_Editor_GD( $file ); … … class Tests_Image_Editor_GD extends WP_Image_UnitTestCase { 72 350 73 351 $gd_image_editor->crop( 0, 0, 50, 50 ); 74 352 75 $this->assertEquals( array( 'width' => 50, 'height' => 50 ), $gd_image_editor->get_size() ); 353 $this->assertEquals( 354 array( 355 'width' => 50, 356 'height' => 50, 357 ), 358 $gd_image_editor->get_size() 359 ); 76 360 77 361 } 78 362 … … class Tests_Image_Editor_GD extends WP_Image_UnitTestCase { 80 364 * Test rotating an image 180 deg 81 365 */ 82 366 public function test_rotate() { 83 84 367 $file = DIR_TESTDATA . '/images/gradient-square.jpg'; 85 368 86 369 $gd_image_editor = new WP_Image_Editor_GD( $file ); … … class Tests_Image_Editor_GD extends WP_Image_UnitTestCase { 100 383 * Test flipping an image 101 384 */ 102 385 public function test_flip() { 103 104 386 $file = DIR_TESTDATA . '/images/gradient-square.jpg'; 105 387 106 388 $gd_image_editor = new WP_Image_Editor_GD( $file ); … … class Tests_Image_Editor_GD extends WP_Image_UnitTestCase { 122 404 * @ticket 23039 123 405 */ 124 406 public function test_image_preserves_alpha_on_resize() { 125 126 407 $file = DIR_TESTDATA . '/images/transparent.png'; 127 408 128 409 $editor = wp_get_image_editor( $file ); … … class Tests_Image_Editor_GD extends WP_Image_UnitTestCase { 142 423 * @ticket 23039 143 424 */ 144 425 public function test_image_preserves_alpha() { 145 146 426 $file = DIR_TESTDATA . '/images/transparent.png'; 147 427 148 428 $editor = wp_get_image_editor( $file ); -
tests/phpunit/tests/image/editor_imagick.php
diff --git tests/phpunit/tests/image/editor_imagick.php tests/phpunit/tests/image/editor_imagick.php index 1e43298..7060f44 100644
class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase { 24 24 } 25 25 26 26 /** 27 * Helper function to check actual image dimensions on disk 28 * 29 * @access private 30 * 31 * @param string Image Filename 32 * @param int Width to Check 33 * @param int Height to Check 34 */ 35 private function check_image_dimensions( $filename, $width, $height ) { 36 $imagick_image_editor = new WP_Image_Editor_Imagick( $filename ); 37 $imagick_image_editor->load(); 38 39 $this->assertEquals( 40 array( 41 'width' => $width, 42 'height' => $height, 43 ), 44 $imagick_image_editor->get_size() 45 ); 46 } 47 48 /** 27 49 * Check support for Image Magick compatible mime types. 28 *29 50 */ 30 51 public function test_supports_mime_type() { 31 52 … … class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase { 38 59 39 60 /** 40 61 * Test resizing an image, not using crop 41 *42 62 */ 43 63 public function test_resize() { 44 45 $file = DIR_TESTDATA . '/images/gradient-square.jpg'; 64 $file = DIR_TESTDATA . '/images/waffles.jpg'; 46 65 47 66 $imagick_image_editor = new WP_Image_Editor_Imagick( $file ); 48 67 $imagick_image_editor->load(); 49 68 50 69 $imagick_image_editor->resize( 100, 50 ); 51 70 52 $this->assertEquals( array( 'width' => 50, 'height' => 50 ), $imagick_image_editor->get_size() ); 71 $this->assertEquals( 72 array( 73 'width' => 75, 74 'height' => 50, 75 ), 76 $imagick_image_editor->get_size() 77 ); 53 78 } 54 79 55 80 /** 56 * Test resizing an image including cropping 57 * 81 * Test multi_resize with single image resize and no crop 58 82 */ 59 public function test_resize_and_crop() { 83 public function test_single_multi_resize() { 84 $file = DIR_TESTDATA . '/images/waffles.jpg'; 85 86 $test_image = new WP_Image_Editor_Imagick( $file ); 87 $test_image->load(); 88 89 $sizes_array = array( 90 array( 91 'width' => 50, 92 'height' => 50, 93 ), 94 ); 95 96 $resized = $test_image->multi_resize( $sizes_array ); 97 98 # First, check to see if returned array is as expected 99 $expected_array = array( 100 array( 101 'file' => 'waffles-50x33.jpg', 102 'width' => 50, 103 'height' => 33, 104 'mime-type' => 'image/jpeg', 105 ), 106 ); 107 108 $this->assertEquals( $expected_array, $resized ); 109 110 // Now, verify real dimensions are as expected 111 $image_path = DIR_TESTDATA . '/images/'. $resized[0]['file']; 112 $this->check_image_dimensions( 113 $image_path, 114 $expected_array[0]['width'], 115 $expected_array[0]['height'] 116 ); 117 118 unlink( $image_path ); 119 } 60 120 61 $file = DIR_TESTDATA . '/images/gradient-square.jpg'; 121 /** 122 * Test multi_resize with multiple sizes 123 * 124 * @ticket 26823 125 */ 126 public function test_multi_resize() { 127 $file = DIR_TESTDATA . '/images/waffles.jpg'; 62 128 63 129 $imagick_image_editor = new WP_Image_Editor_Imagick( $file ); 64 130 $imagick_image_editor->load(); 65 131 66 $imagick_image_editor->resize( 100, 50, true ); 67 68 $this->assertEquals( array( 'width' => 100, 'height' => 50 ), $imagick_image_editor->get_size() ); 132 $sizes_array = array( 133 134 /** 135 * #0 - 10x10 resize, no cropping. 136 * By aspect, should be 10x6 output. 137 */ 138 array( 139 'width' => 10, 140 'height' => 10, 141 'crop' => false, 142 ), 143 144 /** 145 * #1 - 75x50 resize, with cropping. 146 * Output dimensions should be 75x50 147 */ 148 array( 149 'width' => 75, 150 'height' => 50, 151 'crop' => true, 152 ), 153 154 /** 155 * #2 - 20 pixel max height, no cropping. 156 * By aspect, should be 30x20 output. 157 */ 158 array( 159 'width' => 9999, # Arbitrary High Value 160 'height' => 20, 161 'crop' => false, 162 ), 163 164 /** 165 * #3 - 45 pixel max height, with cropping. 166 * By aspect, should be 45x400 output. 167 */ 168 array( 169 'width' => 45, 170 'height' => 9999, # Arbitrary High Value 171 'crop' => true, 172 ), 173 174 /** 175 * #4 - 50 pixel max width, no cropping. 176 * By aspect, should be 50x33 output. 177 */ 178 array( 179 'width' => 50, 180 ), 181 182 /** 183 * #5 - 55 pixel max width, no cropping, null height 184 * By aspect, should be 55x36 output. 185 */ 186 array( 187 'width' => 55, 188 'height' => null, 189 ), 190 191 /** 192 * #6 - 55 pixel max height, no cropping, no width specified. 193 * By aspect, should be 82x55 output. 194 */ 195 array( 196 'height' => 55, 197 ), 198 199 /** 200 * #7 - 60 pixel max height, no cropping, null width. 201 * By aspect, should be 90x60 output. 202 */ 203 array( 204 'width' => null, 205 'height' => 60, 206 ), 207 208 /** 209 * #8 - 70 pixel max height, no cropping, negative width. 210 * By aspect, should be 105x70 output. 211 */ 212 array( 213 'width' => -9999, # Arbitrary Negative Value 214 'height' => 70, 215 ), 216 217 /** 218 * #9 - 200 pixel max width, no cropping, negative height. 219 * By aspect, should be 200x133 output. 220 */ 221 array( 222 'width' => 200, 223 'height' => -9999, # Arbitrary Negative Value 224 ), 225 ); 226 227 $resized = $imagick_image_editor->multi_resize( $sizes_array ); 228 229 $expected_array = array( 230 231 // #0 232 array( 233 'file' => 'waffles-10x6.jpg', 234 'width' => 10, 235 'height' => 6, 236 'mime-type' => 'image/jpeg', 237 ), 238 239 // #1 240 array( 241 'file' => 'waffles-75x50.jpg', 242 'width' => 75, 243 'height' => 50, 244 'mime-type' => 'image/jpeg', 245 ), 246 247 // #2 248 array( 249 'file' => 'waffles-30x20.jpg', 250 'width' => 30, 251 'height' => 20, 252 'mime-type' => 'image/jpeg', 253 ), 254 255 // #3 256 array( 257 'file' => 'waffles-45x400.jpg', 258 'width' => 45, 259 'height' => 400, 260 'mime-type' => 'image/jpeg', 261 ), 262 263 // #4 264 array( 265 'file' => 'waffles-50x33.jpg', 266 'width' => 50, 267 'height' => 33, 268 'mime-type' => 'image/jpeg', 269 ), 270 271 // #5 272 array( 273 'file' => 'waffles-55x36.jpg', 274 'width' => 55, 275 'height' => 36, 276 'mime-type' => 'image/jpeg', 277 ), 278 279 // #6 280 array( 281 'file' => 'waffles-82x55.jpg', 282 'width' => 82, 283 'height' => 55, 284 'mime-type' => 'image/jpeg', 285 ), 286 287 // #7 288 array( 289 'file' => 'waffles-90x60.jpg', 290 'width' => 90, 291 'height' => 60, 292 'mime-type' => 'image/jpeg', 293 ), 294 295 // #8 296 array( 297 'file' => 'waffles-105x70.jpg', 298 'width' => 105, 299 'height' => 70, 300 'mime-type' => 'image/jpeg', 301 ), 302 303 // #9 304 array( 305 'file' => 'waffles-200x133.jpg', 306 'width' => 200, 307 'height' => 133, 308 'mime-type' => 'image/jpeg', 309 ), 310 ); 311 312 $this->assertNotNull( $resized ); 313 $this->assertEquals( $expected_array, $resized ); 314 315 foreach( $resized as $key => $image_data ){ 316 $image_path = DIR_TESTDATA . '/images/' . $image_data['file']; 317 318 // Now, verify real dimensions are as expected 319 $this->check_image_dimensions( 320 $image_path, 321 $expected_array[$key]['width'], 322 $expected_array[$key]['height'] 323 ); 324 325 // Remove temporary file 326 unlink( $image_path ); 327 } 69 328 } 70 329 71 330 /**