Skip to content

Commit

Permalink
fix: vertical images are rotated to horizontal when generating thumbn…
Browse files Browse the repository at this point in the history
…ails
  • Loading branch information
guqing committed Oct 12, 2024
1 parent 91a69de commit 2718db6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
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"
}

}

0 comments on commit 2718db6

Please sign in to comment.