Index: bp-core-avatars.php
===================================================================
--- bp-core-avatars.php	(revision 8611)
+++ bp-core-avatars.php	(working copy)
@@ -572,6 +572,9 @@
 	if ( !apply_filters( 'bp_core_pre_avatar_handle_upload', true, $file, $upload_dir_filter ) )
 		return true;
 
+	//read raw image data
+	$exif = @exif_read_data( $_FILES['file']['tmp_name'] );		
+		
 	require_once( ABSPATH . '/wp-admin/includes/file.php' );
 
 	$uploadErrors = array(
@@ -619,39 +622,88 @@
 	$size  = @getimagesize( $bp->avatar_admin->original['file'] );
 	$error = false;
 
-	// Check image size and shrink if too large
-	if ( $size[0] > bp_core_avatar_original_max_width() ) {
-		$editor = wp_get_image_editor( $bp->avatar_admin->original['file'] );
+	$img_editor_args = array(
+		'methods' => array(
+			'resize',
+			'save',
+			'rotate'
+		)
+	);
 
-		if ( ! is_wp_error( $editor ) ) {
+	$img_editor_test = wp_image_editor_supports($img_editor_args);
+
+	if ( $img_editor_test !== false ) {	
+		
+		$editor = wp_get_image_editor( $bp->avatar_admin->original['file'] );	
+			
+		if ( ! is_wp_error( $editor ) ) {	
+		
 			$editor->set_quality( 100 );
-
-			$resized = $editor->resize( bp_core_avatar_original_max_width(), bp_core_avatar_original_max_width(), false );
-			if ( ! is_wp_error( $resized ) ) {
+			
+			$rotate_img = false; 
+			$resize_img = false; 				
+				
+			// Check image rotation and adjust if necessary	
+	        if( !empty( $exif['Orientation'] ) ) { 
+                  
+				switch( $exif['Orientation'] ) {  
+					case 3:  
+						$editor->rotate( 180 );  
+						$rotate_img = true;
+						break;  
+					case 6:  
+						$editor->rotate( -90 );  
+						$rotate_img = true;
+						break;  
+					case 8:  
+						$editor->rotate( 90 ); 
+						$rotate_img = true; 
+						break; 
+				}  
+			}                     		
+			
+			// Check image size and shrink if too large
+			if ( $size[0] > bp_core_avatar_original_max_width() ) {
+		
+				$resized = $editor->resize( bp_core_avatar_original_max_width(), bp_core_avatar_original_max_width(), false );
+				
+				if ( ! is_wp_error( $resized ) ) {
+					$resize_img = true; 
+				} else {
+					
+					$error = $resized;
+				}
+	
+			} 
+			
+			// Save the image with changes to size and/or rotation 
+			if( $rotate_img || $resize_img ) {
+			
 				$thumb = $editor->save( $editor->generate_filename() );
-			} else {
-				$error = $resized;
+				
+				// Check for thumbnail creation errors
+				if ( false === $error && is_wp_error( $thumb ) ) {
+					$error = $thumb;
+				}
+	
+				// Thumbnail is good so proceed
+				if ( false === $error ) {
+					$bp->avatar_admin->resized = $thumb;
+				}
 			}
 
-			// Check for thumbnail creation errors
-			if ( false === $error && is_wp_error( $thumb ) ) {
-				$error = $thumb;
-			}
-
-			// Thumbnail is good so proceed
-			if ( false === $error ) {
-				$bp->avatar_admin->resized = $thumb;
-			}
-
 		} else {
 			$error = $editor;
 		}
-
+			
 		if ( false !== $error ) {
 			bp_core_add_message( sprintf( __( 'Upload Failed! Error was: %s', 'buddypress' ), $error->get_error_message() ), 'error' );
 			return false;
-		}
+		}		
 	}
+	else { 
+		bp_core_add_message( 'Image editor not supported.' );
+	}
 
 	if ( ! isset( $bp->avatar_admin->image ) )
 		$bp->avatar_admin->image = new stdClass();
