diff --git a/src/wp-includes/class-wp-image-editor-gd.php b/src/wp-includes/class-wp-image-editor-gd.php index ef804571c6..ae65813eff 100644 --- a/src/wp-includes/class-wp-image-editor-gd.php +++ b/src/wp-includes/class-wp-image-editor-gd.php @@ -114,7 +114,7 @@ class WP_Image_Editor_GD extends WP_Image_Editor { $this->update_size( $size[0], $size[1] ); $this->mime_type = $size['mime']; - return true; + return $this->set_quality(); } /** diff --git a/src/wp-includes/class-wp-image-editor-imagick.php b/src/wp-includes/class-wp-image-editor-imagick.php index 8bd6e38861..fd9d6541f1 100644 --- a/src/wp-includes/class-wp-image-editor-imagick.php +++ b/src/wp-includes/class-wp-image-editor-imagick.php @@ -140,10 +140,11 @@ class WP_Image_Editor_Imagick extends WP_Image_Editor { } $updated_size = $this->update_size(); - if ( is_wp_error( $updated_size ) ) - return $updated_size; + if ( is_wp_error( $updated_size ) ) { + return $updated_size; + } - return true; + return $this->set_quality(); } /** diff --git a/src/wp-includes/class-wp-image-editor.php b/src/wp-includes/class-wp-image-editor.php index 5792df3162..a4f2e3991b 100644 --- a/src/wp-includes/class-wp-image-editor.php +++ b/src/wp-includes/class-wp-image-editor.php @@ -236,10 +236,10 @@ abstract class WP_Image_Editor { * @param string $context Context of the filter. */ $quality = apply_filters( 'jpeg_quality', $quality, 'image_resize' ); + } - if ( ! $this->set_quality( $quality ) ) { - $this->quality = $this->default_quality; - } + if ( ! $this->set_quality( $quality ) ) { + $this->quality = $this->default_quality; } } @@ -256,8 +256,12 @@ abstract class WP_Image_Editor { * @return boolean|WP_Error True if set successfully; WP_Error on failure. */ public function set_quality( $quality = null ) { + if ( null === $quality ) { + $quality = $this->default_quality; + } + // Allow 0, but squash to 1 due to identical images in GD, and for backwards compatibility. - if ( $quality == 0 ) { + if ( 0 === $quality ) { $quality = 1; } diff --git a/tests/phpunit/tests/image/editor.php b/tests/phpunit/tests/image/editor.php index 51e681ce2b..2844a0831a 100644 --- a/tests/phpunit/tests/image/editor.php +++ b/tests/phpunit/tests/image/editor.php @@ -52,6 +52,9 @@ class Tests_Image_Editor extends WP_Image_UnitTestCase { // Get an editor $editor = wp_get_image_editor( DIR_TESTDATA . '/images/canola.jpg' ); + // Check default value + $this->assertEquals( 90, $editor->get_quality() ); + // Ensure set_quality works $this->assertTrue( $editor->set_quality( 75 ) ); $this->assertEquals( 75, $editor->get_quality() ); @@ -59,8 +62,8 @@ class Tests_Image_Editor extends WP_Image_UnitTestCase { // Ensure the quality filter works $func = create_function( '', "return 100;"); add_filter( 'wp_editor_set_quality', $func ); - $this->assertTrue( $editor->set_quality( 75 ) ); - $this->assertEquals( 75, $editor->get_quality() ); + $this->assertTrue( $editor->set_quality( 70 ) ); + $this->assertEquals( 70, $editor->get_quality() ); // Clean up remove_filter( 'wp_editor_set_quality', $func ); diff --git a/tests/phpunit/tests/image/editor_gd.php b/tests/phpunit/tests/image/editor_gd.php index 0e1fe608ce..c1abe2c4f4 100644 --- a/tests/phpunit/tests/image/editor_gd.php +++ b/tests/phpunit/tests/image/editor_gd.php @@ -463,6 +463,9 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase { $file = DIR_TESTDATA . '/images/transparent.png'; $editor = wp_get_image_editor( $file ); + + $this->assertNotInstanceOf( 'WP_Error', $editor ); + $editor->load(); $editor->resize( 5, 5 ); $save_to_file = tempnam( get_temp_dir(), '' ) . '.png'; @@ -483,6 +486,9 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase { $file = DIR_TESTDATA . '/images/transparent.png'; $editor = wp_get_image_editor( $file ); + + $this->assertNotInstanceOf( 'WP_Error', $editor ); + $editor->load(); $save_to_file = tempnam( get_temp_dir(), '' ) . '.png'; diff --git a/tests/phpunit/tests/image/editor_imagick.php b/tests/phpunit/tests/image/editor_imagick.php index d997b9c837..6e0342ea7f 100644 --- a/tests/phpunit/tests/image/editor_imagick.php +++ b/tests/phpunit/tests/image/editor_imagick.php @@ -463,6 +463,9 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase { $file = DIR_TESTDATA . '/images/transparent.png'; $editor = wp_get_image_editor( $file ); + + $this->assertNotInstanceOf( 'WP_Error', $editor ); + $editor->load(); $editor->resize( 5, 5 ); $save_to_file = tempnam( get_temp_dir(), '' ) . '.png'; @@ -483,6 +486,9 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase { $file = DIR_TESTDATA . '/images/transparent.png'; $editor = wp_get_image_editor( $file ); + + $this->assertNotInstanceOf( 'WP_Error', $editor ); + $editor->load(); $save_to_file = tempnam( get_temp_dir(), '' ) . '.png'; diff --git a/tests/phpunit/tests/image/functions.php b/tests/phpunit/tests/image/functions.php index 8fd7b81118..b7a456db49 100644 --- a/tests/phpunit/tests/image/functions.php +++ b/tests/phpunit/tests/image/functions.php @@ -224,6 +224,8 @@ class Tests_Image_Functions extends WP_UnitTestCase { // Save the image as each file extension, check the mime type $img = wp_get_image_editor( DIR_TESTDATA . '/images/canola.jpg' ); + $this->assertNotInstanceOf( 'WP_Error', $img ); + $temp = get_temp_dir(); foreach ( $mime_types as $ext => $mime_type ) { $file = wp_unique_filename( $temp, uniqid() . ".$ext" );