Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: vertical images are rotated to horizontal when generating thumbnails #6842

Merged
merged 1 commit into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ dependencies {
api "org.thymeleaf.extras:thymeleaf-extras-springsecurity6"
api 'org.apache.tika:tika-core'
api "org.imgscalr:imgscalr-lib"
api 'com.drewnoakes:metadata-extractor'

api "io.github.resilience4j:resilience4j-spring-boot3"
api "io.github.resilience4j:resilience4j-reactor"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;

import com.drew.imaging.ImageMetadataReader;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.exif.ExifIFD0Directory;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -92,9 +96,38 @@ private void generateThumbnail(Path tempImagePath) throws IOException {
}
var thumbnail = Scalr.resize(img, Scalr.Method.AUTOMATIC, Scalr.Mode.FIT_TO_WIDTH,
size.getWidth());
// Rotate image if needed
var orientation = readExifOrientation(file);
if (orientation != null) {
thumbnail = Scalr.rotate(thumbnail, orientation);
}
ImageIO.write(thumbnail, formatName, thumbnailFile);
}

private static Scalr.Rotation readExifOrientation(File inputFile) {
try {
Metadata metadata = ImageMetadataReader.readMetadata(inputFile);
Directory directory = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class);
if (directory != null && directory.containsTag(ExifIFD0Directory.TAG_ORIENTATION)) {
return getScalrRotationFromExifOrientation(
directory.getInt(ExifIFD0Directory.TAG_ORIENTATION));
}
} catch (Exception e) {
log.debug("Failed to read EXIF orientation from file: {}", inputFile, e);
}
return null;
}

private static Scalr.Rotation getScalrRotationFromExifOrientation(int orientation) {
// https://www.media.mit.edu/pia/Research/deepview/exif.html#:~:text=0x0112-,Orientation,-unsigned%20short
return switch (orientation) {
case 3 -> Scalr.Rotation.CW_180;
case 6 -> Scalr.Rotation.CW_90;
case 8 -> Scalr.Rotation.CW_270;
default -> null;
};
}

private static boolean isUnsupportedFormat(@NonNull String formatName) {
return UNSUPPORTED_FORMATS.contains(formatName.toLowerCase());
}
Expand Down
2 changes: 2 additions & 0 deletions platform/application/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ ext {
twoFactorAuth = "1.3"
tika = "2.9.2"
imgscalr = '4.2'
exifExtractor = '2.19.0'
}

javaPlatform {
Expand Down Expand Up @@ -58,6 +59,7 @@ dependencies {
api "com.j256.two-factor-auth:two-factor-auth:$twoFactorAuth"
api "org.apache.tika:tika-core:$tika"
api "org.imgscalr:imgscalr-lib:$imgscalr"
api "com.drewnoakes:metadata-extractor:$exifExtractor"
}

}
Loading