Make WordPress Core

Ticket #14459: 14459.diff

File 14459.diff, 2.4 KB (added by wpdavis, 9 years ago)
  • includes/file.php

     
    384384                'file' => $new_file,
    385385                'url'  => $url,
    386386                'type' => $type
    387         ), 'wp_handle_sideload' === $action ? 'sideload' : 'upload' ); }
     387        ), 'wp_handle_sideload' === $action ? 'sideload' : 'upload' );
     388}
    388389
    389390/**
    390391 * Wrapper for _wp_handle_upload(), passes 'wp_handle_upload' action.
     
    415416}
    416417
    417418/**
     419 * On successful upload of a new image, check to see if it has an orientation.
     420 * Orientations are only respected on some browsers, and can cause images to
     421 * display incorrectly, so we auto-rotate everything to be upright.
     422 *
     423 * @since 4.4
     424 *
     425 * @param array  $file {
     426 *     Array of upload data.
     427 *
     428 *     @type string $file Filepath of the newly-uploaded file.
     429 *     @type string $url  URL of the uploaded file.
     430 *     @type string $type File mime.
     431 * }
     432 */
     433add_filter( 'wp_handle_upload', 'wp_maybe_rotate_image', 1 );
     434function wp_maybe_rotate_image( $file = array() ) {
     435
     436        //Not an image file
     437        if( 0 !== strpos( $file[ 'type' ], 'image/' ) )
     438                return $file;
     439
     440        //Read the EXIF data of the file
     441        $meta = wp_read_image_metadata( $file[ 'file' ] );
     442       
     443        //No orientation in the file
     444        if( empty( $meta ) || empty( $meta[ 'orientation' ] ) )
     445                return $file;
     446
     447        //Orientation is saved as a numeral, corresponding to the below rotations
     448        switch ( $meta[ 'orientation' ] ) {
     449                case 3:
     450                        $rotation = -180;
     451                        break;
     452                case 6:
     453                        $rotation = -90;
     454                        break;
     455                case 8:
     456                case 9:
     457                        $rotation = -270;
     458                        break;
     459                default:
     460                        $rotation = 0;
     461                        break;
     462        }
     463       
     464        //Unknown rotation, or none at all
     465        if( empty( $rotation ) )
     466                return $file;
     467
     468        //Load the file using ImageMagick or GD
     469        //We test to make sure we can rotate the file using the given image editor
     470        $editor = wp_get_image_editor( $file[ 'file' ], array( 'mime_type' => $file[ 'type' ], 'methods' => array( 'rotate' ) ) );
     471
     472        //No editor, or unable to edit this type of file
     473        if( is_wp_error( $editor ) )
     474                return $file;
     475       
     476        //Rotate the file the specified degrees and save it to its original location
     477        if( !is_wp_error( $editor->rotate( $rotation ) ) )
     478                $editor->save( $file[ 'type' ], $file[ 'type' ] );
     479
     480        return $file;
     481
     482}
     483
     484/**
    418485 * Wrapper for _wp_handle_upload(), passes 'wp_handle_sideload' action
    419486 *
    420487 * @since 2.6.0