diff --git a/src/wp-includes/class-wp-image-editor-gd.php b/src/wp-includes/class-wp-image-editor-gd.php index fdd5586866..303f5004c0 100644 --- a/src/wp-includes/class-wp-image-editor-gd.php +++ b/src/wp-includes/class-wp-image-editor-gd.php @@ -140,12 +140,16 @@ class WP_Image_Editor_GD extends WP_Image_Editor { * Resizes current image. * Wraps _resize, since _resize returns a GD Resource. * + * At minimum, either a height or width must be provided. + * If one of the two is set to null, the resize will + * maintain aspect ratio according to the provided dimension. + * * @since 3.5.0 * @access public * - * @param int $max_w - * @param int $max_h - * @param boolean $crop + * @param int|null $max_w Image width. + * @param int|null $max_h Image height. + * @param boolean $crop * @return boolean|WP_Error */ public function resize( $max_w, $max_h, $crop = false ) { @@ -192,24 +196,37 @@ class WP_Image_Editor_GD extends WP_Image_Editor { * @param array $sizes { * An array of image size arrays. Default sizes are 'small', 'medium', 'large'. * + * Either a height or width must be provided. + * If one of the two is set to null, the resize will + * maintain aspect ratio according to the provided dimension. + * * @type array $size { - * @type int $width Image width. - * @type int $height Image height. - * @type bool $crop Optional. Whether to crop the image. Default false. + * @type int ['width'] Optional. Image width. + * @type int ['height'] Optional. Image height. + * @type bool ['crop'] Optional. Whether to crop the image. Default false. * } * } - * @return array An array of resized images metadata by size. + * @return array An array of resized images' metadata by size. */ public function multi_resize( $sizes ) { $metadata = array(); $orig_size = $this->size; foreach ( $sizes as $size => $size_data ) { - if ( ! ( isset( $size_data['width'] ) && isset( $size_data['height'] ) ) ) + if ( ! isset( $size_data['width'] ) && ! isset( $size_data['height'] ) ) { continue; + } - if ( ! isset( $size_data['crop'] ) ) + if ( ! isset( $size_data['width'] ) ) { + $size_data['width'] = null; + } + if ( ! isset( $size_data['height'] ) ) { + $size_data['height'] = null; + } + + if ( ! isset( $size_data['crop'] ) ) { $size_data['crop'] = false; + } $image = $this->_resize( $size_data['width'], $size_data['height'], $size_data['crop'] ); diff --git a/src/wp-includes/class-wp-image-editor-imagick.php b/src/wp-includes/class-wp-image-editor-imagick.php index f3c9451ec3..4ebed4bae7 100644 --- a/src/wp-includes/class-wp-image-editor-imagick.php +++ b/src/wp-includes/class-wp-image-editor-imagick.php @@ -211,12 +211,16 @@ class WP_Image_Editor_Imagick extends WP_Image_Editor { /** * Resizes current image. * + * At minimum, either a height or width must be provided. + * If one of the two is set to null, the resize will + * maintain aspect ratio according to the provided dimension. + * * @since 3.5.0 * @access public * - * @param int $max_w - * @param int $max_h - * @param boolean $crop + * @param int|null $max_w Image width. + * @param int|null $max_h Image height. + * @param boolean $crop * @return boolean|WP_Error */ public function resize( $max_w, $max_h, $crop = false ) { @@ -255,13 +259,17 @@ class WP_Image_Editor_Imagick extends WP_Image_Editor { * @param array $sizes { * An array of image size arrays. Default sizes are 'small', 'medium', 'large'. * + * Either a height or width must be provided. + * If one of the two is set to null, the resize will + * maintain aspect ratio according to the provided dimension. + * * @type array $size { - * @type int $width Image width. - * @type int $height Image height. + * @type int ['width'] Optional. Image width. + * @type int ['height'] Optional. Image height. * @type bool $crop Optional. Whether to crop the image. Default false. * } * } - * @return array An array of resized images metadata by size. + * @return array An array of resized images' metadata by size. */ public function multi_resize( $sizes ) { $metadata = array(); @@ -272,11 +280,20 @@ class WP_Image_Editor_Imagick extends WP_Image_Editor { if ( ! $this->image ) $this->image = $orig_image->getImage(); - if ( ! ( isset( $size_data['width'] ) && isset( $size_data['height'] ) ) ) + if ( ! isset( $size_data['width'] ) && ! isset( $size_data['height'] ) ) { continue; + } - if ( ! isset( $size_data['crop'] ) ) + if ( ! isset( $size_data['width'] ) ) { + $size_data['width'] = null; + } + if ( ! isset( $size_data['height'] ) ) { + $size_data['height'] = null; + } + + if ( ! isset( $size_data['crop'] ) ) { $size_data['crop'] = false; + } $resize_result = $this->resize( $size_data['width'], $size_data['height'], $size_data['crop'] ); diff --git a/src/wp-includes/class-wp-image-editor.php b/src/wp-includes/class-wp-image-editor.php index 7f6488f91f..5affba48c7 100644 --- a/src/wp-includes/class-wp-image-editor.php +++ b/src/wp-includes/class-wp-image-editor.php @@ -82,13 +82,17 @@ abstract class WP_Image_Editor { /** * Resizes current image. * + * At minimum, either a height or width must be provided. + * If one of the two is set to null, the resize will + * maintain aspect ratio according to the provided dimension. + * * @since 3.5.0 * @access public * @abstract * - * @param int $max_w - * @param int $max_h - * @param boolean $crop + * @param int|null $max_w Image width. + * @param int|null $max_h Image height. + * @param boolean $crop * @return boolean|WP_Error */ abstract public function resize( $max_w, $max_h, $crop = false ); diff --git a/tests/phpunit/tests/image/base.php b/tests/phpunit/tests/image/base.php index 26f3b59d9a..be9ab67350 100644 --- a/tests/phpunit/tests/image/base.php +++ b/tests/phpunit/tests/image/base.php @@ -39,12 +39,35 @@ abstract class WP_Image_UnitTestCase extends WP_UnitTestCase { * @param int $alpha */ protected function assertImageAlphaAtPoint( $image_path, $point, $alpha ) { - $im = imagecreatefrompng( $image_path ); - $rgb = imagecolorat($im, $point[0], $point[1]); + $rgb = imagecolorat( $im, $point[0], $point[1] ); - $colors = imagecolorsforindex($im, $rgb); + $colors = imagecolorsforindex( $im, $rgb ); $this->assertEquals( $alpha, $colors['alpha'] ); } + + /** + * Helper assertion to check actual image dimensions on disk + * + * @param string $filename Image filename. + * @param int $width Width to verify. + * @param int $height Height to verify. + */ + protected function assertImageDimensions( $filename, $width, $height ) { + $detected_width = 0; + $detected_height = 0; + $image_size = @getimagesize( $filename ); + + if ( isset( $image_size[0] ) ) { + $detected_width = $image_size[0]; + } + + if ( isset( $image_size[1] ) ) { + $detected_height = $image_size[1]; + } + + $this->assertEquals( $width, $detected_width ); + $this->assertEquals( $height, $detected_height ); + } } diff --git a/tests/phpunit/tests/image/editor_gd.php b/tests/phpunit/tests/image/editor_gd.php index f5fdd99651..ca134bdb39 100644 --- a/tests/phpunit/tests/image/editor_gd.php +++ b/tests/phpunit/tests/image/editor_gd.php @@ -8,17 +8,28 @@ */ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase { + public $editor_engine = 'WP_Image_Editor_GD'; - public function setup() { + public function setUp() { require_once( ABSPATH . WPINC . '/class-wp-image-editor.php' ); require_once( ABSPATH . WPINC . '/class-wp-image-editor-gd.php' ); + parent::setUp(); } + public function shutDown() { + $folder = DIR_TESTDATA . '/images/waffles-*.jpg'; + + foreach ( glob( $folder ) as $file ) { + unlink( $file ); + } + + parent::shutDown(); + } + /** * Check support for GD compatible mime types. - * */ public function test_supports_mime_type() { $gd_image_editor = new WP_Image_Editor_GD( null ); @@ -30,41 +41,362 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase { /** * Test resizing an image, not using crop - * */ public function test_resize() { - - $file = DIR_TESTDATA . '/images/gradient-square.jpg'; + $file = DIR_TESTDATA . '/images/waffles.jpg'; $gd_image_editor = new WP_Image_Editor_GD( $file ); $gd_image_editor->load(); $gd_image_editor->resize( 100, 50 ); - $this->assertEquals( array( 'width' => 50, 'height' => 50 ), $gd_image_editor->get_size() ); + $this->assertEquals( + array( + 'width' => 75, + 'height' => 50, + ), + $gd_image_editor->get_size() + ); } /** - * Test resizing an image including cropping - * + * Test multi_resize with single image resize and no crop + */ + public function test_single_multi_resize() { + $file = DIR_TESTDATA . '/images/waffles.jpg'; + + $gd_image_editor = new WP_Image_Editor_GD( $file ); + $gd_image_editor->load(); + + $sizes_array = array( + array( + 'width' => 50, + 'height' => 50, + ), + ); + + $resized = $gd_image_editor->multi_resize( $sizes_array ); + + # First, check to see if returned array is as expected + $expected_array = array( + array( + 'file' => 'waffles-50x33.jpg', + 'width' => 50, + 'height' => 33, + 'mime-type' => 'image/jpeg', + ), + ); + + $this->assertEquals( $expected_array, $resized ); + + // Now, verify real dimensions are as expected + $image_path = DIR_TESTDATA . '/images/'. $resized[0]['file']; + $this->assertImageDimensions( + $image_path, + $expected_array[0]['width'], + $expected_array[0]['height'] + ); + } + + /** + * Ensure multi_resize doesn't create an image when + * both height and weight are missing, null, or 0. + * + * ticket 26823 + */ + public function test_multi_resize_does_not_create() { + $file = DIR_TESTDATA . '/images/waffles.jpg'; + + $gd_image_editor = new WP_Image_Editor_GD( $file ); + $gd_image_editor->load(); + + $sizes_array = array( + array( + 'width' => 0, + 'height' => 0, + ), + array( + 'width' => 0, + 'height' => 0, + 'crop' => true, + ), + array( + 'width' => null, + 'height' => null, + ), + array( + 'width' => null, + 'height' => null, + 'crop' => true, + ), + array( + 'width' => '', + 'height' => '', + ), + array( + 'width' => '', + 'height' => '', + 'crop' => true, + ), + array( + 'width' => 0, + ), + array( + 'width' => 0, + 'crop' => true, + ), + array( + 'width' => null, + ), + array( + 'width' => null, + 'crop' => true, + ), + array( + 'width' => '', + ), + array( + 'width' => '', + 'crop' => true, + ), + ); + + $resized = $gd_image_editor->multi_resize( $sizes_array ); + + // If no images are generated, the returned array is empty. + $this->assertEmpty( $resized ); + } + + /** + * Test multi_resize with multiple sizes + * + * ticket 26823 + */ + public function test_multi_resize() { + $file = DIR_TESTDATA . '/images/waffles.jpg'; + + $gd_image_editor = new WP_Image_Editor_GD( $file ); + $gd_image_editor->load(); + + $sizes_array = array( + + /** + * #0 - 10x10 resize, no cropping. + * By aspect, should be 10x6 output. + */ + array( + 'width' => 10, + 'height' => 10, + 'crop' => false, + ), + + /** + * #1 - 75x50 resize, with cropping. + * Output dimensions should be 75x50 + */ + array( + 'width' => 75, + 'height' => 50, + 'crop' => true, + ), + + /** + * #2 - 20 pixel max height, no cropping. + * By aspect, should be 30x20 output. + */ + array( + 'width' => 9999, # Arbitrary High Value + 'height' => 20, + 'crop' => false, + ), + + /** + * #3 - 45 pixel max height, with cropping. + * By aspect, should be 45x400 output. + */ + array( + 'width' => 45, + 'height' => 9999, # Arbitrary High Value + 'crop' => true, + ), + + /** + * #4 - 50 pixel max width, no cropping. + * By aspect, should be 50x33 output. + */ + array( + 'width' => 50, + ), + + /** + * #5 - 55 pixel max width, no cropping, null height + * By aspect, should be 55x36 output. + */ + array( + 'width' => 55, + 'height' => null, + ), + + /** + * #6 - 55 pixel max height, no cropping, no width specified. + * By aspect, should be 82x55 output. + */ + array( + 'height' => 55, + ), + + /** + * #7 - 60 pixel max height, no cropping, null width. + * By aspect, should be 90x60 output. + */ + array( + 'width' => null, + 'height' => 60, + ), + + /** + * #8 - 70 pixel max height, no cropping, negative width. + * By aspect, should be 105x70 output. + */ + array( + 'width' => -9999, # Arbitrary Negative Value + 'height' => 70, + ), + + /** + * #9 - 200 pixel max width, no cropping, negative height. + * By aspect, should be 200x133 output. + */ + array( + 'width' => 200, + 'height' => -9999, # Arbitrary Negative Value + ), + ); + + $resized = $gd_image_editor->multi_resize( $sizes_array ); + + $expected_array = array( + + // #0 + array( + 'file' => 'waffles-10x6.jpg', + 'width' => 10, + 'height' => 6, + 'mime-type' => 'image/jpeg', + ), + + // #1 + array( + 'file' => 'waffles-75x50.jpg', + 'width' => 75, + 'height' => 50, + 'mime-type' => 'image/jpeg', + ), + + // #2 + array( + 'file' => 'waffles-30x20.jpg', + 'width' => 30, + 'height' => 20, + 'mime-type' => 'image/jpeg', + ), + + // #3 + array( + 'file' => 'waffles-45x400.jpg', + 'width' => 45, + 'height' => 400, + 'mime-type' => 'image/jpeg', + ), + + // #4 + array( + 'file' => 'waffles-50x33.jpg', + 'width' => 50, + 'height' => 33, + 'mime-type' => 'image/jpeg', + ), + + // #5 + array( + 'file' => 'waffles-55x36.jpg', + 'width' => 55, + 'height' => 36, + 'mime-type' => 'image/jpeg', + ), + + // #6 + array( + 'file' => 'waffles-82x55.jpg', + 'width' => 82, + 'height' => 55, + 'mime-type' => 'image/jpeg', + ), + + // #7 + array( + 'file' => 'waffles-90x60.jpg', + 'width' => 90, + 'height' => 60, + 'mime-type' => 'image/jpeg', + ), + + // #8 + array( + 'file' => 'waffles-105x70.jpg', + 'width' => 105, + 'height' => 70, + 'mime-type' => 'image/jpeg', + ), + + // #9 + array( + 'file' => 'waffles-200x133.jpg', + 'width' => 200, + 'height' => 133, + 'mime-type' => 'image/jpeg', + ), + ); + + $this->assertNotNull( $resized ); + $this->assertEquals( $expected_array, $resized ); + + foreach( $resized as $key => $image_data ){ + $image_path = DIR_TESTDATA . '/images/' . $image_data['file']; + + // Now, verify real dimensions are as expected + $this->assertImageDimensions( + $image_path, + $expected_array[$key]['width'], + $expected_array[$key]['height'] + ); + } + } + + /** + * Test resizing an image with cropping */ public function test_resize_and_crop() { - - $file = DIR_TESTDATA . '/images/gradient-square.jpg'; + $file = DIR_TESTDATA . '/images/waffles.jpg'; $gd_image_editor = new WP_Image_Editor_GD( $file ); $gd_image_editor->load(); $gd_image_editor->resize( 100, 50, true ); - $this->assertEquals( array( 'width' => 100, 'height' => 50 ), $gd_image_editor->get_size() ); + $this->assertEquals( + array( + 'width' => 100, + 'height' => 50, + ), + $gd_image_editor->get_size() + ); } /** * Test cropping an image */ public function test_crop() { - $file = DIR_TESTDATA . '/images/gradient-square.jpg'; $gd_image_editor = new WP_Image_Editor_GD( $file ); @@ -72,15 +404,19 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase { $gd_image_editor->crop( 0, 0, 50, 50 ); - $this->assertEquals( array( 'width' => 50, 'height' => 50 ), $gd_image_editor->get_size() ); - + $this->assertEquals( + array( + 'width' => 50, + 'height' => 50, + ), + $gd_image_editor->get_size() + ); } /** * Test rotating an image 180 deg */ public function test_rotate() { - $file = DIR_TESTDATA . '/images/gradient-square.jpg'; $gd_image_editor = new WP_Image_Editor_GD( $file ); @@ -100,7 +436,6 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase { * Test flipping an image */ public function test_flip() { - $file = DIR_TESTDATA . '/images/gradient-square.jpg'; $gd_image_editor = new WP_Image_Editor_GD( $file ); @@ -117,42 +452,38 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase { } /** - * Test the image created with WP_Image_Edior_GD preserves alpha when resizing - * + * Test the image created with WP_Image_Editor_GD preserves alpha when resizing + * * @ticket 23039 */ public function test_image_preserves_alpha_on_resize() { - $file = DIR_TESTDATA . '/images/transparent.png'; $editor = wp_get_image_editor( $file ); $editor->load(); - $editor->resize(5,5); + $editor->resize( 5, 5 ); $save_to_file = tempnam( get_temp_dir(), '' ) . '.png'; - + $editor->save( $save_to_file ); $this->assertImageAlphaAtPoint( $save_to_file, array( 0,0 ), 127 ); - } - + /** - * Test the image created with WP_Image_Edior_GD preserves alpha with no resizing etc - * + * Test the image created with WP_Image_Editor_GD preserves alpha with no resizing etc + * * @ticket 23039 */ public function test_image_preserves_alpha() { - $file = DIR_TESTDATA . '/images/transparent.png'; $editor = wp_get_image_editor( $file ); $editor->load(); $save_to_file = tempnam( get_temp_dir(), '' ) . '.png'; - + $editor->save( $save_to_file ); $this->assertImageAlphaAtPoint( $save_to_file, array( 0,0 ), 127 ); } - } diff --git a/tests/phpunit/tests/image/editor_imagick.php b/tests/phpunit/tests/image/editor_imagick.php index 1e43298a54..ec0cd23dec 100644 --- a/tests/phpunit/tests/image/editor_imagick.php +++ b/tests/phpunit/tests/image/editor_imagick.php @@ -11,24 +11,27 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase { public $editor_engine = 'WP_Image_Editor_Imagick'; - public function setup() { + public function setUp() { require_once( ABSPATH . WPINC . '/class-wp-image-editor.php' ); require_once( ABSPATH . WPINC . '/class-wp-image-editor-imagick.php' ); - $editor = new WP_Image_Editor_Imagick( null ); - - if ( ! $editor->test() ) - $this->markTestSkipped( 'Image Magick not available' ); - parent::setUp(); } + public function shutDown() { + $folder = DIR_TESTDATA . '/images/waffles-*.jpg'; + + foreach ( glob( $folder ) as $file ) { + unlink( $file ); + } + + parent::shutDown(); + } + /** - * Check support for Image Magick compatible mime types. - * + * Check support for ImageMagick compatible mime types. */ public function test_supports_mime_type() { - $imagick_image_editor = new WP_Image_Editor_Imagick( null ); $this->assertTrue( $imagick_image_editor->supports_mime_type( 'image/jpeg' ), 'Does not support image/jpeg' ); @@ -38,41 +41,362 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase { /** * Test resizing an image, not using crop - * */ public function test_resize() { - - $file = DIR_TESTDATA . '/images/gradient-square.jpg'; + $file = DIR_TESTDATA . '/images/waffles.jpg'; $imagick_image_editor = new WP_Image_Editor_Imagick( $file ); $imagick_image_editor->load(); $imagick_image_editor->resize( 100, 50 ); - $this->assertEquals( array( 'width' => 50, 'height' => 50 ), $imagick_image_editor->get_size() ); + $this->assertEquals( + array( + 'width' => 75, + 'height' => 50, + ), + $imagick_image_editor->get_size() + ); } /** - * Test resizing an image including cropping - * + * Test multi_resize with single image resize and no crop + */ + public function test_single_multi_resize() { + $file = DIR_TESTDATA . '/images/waffles.jpg'; + + $imagick_image_editor = new WP_Image_Editor_Imagick( $file ); + $imagick_image_editor->load(); + + $sizes_array = array( + array( + 'width' => 50, + 'height' => 50, + ), + ); + + $resized = $imagick_image_editor->multi_resize( $sizes_array ); + + # First, check to see if returned array is as expected + $expected_array = array( + array( + 'file' => 'waffles-50x33.jpg', + 'width' => 50, + 'height' => 33, + 'mime-type' => 'image/jpeg', + ), + ); + + $this->assertEquals( $expected_array, $resized ); + + // Now, verify real dimensions are as expected + $image_path = DIR_TESTDATA . '/images/'. $resized[0]['file']; + $this->assertImageDimensions( + $image_path, + $expected_array[0]['width'], + $expected_array[0]['height'] + ); + } + + /** + * Ensure multi_resize doesn't create an image when + * both height and weight are missing, null, or 0. + * + * ticket 26823 + */ + public function test_multi_resize_does_not_create() { + $file = DIR_TESTDATA . '/images/waffles.jpg'; + + $imagick_image_editor = new WP_Image_Editor_Imagick( $file ); + $imagick_image_editor->load(); + + $sizes_array = array( + array( + 'width' => 0, + 'height' => 0, + ), + array( + 'width' => 0, + 'height' => 0, + 'crop' => true, + ), + array( + 'width' => null, + 'height' => null, + ), + array( + 'width' => null, + 'height' => null, + 'crop' => true, + ), + array( + 'width' => '', + 'height' => '', + ), + array( + 'width' => '', + 'height' => '', + 'crop' => true, + ), + array( + 'width' => 0, + ), + array( + 'width' => 0, + 'crop' => true, + ), + array( + 'width' => null, + ), + array( + 'width' => null, + 'crop' => true, + ), + array( + 'width' => '', + ), + array( + 'width' => '', + 'crop' => true, + ), + ); + + $resized = $imagick_image_editor->multi_resize( $sizes_array ); + + // If no images are generated, the returned array is empty. + $this->assertEmpty( $resized ); + } + + /** + * Test multi_resize with multiple sizes + * + * ticket 26823 + */ + public function test_multi_resize() { + $file = DIR_TESTDATA . '/images/waffles.jpg'; + + $imagick_image_editor = new WP_Image_Editor_Imagick( $file ); + $imagick_image_editor->load(); + + $sizes_array = array( + + /** + * #0 - 10x10 resize, no cropping. + * By aspect, should be 10x6 output. + */ + array( + 'width' => 10, + 'height' => 10, + 'crop' => false, + ), + + /** + * #1 - 75x50 resize, with cropping. + * Output dimensions should be 75x50 + */ + array( + 'width' => 75, + 'height' => 50, + 'crop' => true, + ), + + /** + * #2 - 20 pixel max height, no cropping. + * By aspect, should be 30x20 output. + */ + array( + 'width' => 9999, # Arbitrary High Value + 'height' => 20, + 'crop' => false, + ), + + /** + * #3 - 45 pixel max height, with cropping. + * By aspect, should be 45x400 output. + */ + array( + 'width' => 45, + 'height' => 9999, # Arbitrary High Value + 'crop' => true, + ), + + /** + * #4 - 50 pixel max width, no cropping. + * By aspect, should be 50x33 output. + */ + array( + 'width' => 50, + ), + + /** + * #5 - 55 pixel max width, no cropping, null height + * By aspect, should be 55x36 output. + */ + array( + 'width' => 55, + 'height' => null, + ), + + /** + * #6 - 55 pixel max height, no cropping, no width specified. + * By aspect, should be 82x55 output. + */ + array( + 'height' => 55, + ), + + /** + * #7 - 60 pixel max height, no cropping, null width. + * By aspect, should be 90x60 output. + */ + array( + 'width' => null, + 'height' => 60, + ), + + /** + * #8 - 70 pixel max height, no cropping, negative width. + * By aspect, should be 105x70 output. + */ + array( + 'width' => -9999, # Arbitrary Negative Value + 'height' => 70, + ), + + /** + * #9 - 200 pixel max width, no cropping, negative height. + * By aspect, should be 200x133 output. + */ + array( + 'width' => 200, + 'height' => -9999, # Arbitrary Negative Value + ), + ); + + $resized = $imagick_image_editor->multi_resize( $sizes_array ); + + $expected_array = array( + + // #0 + array( + 'file' => 'waffles-10x6.jpg', + 'width' => 10, + 'height' => 6, + 'mime-type' => 'image/jpeg', + ), + + // #1 + array( + 'file' => 'waffles-75x50.jpg', + 'width' => 75, + 'height' => 50, + 'mime-type' => 'image/jpeg', + ), + + // #2 + array( + 'file' => 'waffles-30x20.jpg', + 'width' => 30, + 'height' => 20, + 'mime-type' => 'image/jpeg', + ), + + // #3 + array( + 'file' => 'waffles-45x400.jpg', + 'width' => 45, + 'height' => 400, + 'mime-type' => 'image/jpeg', + ), + + // #4 + array( + 'file' => 'waffles-50x33.jpg', + 'width' => 50, + 'height' => 33, + 'mime-type' => 'image/jpeg', + ), + + // #5 + array( + 'file' => 'waffles-55x36.jpg', + 'width' => 55, + 'height' => 36, + 'mime-type' => 'image/jpeg', + ), + + // #6 + array( + 'file' => 'waffles-82x55.jpg', + 'width' => 82, + 'height' => 55, + 'mime-type' => 'image/jpeg', + ), + + // #7 + array( + 'file' => 'waffles-90x60.jpg', + 'width' => 90, + 'height' => 60, + 'mime-type' => 'image/jpeg', + ), + + // #8 + array( + 'file' => 'waffles-105x70.jpg', + 'width' => 105, + 'height' => 70, + 'mime-type' => 'image/jpeg', + ), + + // #9 + array( + 'file' => 'waffles-200x133.jpg', + 'width' => 200, + 'height' => 133, + 'mime-type' => 'image/jpeg', + ), + ); + + $this->assertNotNull( $resized ); + $this->assertEquals( $expected_array, $resized ); + + foreach( $resized as $key => $image_data ){ + $image_path = DIR_TESTDATA . '/images/' . $image_data['file']; + + // Now, verify real dimensions are as expected + $this->assertImageDimensions( + $image_path, + $expected_array[$key]['width'], + $expected_array[$key]['height'] + ); + } + } + + /** + * Test resizing an image with cropping */ public function test_resize_and_crop() { - - $file = DIR_TESTDATA . '/images/gradient-square.jpg'; + $file = DIR_TESTDATA . '/images/waffles.jpg'; $imagick_image_editor = new WP_Image_Editor_Imagick( $file ); $imagick_image_editor->load(); $imagick_image_editor->resize( 100, 50, true ); - $this->assertEquals( array( 'width' => 100, 'height' => 50 ), $imagick_image_editor->get_size() ); + $this->assertEquals( + array( + 'width' => 100, + 'height' => 50, + ), + $imagick_image_editor->get_size() + ); } /** * Test cropping an image */ public function test_crop() { - $file = DIR_TESTDATA . '/images/gradient-square.jpg'; $imagick_image_editor = new WP_Image_Editor_Imagick( $file ); @@ -80,15 +404,19 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase { $imagick_image_editor->crop( 0, 0, 50, 50 ); - $this->assertEquals( array( 'width' => 50, 'height' => 50 ), $imagick_image_editor->get_size() ); - + $this->assertEquals( + array( + 'width' => 50, + 'height' => 50, + ), + $imagick_image_editor->get_size() + ); } /** * Test rotating an image 180 deg */ public function test_rotate() { - $file = DIR_TESTDATA . '/images/gradient-square.jpg'; $imagick_image_editor = new WP_Image_Editor_Imagick( $file ); @@ -108,7 +436,6 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase { * Test flipping an image */ public function test_flip() { - $file = DIR_TESTDATA . '/images/gradient-square.jpg'; $imagick_image_editor = new WP_Image_Editor_Imagick( $file ); @@ -125,39 +452,36 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase { } /** - * Test the image created with WP_Image_Edior_Imagick preserves alpha when resizing + * Test the image created with WP_Image_Editor_Imagick preserves alpha when resizing * * @ticket 24871 */ public function test_image_preserves_alpha_on_resize() { - $file = DIR_TESTDATA . '/images/transparent.png'; $editor = wp_get_image_editor( $file ); $editor->load(); - $editor->resize(5,5); + $editor->resize( 5, 5 ); $save_to_file = tempnam( get_temp_dir(), '' ) . '.png'; $editor->save( $save_to_file ); $this->assertImageAlphaAtPoint( $save_to_file, array( 0,0 ), 127 ); - } - + /** - * Test the image created with WP_Image_Edior_Imagick preserves alpha with no resizing etc + * Test the image created with WP_Image_Editor_Imagick preserves alpha with no resizing etc * * @ticket 24871 */ public function test_image_preserves_alpha() { - $file = DIR_TESTDATA . '/images/transparent.png'; $editor = wp_get_image_editor( $file ); $editor->load(); $save_to_file = tempnam( get_temp_dir(), '' ) . '.png'; - + $editor->save( $save_to_file ); $this->assertImageAlphaAtPoint( $save_to_file, array( 0,0 ), 127 );