Make WordPress Core


Ignore:
Timestamp:
09/20/2019 06:20:26 PM (5 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.php

    r45590 r46202  
    386386
    387387    /**
     388     * Check if a JPEG image has EXIF Orientation tag and rotate it if needed.
     389     *
     390     * @since 5.3.0
     391     *
     392     * @return bool|WP_Error True if the image was rotated. False if not rotated (no EXIF data or the image doesn't need to be rotated).
     393     *                       WP_Error if error while rotating.
     394     */
     395    public function maybe_exif_rotate() {
     396        $orientation = null;
     397
     398        if ( is_callable( 'exif_read_data' ) && 'image/jpeg' === $this->mime_type ) {
     399            $exif_data = @exif_read_data( $this->file );
     400
     401            if ( ! empty( $exif_data['Orientation'] ) ) {
     402                $orientation = (int) $exif_data['Orientation'];
     403            }
     404        }
     405
     406        /**
     407         * Filters the `$orientation` value to correct it before rotating or to prevemnt rotating the image.
     408         *
     409         * @since 5.3.0
     410         *
     411         * @param int    $orientation EXIF Orientation value as retrieved from the image file.
     412         * @param string $file        Path to the image file.
     413         */
     414        $orientation = apply_filters( 'wp_image_maybe_exif_rotate', $orientation, $this->file );
     415
     416        if ( ! $orientation || $orientation === 1 ) {
     417            return false;
     418        }
     419
     420        switch ( $orientation ) {
     421            case 2:
     422                // Flip horizontally.
     423                $result = $this->flip( true, false );
     424                break;
     425            case 3:
     426                // Rotate 180 degrees or flip horizontally and vertically.
     427                // Flipping seems faster/uses less resources.
     428                $result = $this->flip( true, true );
     429                break;
     430            case 4:
     431                // Flip vertically.
     432                $result = $this->flip( false, true );
     433                break;
     434            case 5:
     435                // Rotate 90 degrees counter-clockwise and flip vertically.
     436                $result = $this->rotate( 90 );
     437
     438                if ( ! is_wp_error( $result ) ) {
     439                    $result = $this->flip( false, true );
     440                }
     441
     442                break;
     443            case 6:
     444                // Rotate 90 degrees clockwise (270 counter-clockwise).
     445                $result = $this->rotate( 270 );
     446                break;
     447            case 7:
     448                // Rotate 90 degrees counter-clockwise and flip horizontally.
     449                $result = $this->rotate( 90 );
     450
     451                if ( ! is_wp_error( $result ) ) {
     452                    $result = $this->flip( true, false );
     453                }
     454
     455                break;
     456            case 8:
     457                // Rotate 90 degrees counter-clockwise.
     458                $result = $this->rotate( 90 );
     459                break;
     460        }
     461
     462        return $result;
     463    }
     464
     465    /**
    388466     * Either calls editor's save function or handles file as a stream.
    389467     *
Note: See TracChangeset for help on using the changeset viewer.