Make WordPress Core


Ignore:
Timestamp:
09/20/2019 06:20:26 PM (4 years ago)
Author:
azaozz
Message:

Media/Upload: rotate images on upload according to EXIF Orientation.

Props msaggiorato, wpdavis, markoheijnen, dhuyvetter, msaggiorato, n7studios, triplejumper12, pbiron, mikeschroder, joemcgill, azaozz.

Fixes #14459.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-image-editor-imagick.php

    r46088 r46202  
    567567            $this->image->rotateImage( new ImagickPixel( 'none' ), 360 - $angle );
    568568
    569             // Normalise Exif orientation data so that display is consistent across devices.
     569            // Normalise EXIF orientation data so that display is consistent across devices.
    570570            if ( is_callable( array( $this->image, 'setImageOrientation' ) ) && defined( 'Imagick::ORIENTATION_TOPLEFT' ) ) {
    571571                $this->image->setImageOrientation( Imagick::ORIENTATION_TOPLEFT );
     
    603603                $this->image->flopImage();
    604604            }
     605
     606            // Normalise EXIF orientation data so that display is consistent across devices.
     607            if ( is_callable( array( $this->image, 'setImageOrientation' ) ) && defined( 'Imagick::ORIENTATION_TOPLEFT' ) ) {
     608                $this->image->setImageOrientation( Imagick::ORIENTATION_TOPLEFT );
     609            }
    605610        } catch ( Exception $e ) {
    606611            return new WP_Error( 'image_flip_error', $e->getMessage() );
    607612        }
     613
    608614        return true;
     615    }
     616
     617    /**
     618     * Check if a JPEG image has EXIF Orientation tag and rotate it if needed.
     619     *
     620     * As ImageMagick copies the EXIF data to the flipped/rotated image, proceed only
     621     * if EXIF Orientation can be reset afterwards.
     622     *
     623     * @since 5.3.0
     624     *
     625     * @return bool|WP_Error True if the image was rotated. False if no EXIF data or if the image doesn't need rotation.
     626     *                       WP_Error if error while rotating.
     627     */
     628    public function maybe_exif_rotate() {
     629        if ( is_callable( array( $this->image, 'setImageOrientation' ) ) && defined( 'Imagick::ORIENTATION_TOPLEFT' ) ) {
     630            return parent::maybe_exif_rotate();
     631        } else {
     632            return new WP_Error( 'write_exif_error', __( 'The image cannot be rotated because the embedded meta data cannot be updated.' ) );
     633        }
    609634    }
    610635
Note: See TracChangeset for help on using the changeset viewer.