diff --git a/includes/class-files.php b/includes/class-files.php index ab86ab96..7e80d6f2 100644 --- a/includes/class-files.php +++ b/includes/class-files.php @@ -203,9 +203,30 @@ public function upload_file( $file, $args = array() ) { } } else { $upload = wp_handle_upload( $file, apply_filters( 'uwp_handle_upload_overrides', array( 'test_form' => false ) ) ); + if ( ! empty( $upload['error'] ) ) { return new WP_Error( 'upload', $upload['error'] ); } else { + if ( ! empty( $upload['type'] ) && $upload['type'] != 'image/png' && strpos( $upload['type'], 'image/' ) === 0 ) { + // Fetch additional metadata from EXIF/IPTC. + $exif_meta = wp_read_image_metadata( $upload['file'] ); + + if ( ! empty( $exif_meta ) && is_array( $exif_meta ) && ! empty( $exif_meta['orientation'] ) && 1 !== (int) $exif_meta['orientation'] ) { + $editor = wp_get_image_editor( $upload['file'] ); + + if ( ! empty( $editor ) && ! is_wp_error( $editor ) ) { + // Rotate the whole original image if there is EXIF data and "orientation" is not 1. + $rotated = $editor->maybe_exif_rotate(); + $rotated = $rotated === true ? $editor->save( $editor->generate_filename( 'rotated' ) ) : false; + + if ( ! empty( $rotated ) && ! is_wp_error( $rotated ) && ! empty( $rotated['path'] ) ) { + $upload['url'] = str_replace( basename( $upload['url'] ), basename( $rotated['path'] ), $upload['url'] ); + $upload['file'] = $rotated['path']; + } + } + } + } + $uploaded_file->url = $upload['url']; $uploaded_file->name = basename( $upload['file'] ); $uploaded_file->path = $upload['file']; @@ -215,7 +236,6 @@ public function upload_file( $file, $args = array() ) { } } - return $uploaded_file; } diff --git a/readme.txt b/readme.txt index e4d11b58..f97a1d53 100644 --- a/readme.txt +++ b/readme.txt @@ -146,6 +146,9 @@ Yes, you can customize it with Elementor, but also with Gutenberg, Divi, Beaver == Changelog == += 1.2.10 = +* Rotate image when required before crop image - CHANGED + = 1.2.9 = * JS error breaks Country field in register form - FIXED