Media: Reset Exif orientation after rotate in WP_Image_Editor_Imagick.

Due to inconsistencies in the way browsers handle Exif orientation data,
if a user manually rotates an image within WordPress, set the Exif orientation to
the default (1) so that the image displays with the same rotation/flip in every browser.

Props sanchothefat, triplejumper12, joemcgill, azaozz, markoheijnen, mikeschroder.
Merges [40123] and [40129] to the 4.7 branch.
Fixes #37140. See #14459.


git-svn-id: https://develop.svn.wordpress.org/branches/4.7@40135 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Joe McGill 2017-02-27 19:32:45 +00:00
parent f055694b32
commit 260f39ea0f
3 changed files with 35 additions and 0 deletions

View File

@ -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 ) )

Binary file not shown.

After

Width:  |  Height:  |  Size: 138 KiB

View File

@ -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'] );
}
}