Ticket #26823: 26823.4.diff
File 26823.4.diff, 29.0 KB (added by , 10 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..303f500 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_h148 * @param boolean$crop150 * @param int|null $max_w Image width. 151 * @param int|null $max_h Image height. 152 * @param boolean $crop 149 153 * @return boolean|WP_Error 150 154 */ 151 155 public function resize( $max_w, $max_h, $crop = false ) { … … 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..4ebed4b 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_h219 * @param boolean$crop221 * @param int|null $max_w Image width. 222 * @param int|null $max_h Image height. 223 * @param boolean $crop 220 224 * @return boolean|WP_Error 221 225 */ 222 226 public function resize( $max_w, $max_h, $crop = false ) { … … 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 -
src/wp-includes/class-wp-image-editor.php
diff --git src/wp-includes/class-wp-image-editor.php src/wp-includes/class-wp-image-editor.php index 7f6488f..5affba4 100644
abstract class WP_Image_Editor { 82 82 /** 83 83 * Resizes current image. 84 84 * 85 * At minimum, either a height or width must be provided. 86 * If one of the two is set to null, the resize will 87 * maintain aspect ratio according to the provided dimension. 88 * 85 89 * @since 3.5.0 86 90 * @access public 87 91 * @abstract 88 92 * 89 * @param int $max_w90 * @param int $max_h91 * @param boolean$crop93 * @param int|null $max_w Image width. 94 * @param int|null $max_h Image height. 95 * @param boolean $crop 92 96 * @return boolean|WP_Error 93 97 */ 94 98 abstract public function resize( $max_w, $max_h, $crop = false ); -
tests/phpunit/tests/image/base.php
diff --git tests/phpunit/tests/image/base.php tests/phpunit/tests/image/base.php index 26f3b59..be9ab67 100644
abstract class WP_Image_UnitTestCase extends WP_UnitTestCase { 39 39 * @param int $alpha 40 40 */ 41 41 protected function assertImageAlphaAtPoint( $image_path, $point, $alpha ) { 42 43 42 $im = imagecreatefrompng( $image_path ); 44 $rgb = imagecolorat( $im, $point[0], $point[1]);43 $rgb = imagecolorat( $im, $point[0], $point[1] ); 45 44 46 $colors = imagecolorsforindex( $im, $rgb);45 $colors = imagecolorsforindex( $im, $rgb ); 47 46 48 47 $this->assertEquals( $alpha, $colors['alpha'] ); 49 48 } 49 50 /** 51 * Helper assertion to check actual image dimensions on disk 52 * 53 * @param string $filename Image filename. 54 * @param int $width Width to verify. 55 * @param int $height Height to verify. 56 */ 57 protected function assertImageDimensions( $filename, $width, $height ) { 58 $detected_width = 0; 59 $detected_height = 0; 60 $image_size = @getimagesize( $filename ); 61 62 if ( isset( $image_size[0] ) ) { 63 $detected_width = $image_size[0]; 64 } 65 66 if ( isset( $image_size[1] ) ) { 67 $detected_height = $image_size[1]; 68 } 69 70 $this->assertEquals( $width, $detected_width ); 71 $this->assertEquals( $height, $detected_height ); 72 } 50 73 } -
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..ca134bd 100644
8 8 */ 9 9 10 10 class Tests_Image_Editor_GD extends WP_Image_UnitTestCase { 11 11 12 public $editor_engine = 'WP_Image_Editor_GD'; 12 13 13 public function set up() {14 public function setUp() { 14 15 require_once( ABSPATH . WPINC . '/class-wp-image-editor.php' ); 15 16 require_once( ABSPATH . WPINC . '/class-wp-image-editor-gd.php' ); 17 16 18 parent::setUp(); 17 19 } 18 20 21 public function shutDown() { 22 $folder = DIR_TESTDATA . '/images/waffles-*.jpg'; 23 24 foreach ( glob( $folder ) as $file ) { 25 unlink( $file ); 26 } 27 28 parent::shutDown(); 29 } 30 19 31 /** 20 32 * Check support for GD compatible mime types. 21 *22 33 */ 23 34 public function test_supports_mime_type() { 24 35 $gd_image_editor = new WP_Image_Editor_GD( null ); … … class Tests_Image_Editor_GD extends WP_Image_UnitTestCase { 30 41 31 42 /** 32 43 * Test resizing an image, not using crop 33 *34 44 */ 35 45 public function test_resize() { 36 37 $file = DIR_TESTDATA . '/images/gradient-square.jpg'; 46 $file = DIR_TESTDATA . '/images/waffles.jpg'; 38 47 39 48 $gd_image_editor = new WP_Image_Editor_GD( $file ); 40 49 $gd_image_editor->load(); 41 50 42 51 $gd_image_editor->resize( 100, 50 ); 43 52 44 $this->assertEquals( array( 'width' => 50, 'height' => 50 ), $gd_image_editor->get_size() ); 53 $this->assertEquals( 54 array( 55 'width' => 75, 56 'height' => 50, 57 ), 58 $gd_image_editor->get_size() 59 ); 45 60 } 46 61 47 62 /** 48 * Test resizing an image including cropping 49 * 63 * Test multi_resize with single image resize and no crop 50 64 */ 51 public function test_resize_and_crop() { 65 public function test_single_multi_resize() { 66 $file = DIR_TESTDATA . '/images/waffles.jpg'; 52 67 53 $file = DIR_TESTDATA . '/images/gradient-square.jpg'; 68 $gd_image_editor = new WP_Image_Editor_GD( $file ); 69 $gd_image_editor->load(); 70 71 $sizes_array = array( 72 array( 73 'width' => 50, 74 'height' => 50, 75 ), 76 ); 77 78 $resized = $gd_image_editor->multi_resize( $sizes_array ); 79 80 # First, check to see if returned array is as expected 81 $expected_array = array( 82 array( 83 'file' => 'waffles-50x33.jpg', 84 'width' => 50, 85 'height' => 33, 86 'mime-type' => 'image/jpeg', 87 ), 88 ); 89 90 $this->assertEquals( $expected_array, $resized ); 91 92 // Now, verify real dimensions are as expected 93 $image_path = DIR_TESTDATA . '/images/'. $resized[0]['file']; 94 $this->assertImageDimensions( 95 $image_path, 96 $expected_array[0]['width'], 97 $expected_array[0]['height'] 98 ); 99 } 100 101 /** 102 * Ensure multi_resize doesn't create an image when 103 * both height and weight are missing, null, or 0. 104 * 105 * ticket 26823 106 */ 107 public function test_multi_resize_does_not_create() { 108 $file = DIR_TESTDATA . '/images/waffles.jpg'; 109 110 $gd_image_editor = new WP_Image_Editor_GD( $file ); 111 $gd_image_editor->load(); 112 113 $sizes_array = array( 114 array( 115 'width' => 0, 116 'height' => 0, 117 ), 118 array( 119 'width' => 0, 120 'height' => 0, 121 'crop' => true, 122 ), 123 array( 124 'width' => null, 125 'height' => null, 126 ), 127 array( 128 'width' => null, 129 'height' => null, 130 'crop' => true, 131 ), 132 array( 133 'width' => '', 134 'height' => '', 135 ), 136 array( 137 'width' => '', 138 'height' => '', 139 'crop' => true, 140 ), 141 array( 142 'width' => 0, 143 ), 144 array( 145 'width' => 0, 146 'crop' => true, 147 ), 148 array( 149 'width' => null, 150 ), 151 array( 152 'width' => null, 153 'crop' => true, 154 ), 155 array( 156 'width' => '', 157 ), 158 array( 159 'width' => '', 160 'crop' => true, 161 ), 162 ); 163 164 $resized = $gd_image_editor->multi_resize( $sizes_array ); 165 166 // If no images are generated, the returned array is empty. 167 $this->assertEmpty( $resized ); 168 } 169 170 /** 171 * Test multi_resize with multiple sizes 172 * 173 * ticket 26823 174 */ 175 public function test_multi_resize() { 176 $file = DIR_TESTDATA . '/images/waffles.jpg'; 177 178 $gd_image_editor = new WP_Image_Editor_GD( $file ); 179 $gd_image_editor->load(); 180 181 $sizes_array = array( 182 183 /** 184 * #0 - 10x10 resize, no cropping. 185 * By aspect, should be 10x6 output. 186 */ 187 array( 188 'width' => 10, 189 'height' => 10, 190 'crop' => false, 191 ), 192 193 /** 194 * #1 - 75x50 resize, with cropping. 195 * Output dimensions should be 75x50 196 */ 197 array( 198 'width' => 75, 199 'height' => 50, 200 'crop' => true, 201 ), 202 203 /** 204 * #2 - 20 pixel max height, no cropping. 205 * By aspect, should be 30x20 output. 206 */ 207 array( 208 'width' => 9999, # Arbitrary High Value 209 'height' => 20, 210 'crop' => false, 211 ), 212 213 /** 214 * #3 - 45 pixel max height, with cropping. 215 * By aspect, should be 45x400 output. 216 */ 217 array( 218 'width' => 45, 219 'height' => 9999, # Arbitrary High Value 220 'crop' => true, 221 ), 222 223 /** 224 * #4 - 50 pixel max width, no cropping. 225 * By aspect, should be 50x33 output. 226 */ 227 array( 228 'width' => 50, 229 ), 230 231 /** 232 * #5 - 55 pixel max width, no cropping, null height 233 * By aspect, should be 55x36 output. 234 */ 235 array( 236 'width' => 55, 237 'height' => null, 238 ), 239 240 /** 241 * #6 - 55 pixel max height, no cropping, no width specified. 242 * By aspect, should be 82x55 output. 243 */ 244 array( 245 'height' => 55, 246 ), 247 248 /** 249 * #7 - 60 pixel max height, no cropping, null width. 250 * By aspect, should be 90x60 output. 251 */ 252 array( 253 'width' => null, 254 'height' => 60, 255 ), 256 257 /** 258 * #8 - 70 pixel max height, no cropping, negative width. 259 * By aspect, should be 105x70 output. 260 */ 261 array( 262 'width' => -9999, # Arbitrary Negative Value 263 'height' => 70, 264 ), 265 266 /** 267 * #9 - 200 pixel max width, no cropping, negative height. 268 * By aspect, should be 200x133 output. 269 */ 270 array( 271 'width' => 200, 272 'height' => -9999, # Arbitrary Negative Value 273 ), 274 ); 275 276 $resized = $gd_image_editor->multi_resize( $sizes_array ); 277 278 $expected_array = array( 279 280 // #0 281 array( 282 'file' => 'waffles-10x6.jpg', 283 'width' => 10, 284 'height' => 6, 285 'mime-type' => 'image/jpeg', 286 ), 287 288 // #1 289 array( 290 'file' => 'waffles-75x50.jpg', 291 'width' => 75, 292 'height' => 50, 293 'mime-type' => 'image/jpeg', 294 ), 295 296 // #2 297 array( 298 'file' => 'waffles-30x20.jpg', 299 'width' => 30, 300 'height' => 20, 301 'mime-type' => 'image/jpeg', 302 ), 303 304 // #3 305 array( 306 'file' => 'waffles-45x400.jpg', 307 'width' => 45, 308 'height' => 400, 309 'mime-type' => 'image/jpeg', 310 ), 311 312 // #4 313 array( 314 'file' => 'waffles-50x33.jpg', 315 'width' => 50, 316 'height' => 33, 317 'mime-type' => 'image/jpeg', 318 ), 319 320 // #5 321 array( 322 'file' => 'waffles-55x36.jpg', 323 'width' => 55, 324 'height' => 36, 325 'mime-type' => 'image/jpeg', 326 ), 327 328 // #6 329 array( 330 'file' => 'waffles-82x55.jpg', 331 'width' => 82, 332 'height' => 55, 333 'mime-type' => 'image/jpeg', 334 ), 335 336 // #7 337 array( 338 'file' => 'waffles-90x60.jpg', 339 'width' => 90, 340 'height' => 60, 341 'mime-type' => 'image/jpeg', 342 ), 343 344 // #8 345 array( 346 'file' => 'waffles-105x70.jpg', 347 'width' => 105, 348 'height' => 70, 349 'mime-type' => 'image/jpeg', 350 ), 351 352 // #9 353 array( 354 'file' => 'waffles-200x133.jpg', 355 'width' => 200, 356 'height' => 133, 357 'mime-type' => 'image/jpeg', 358 ), 359 ); 360 361 $this->assertNotNull( $resized ); 362 $this->assertEquals( $expected_array, $resized ); 363 364 foreach( $resized as $key => $image_data ){ 365 $image_path = DIR_TESTDATA . '/images/' . $image_data['file']; 366 367 // Now, verify real dimensions are as expected 368 $this->assertImageDimensions( 369 $image_path, 370 $expected_array[$key]['width'], 371 $expected_array[$key]['height'] 372 ); 373 } 374 } 375 376 /** 377 * Test resizing an image with cropping 378 */ 379 public function test_resize_and_crop() { 380 $file = DIR_TESTDATA . '/images/waffles.jpg'; 54 381 55 382 $gd_image_editor = new WP_Image_Editor_GD( $file ); 56 383 $gd_image_editor->load(); 57 384 58 385 $gd_image_editor->resize( 100, 50, true ); 59 386 60 $this->assertEquals( array( 'width' => 100, 'height' => 50 ), $gd_image_editor->get_size() ); 387 $this->assertEquals( 388 array( 389 'width' => 100, 390 'height' => 50, 391 ), 392 $gd_image_editor->get_size() 393 ); 61 394 } 62 395 63 396 /** 64 397 * Test cropping an image 65 398 */ 66 399 public function test_crop() { 67 68 400 $file = DIR_TESTDATA . '/images/gradient-square.jpg'; 69 401 70 402 $gd_image_editor = new WP_Image_Editor_GD( $file ); … … class Tests_Image_Editor_GD extends WP_Image_UnitTestCase { 72 404 73 405 $gd_image_editor->crop( 0, 0, 50, 50 ); 74 406 75 $this->assertEquals( array( 'width' => 50, 'height' => 50 ), $gd_image_editor->get_size() ); 76 407 $this->assertEquals( 408 array( 409 'width' => 50, 410 'height' => 50, 411 ), 412 $gd_image_editor->get_size() 413 ); 77 414 } 78 415 79 416 /** 80 417 * Test rotating an image 180 deg 81 418 */ 82 419 public function test_rotate() { 83 84 420 $file = DIR_TESTDATA . '/images/gradient-square.jpg'; 85 421 86 422 $gd_image_editor = new WP_Image_Editor_GD( $file ); … … class Tests_Image_Editor_GD extends WP_Image_UnitTestCase { 100 436 * Test flipping an image 101 437 */ 102 438 public function test_flip() { 103 104 439 $file = DIR_TESTDATA . '/images/gradient-square.jpg'; 105 440 106 441 $gd_image_editor = new WP_Image_Editor_GD( $file ); … … class Tests_Image_Editor_GD extends WP_Image_UnitTestCase { 117 452 } 118 453 119 454 /** 120 * Test the image created with WP_Image_Edi or_GD preserves alpha when resizing121 * 455 * Test the image created with WP_Image_Editor_GD preserves alpha when resizing 456 * 122 457 * @ticket 23039 123 458 */ 124 459 public function test_image_preserves_alpha_on_resize() { 125 126 460 $file = DIR_TESTDATA . '/images/transparent.png'; 127 461 128 462 $editor = wp_get_image_editor( $file ); 129 463 $editor->load(); 130 $editor->resize( 5,5);464 $editor->resize( 5, 5 ); 131 465 $save_to_file = tempnam( get_temp_dir(), '' ) . '.png'; 132 466 133 467 $editor->save( $save_to_file ); 134 468 135 469 $this->assertImageAlphaAtPoint( $save_to_file, array( 0,0 ), 127 ); 136 137 470 } 138 471 139 472 /** 140 * Test the image created with WP_Image_Edi or_GD preserves alpha with no resizing etc141 * 473 * Test the image created with WP_Image_Editor_GD preserves alpha with no resizing etc 474 * 142 475 * @ticket 23039 143 476 */ 144 477 public function test_image_preserves_alpha() { 145 146 478 $file = DIR_TESTDATA . '/images/transparent.png'; 147 479 148 480 $editor = wp_get_image_editor( $file ); 149 481 $editor->load(); 150 482 151 483 $save_to_file = tempnam( get_temp_dir(), '' ) . '.png'; 152 484 153 485 $editor->save( $save_to_file ); 154 486 155 487 $this->assertImageAlphaAtPoint( $save_to_file, array( 0,0 ), 127 ); 156 488 } 157 158 489 } -
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..ec0cd23 100644
class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase { 11 11 12 12 public $editor_engine = 'WP_Image_Editor_Imagick'; 13 13 14 public function set up() {14 public function setUp() { 15 15 require_once( ABSPATH . WPINC . '/class-wp-image-editor.php' ); 16 16 require_once( ABSPATH . WPINC . '/class-wp-image-editor-imagick.php' ); 17 17 18 $editor = new WP_Image_Editor_Imagick( null ); 18 parent::setUp(); 19 } 19 20 20 if ( ! $editor->test() )21 $this->markTestSkipped( 'Image Magick not available' );21 public function shutDown() { 22 $folder = DIR_TESTDATA . '/images/waffles-*.jpg'; 22 23 23 parent::setUp(); 24 foreach ( glob( $folder ) as $file ) { 25 unlink( $file ); 26 } 27 28 parent::shutDown(); 24 29 } 25 30 26 31 /** 27 * Check support for Image Magick compatible mime types. 28 * 32 * Check support for ImageMagick compatible mime types. 29 33 */ 30 34 public function test_supports_mime_type() { 31 32 35 $imagick_image_editor = new WP_Image_Editor_Imagick( null ); 33 36 34 37 $this->assertTrue( $imagick_image_editor->supports_mime_type( 'image/jpeg' ), 'Does not support image/jpeg' ); … … class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase { 38 41 39 42 /** 40 43 * Test resizing an image, not using crop 41 *42 44 */ 43 45 public function test_resize() { 44 45 $file = DIR_TESTDATA . '/images/gradient-square.jpg'; 46 $file = DIR_TESTDATA . '/images/waffles.jpg'; 46 47 47 48 $imagick_image_editor = new WP_Image_Editor_Imagick( $file ); 48 49 $imagick_image_editor->load(); 49 50 50 51 $imagick_image_editor->resize( 100, 50 ); 51 52 52 $this->assertEquals( array( 'width' => 50, 'height' => 50 ), $imagick_image_editor->get_size() ); 53 $this->assertEquals( 54 array( 55 'width' => 75, 56 'height' => 50, 57 ), 58 $imagick_image_editor->get_size() 59 ); 53 60 } 54 61 55 62 /** 56 * Test resizing an image including cropping 57 * 63 * Test multi_resize with single image resize and no crop 58 64 */ 59 public function test_resize_and_crop() { 65 public function test_single_multi_resize() { 66 $file = DIR_TESTDATA . '/images/waffles.jpg'; 60 67 61 $file = DIR_TESTDATA . '/images/gradient-square.jpg'; 68 $imagick_image_editor = new WP_Image_Editor_Imagick( $file ); 69 $imagick_image_editor->load(); 70 71 $sizes_array = array( 72 array( 73 'width' => 50, 74 'height' => 50, 75 ), 76 ); 77 78 $resized = $imagick_image_editor->multi_resize( $sizes_array ); 79 80 # First, check to see if returned array is as expected 81 $expected_array = array( 82 array( 83 'file' => 'waffles-50x33.jpg', 84 'width' => 50, 85 'height' => 33, 86 'mime-type' => 'image/jpeg', 87 ), 88 ); 89 90 $this->assertEquals( $expected_array, $resized ); 91 92 // Now, verify real dimensions are as expected 93 $image_path = DIR_TESTDATA . '/images/'. $resized[0]['file']; 94 $this->assertImageDimensions( 95 $image_path, 96 $expected_array[0]['width'], 97 $expected_array[0]['height'] 98 ); 99 } 100 101 /** 102 * Ensure multi_resize doesn't create an image when 103 * both height and weight are missing, null, or 0. 104 * 105 * ticket 26823 106 */ 107 public function test_multi_resize_does_not_create() { 108 $file = DIR_TESTDATA . '/images/waffles.jpg'; 109 110 $imagick_image_editor = new WP_Image_Editor_Imagick( $file ); 111 $imagick_image_editor->load(); 112 113 $sizes_array = array( 114 array( 115 'width' => 0, 116 'height' => 0, 117 ), 118 array( 119 'width' => 0, 120 'height' => 0, 121 'crop' => true, 122 ), 123 array( 124 'width' => null, 125 'height' => null, 126 ), 127 array( 128 'width' => null, 129 'height' => null, 130 'crop' => true, 131 ), 132 array( 133 'width' => '', 134 'height' => '', 135 ), 136 array( 137 'width' => '', 138 'height' => '', 139 'crop' => true, 140 ), 141 array( 142 'width' => 0, 143 ), 144 array( 145 'width' => 0, 146 'crop' => true, 147 ), 148 array( 149 'width' => null, 150 ), 151 array( 152 'width' => null, 153 'crop' => true, 154 ), 155 array( 156 'width' => '', 157 ), 158 array( 159 'width' => '', 160 'crop' => true, 161 ), 162 ); 163 164 $resized = $imagick_image_editor->multi_resize( $sizes_array ); 165 166 // If no images are generated, the returned array is empty. 167 $this->assertEmpty( $resized ); 168 } 169 170 /** 171 * Test multi_resize with multiple sizes 172 * 173 * ticket 26823 174 */ 175 public function test_multi_resize() { 176 $file = DIR_TESTDATA . '/images/waffles.jpg'; 177 178 $imagick_image_editor = new WP_Image_Editor_Imagick( $file ); 179 $imagick_image_editor->load(); 180 181 $sizes_array = array( 182 183 /** 184 * #0 - 10x10 resize, no cropping. 185 * By aspect, should be 10x6 output. 186 */ 187 array( 188 'width' => 10, 189 'height' => 10, 190 'crop' => false, 191 ), 192 193 /** 194 * #1 - 75x50 resize, with cropping. 195 * Output dimensions should be 75x50 196 */ 197 array( 198 'width' => 75, 199 'height' => 50, 200 'crop' => true, 201 ), 202 203 /** 204 * #2 - 20 pixel max height, no cropping. 205 * By aspect, should be 30x20 output. 206 */ 207 array( 208 'width' => 9999, # Arbitrary High Value 209 'height' => 20, 210 'crop' => false, 211 ), 212 213 /** 214 * #3 - 45 pixel max height, with cropping. 215 * By aspect, should be 45x400 output. 216 */ 217 array( 218 'width' => 45, 219 'height' => 9999, # Arbitrary High Value 220 'crop' => true, 221 ), 222 223 /** 224 * #4 - 50 pixel max width, no cropping. 225 * By aspect, should be 50x33 output. 226 */ 227 array( 228 'width' => 50, 229 ), 230 231 /** 232 * #5 - 55 pixel max width, no cropping, null height 233 * By aspect, should be 55x36 output. 234 */ 235 array( 236 'width' => 55, 237 'height' => null, 238 ), 239 240 /** 241 * #6 - 55 pixel max height, no cropping, no width specified. 242 * By aspect, should be 82x55 output. 243 */ 244 array( 245 'height' => 55, 246 ), 247 248 /** 249 * #7 - 60 pixel max height, no cropping, null width. 250 * By aspect, should be 90x60 output. 251 */ 252 array( 253 'width' => null, 254 'height' => 60, 255 ), 256 257 /** 258 * #8 - 70 pixel max height, no cropping, negative width. 259 * By aspect, should be 105x70 output. 260 */ 261 array( 262 'width' => -9999, # Arbitrary Negative Value 263 'height' => 70, 264 ), 265 266 /** 267 * #9 - 200 pixel max width, no cropping, negative height. 268 * By aspect, should be 200x133 output. 269 */ 270 array( 271 'width' => 200, 272 'height' => -9999, # Arbitrary Negative Value 273 ), 274 ); 275 276 $resized = $imagick_image_editor->multi_resize( $sizes_array ); 277 278 $expected_array = array( 279 280 // #0 281 array( 282 'file' => 'waffles-10x6.jpg', 283 'width' => 10, 284 'height' => 6, 285 'mime-type' => 'image/jpeg', 286 ), 287 288 // #1 289 array( 290 'file' => 'waffles-75x50.jpg', 291 'width' => 75, 292 'height' => 50, 293 'mime-type' => 'image/jpeg', 294 ), 295 296 // #2 297 array( 298 'file' => 'waffles-30x20.jpg', 299 'width' => 30, 300 'height' => 20, 301 'mime-type' => 'image/jpeg', 302 ), 303 304 // #3 305 array( 306 'file' => 'waffles-45x400.jpg', 307 'width' => 45, 308 'height' => 400, 309 'mime-type' => 'image/jpeg', 310 ), 311 312 // #4 313 array( 314 'file' => 'waffles-50x33.jpg', 315 'width' => 50, 316 'height' => 33, 317 'mime-type' => 'image/jpeg', 318 ), 319 320 // #5 321 array( 322 'file' => 'waffles-55x36.jpg', 323 'width' => 55, 324 'height' => 36, 325 'mime-type' => 'image/jpeg', 326 ), 327 328 // #6 329 array( 330 'file' => 'waffles-82x55.jpg', 331 'width' => 82, 332 'height' => 55, 333 'mime-type' => 'image/jpeg', 334 ), 335 336 // #7 337 array( 338 'file' => 'waffles-90x60.jpg', 339 'width' => 90, 340 'height' => 60, 341 'mime-type' => 'image/jpeg', 342 ), 343 344 // #8 345 array( 346 'file' => 'waffles-105x70.jpg', 347 'width' => 105, 348 'height' => 70, 349 'mime-type' => 'image/jpeg', 350 ), 351 352 // #9 353 array( 354 'file' => 'waffles-200x133.jpg', 355 'width' => 200, 356 'height' => 133, 357 'mime-type' => 'image/jpeg', 358 ), 359 ); 360 361 $this->assertNotNull( $resized ); 362 $this->assertEquals( $expected_array, $resized ); 363 364 foreach( $resized as $key => $image_data ){ 365 $image_path = DIR_TESTDATA . '/images/' . $image_data['file']; 366 367 // Now, verify real dimensions are as expected 368 $this->assertImageDimensions( 369 $image_path, 370 $expected_array[$key]['width'], 371 $expected_array[$key]['height'] 372 ); 373 } 374 } 375 376 /** 377 * Test resizing an image with cropping 378 */ 379 public function test_resize_and_crop() { 380 $file = DIR_TESTDATA . '/images/waffles.jpg'; 62 381 63 382 $imagick_image_editor = new WP_Image_Editor_Imagick( $file ); 64 383 $imagick_image_editor->load(); 65 384 66 385 $imagick_image_editor->resize( 100, 50, true ); 67 386 68 $this->assertEquals( array( 'width' => 100, 'height' => 50 ), $imagick_image_editor->get_size() ); 387 $this->assertEquals( 388 array( 389 'width' => 100, 390 'height' => 50, 391 ), 392 $imagick_image_editor->get_size() 393 ); 69 394 } 70 395 71 396 /** 72 397 * Test cropping an image 73 398 */ 74 399 public function test_crop() { 75 76 400 $file = DIR_TESTDATA . '/images/gradient-square.jpg'; 77 401 78 402 $imagick_image_editor = new WP_Image_Editor_Imagick( $file ); … … class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase { 80 404 81 405 $imagick_image_editor->crop( 0, 0, 50, 50 ); 82 406 83 $this->assertEquals( array( 'width' => 50, 'height' => 50 ), $imagick_image_editor->get_size() ); 84 407 $this->assertEquals( 408 array( 409 'width' => 50, 410 'height' => 50, 411 ), 412 $imagick_image_editor->get_size() 413 ); 85 414 } 86 415 87 416 /** 88 417 * Test rotating an image 180 deg 89 418 */ 90 419 public function test_rotate() { 91 92 420 $file = DIR_TESTDATA . '/images/gradient-square.jpg'; 93 421 94 422 $imagick_image_editor = new WP_Image_Editor_Imagick( $file ); … … class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase { 108 436 * Test flipping an image 109 437 */ 110 438 public function test_flip() { 111 112 439 $file = DIR_TESTDATA . '/images/gradient-square.jpg'; 113 440 114 441 $imagick_image_editor = new WP_Image_Editor_Imagick( $file ); … … class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase { 125 452 } 126 453 127 454 /** 128 * Test the image created with WP_Image_Edi or_Imagick preserves alpha when resizing455 * Test the image created with WP_Image_Editor_Imagick preserves alpha when resizing 129 456 * 130 457 * @ticket 24871 131 458 */ 132 459 public function test_image_preserves_alpha_on_resize() { 133 134 460 $file = DIR_TESTDATA . '/images/transparent.png'; 135 461 136 462 $editor = wp_get_image_editor( $file ); 137 463 $editor->load(); 138 $editor->resize( 5,5);464 $editor->resize( 5, 5 ); 139 465 $save_to_file = tempnam( get_temp_dir(), '' ) . '.png'; 140 466 141 467 $editor->save( $save_to_file ); 142 468 143 469 $this->assertImageAlphaAtPoint( $save_to_file, array( 0,0 ), 127 ); 144 145 470 } 146 471 147 472 /** 148 * Test the image created with WP_Image_Edi or_Imagick preserves alpha with no resizing etc473 * Test the image created with WP_Image_Editor_Imagick preserves alpha with no resizing etc 149 474 * 150 475 * @ticket 24871 151 476 */ 152 477 public function test_image_preserves_alpha() { 153 154 478 $file = DIR_TESTDATA . '/images/transparent.png'; 155 479 156 480 $editor = wp_get_image_editor( $file ); 157 481 $editor->load(); 158 482 159 483 $save_to_file = tempnam( get_temp_dir(), '' ) . '.png'; 160 484 161 485 $editor->save( $save_to_file ); 162 486 163 487 $this->assertImageAlphaAtPoint( $save_to_file, array( 0,0 ), 127 );