diff --git a/src/wp-includes/class-wp-image-editor-imagick.php b/src/wp-includes/class-wp-image-editor-imagick.php index 8f9b43cb89..3589cc22a1 100644 --- a/src/wp-includes/class-wp-image-editor-imagick.php +++ b/src/wp-includes/class-wp-image-editor-imagick.php @@ -549,6 +549,11 @@ class WP_Image_Editor_Imagick extends WP_Image_Editor { try { $this->image->rotateImage( new ImagickPixel('none'), 360-$angle ); + // Normalise Exif orientation data so that display is consistent across devices. + if ( is_callable( array( $this->image, 'setImageOrientation' ) ) && defined( 'Imagick::ORIENTATION_TOPLEFT' ) ) { + $this->image->setImageOrientation( Imagick::ORIENTATION_TOPLEFT ); + } + // Since this changes the dimensions of the image, update the size. $result = $this->update_size(); if ( is_wp_error( $result ) ) diff --git a/tests/phpunit/data/images/test-image-upside-down.jpg b/tests/phpunit/data/images/test-image-upside-down.jpg new file mode 100644 index 0000000000..74505cfc1d Binary files /dev/null and b/tests/phpunit/data/images/test-image-upside-down.jpg differ diff --git a/tests/phpunit/tests/image/editor_imagick.php b/tests/phpunit/tests/image/editor_imagick.php index c475201c41..e113b77937 100644 --- a/tests/phpunit/tests/image/editor_imagick.php +++ b/tests/phpunit/tests/image/editor_imagick.php @@ -542,4 +542,34 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase { $this->assertTrue( $result ); } + + /** + * Test resetting Exif orientation data on rotate + * + * @ticket 37140 + */ + public function test_remove_orientation_data_on_rotate() { + $file = DIR_TESTDATA . "/images/test-image-upside-down.jpg"; + $data = wp_read_image_metadata( $file ); + + // The orientation value 3 is equivalent to rotated upside down (180 degrees). + $this->assertEquals( 3, intval( $data['orientation'] ), 'Orientation value read from does not match image file Exif data: ' . $file ); + + $temp_file = wp_tempnam( $file ); + $image = wp_get_image_editor( $file ); + + // Test a value that would not lead back to 1, as WP is resetting the value to 1 manually. + $image->rotate( 90 ); + $ret = $image->save( $temp_file, 'image/jpeg' ); + + $data = wp_read_image_metadata( $ret['path'] ); + + // Make sure the image is no longer in The Upside Down Exif orientation. + $this->assertEquals( 1, intval( $data['orientation'] ), 'Orientation Exif data was not updated after rotating image: ' . $file ); + + // Remove both the generated file ending in .tmp and tmp.jpg due to wp_tempnam(). + unlink( $temp_file ); + unlink( $ret['path'] ); + } + }