Skip to:
Content

BuddyPress.org

Ticket #5089: 5089.2.patch

File 5089.2.patch, 3.3 KB (added by shanebp, 12 years ago)
  • bp-core-avatars.php

     
    572572        if ( !apply_filters( 'bp_core_pre_avatar_handle_upload', true, $file, $upload_dir_filter ) )
    573573                return true;
    574574
     575        //read raw image data
     576        $exif = @exif_read_data( $_FILES['file']['tmp_name'] );         
     577               
    575578        require_once( ABSPATH . '/wp-admin/includes/file.php' );
    576579
    577580        $uploadErrors = array(
     
    619622        $size  = @getimagesize( $bp->avatar_admin->original['file'] );
    620623        $error = false;
    621624
    622         // Check image size and shrink if too large
    623         if ( $size[0] > bp_core_avatar_original_max_width() ) {
    624                 $editor = wp_get_image_editor( $bp->avatar_admin->original['file'] );
     625        $img_editor_args = array(
     626                'methods' => array(
     627                        'resize',
     628                        'save',
     629                        'rotate'
     630                )
     631        );
    625632
    626                 if ( ! is_wp_error( $editor ) ) {
     633        $img_editor_test = wp_image_editor_supports($img_editor_args);
     634
     635        if ( $img_editor_test !== false ) {     
     636               
     637                $editor = wp_get_image_editor( $bp->avatar_admin->original['file'] );   
     638                       
     639                if ( ! is_wp_error( $editor ) ) {       
     640               
    627641                        $editor->set_quality( 100 );
    628 
    629                         $resized = $editor->resize( bp_core_avatar_original_max_width(), bp_core_avatar_original_max_width(), false );
    630                         if ( ! is_wp_error( $resized ) ) {
     642                       
     643                        $rotate_img = false;
     644                        $resize_img = false;                           
     645                               
     646                        // Check image rotation and adjust if necessary
     647                if( !empty( $exif['Orientation'] ) ) {
     648                 
     649                                switch( $exif['Orientation'] ) { 
     650                                        case 3: 
     651                                                $editor->rotate( 180 ); 
     652                                                $rotate_img = true;
     653                                                break; 
     654                                        case 6: 
     655                                                $editor->rotate( -90 ); 
     656                                                $rotate_img = true;
     657                                                break; 
     658                                        case 8: 
     659                                                $editor->rotate( 90 );
     660                                                $rotate_img = true;
     661                                                break;
     662                                } 
     663                        }                               
     664                       
     665                        // Check image size and shrink if too large
     666                        if ( $size[0] > bp_core_avatar_original_max_width() ) {
     667               
     668                                $resized = $editor->resize( bp_core_avatar_original_max_width(), bp_core_avatar_original_max_width(), false );
     669                               
     670                                if ( ! is_wp_error( $resized ) ) {
     671                                        $resize_img = true;
     672                                } else {
     673                                       
     674                                        $error = $resized;
     675                                }
     676       
     677                        }
     678                       
     679                        // Save the image with changes to size and/or rotation
     680                        if( $rotate_img || $resize_img ) {
     681                       
    631682                                $thumb = $editor->save( $editor->generate_filename() );
    632                         } else {
    633                                 $error = $resized;
     683                               
     684                                // Check for thumbnail creation errors
     685                                if ( false === $error && is_wp_error( $thumb ) ) {
     686                                        $error = $thumb;
     687                                }
     688       
     689                                // Thumbnail is good so proceed
     690                                if ( false === $error ) {
     691                                        $bp->avatar_admin->resized = $thumb;
     692                                }
    634693                        }
    635694
    636                         // Check for thumbnail creation errors
    637                         if ( false === $error && is_wp_error( $thumb ) ) {
    638                                 $error = $thumb;
    639                         }
    640 
    641                         // Thumbnail is good so proceed
    642                         if ( false === $error ) {
    643                                 $bp->avatar_admin->resized = $thumb;
    644                         }
    645 
    646695                } else {
    647696                        $error = $editor;
    648697                }
    649 
     698                       
    650699                if ( false !== $error ) {
    651700                        bp_core_add_message( sprintf( __( 'Upload Failed! Error was: %s', 'buddypress' ), $error->get_error_message() ), 'error' );
    652701                        return false;
    653                 }
     702                }               
    654703        }
     704        else {
     705                bp_core_add_message( 'Image editor not supported.' );
     706        }
    655707
    656708        if ( ! isset( $bp->avatar_admin->image ) )
    657709                $bp->avatar_admin->image = new stdClass();