Skip to content

Commit

Permalink
Apply image EXIF rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurentTreguier committed Oct 18, 2024
1 parent 0709124 commit 4abc768
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 17 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ group = "app.fyreplace"
version = gitVersion()

repositories {
maven { url "https://www.javaxt.com/maven/" }
mavenCentral()
mavenLocal()
}
Expand Down Expand Up @@ -47,6 +48,7 @@ dependencies {
implementation("io.quarkus:quarkus-smallrye-openapi")
implementation("io.quarkiverse.amazonservices:quarkus-amazon-s3")
implementation("io.sentry:sentry-jul")
implementation("javaxt:javaxt-core:${javaXtVersion}")
implementation("software.amazon.awssdk:url-connection-client")
implementation("software.amazon.awssdk.crt:aws-crt")
implementation("com.twelvemonkeys.imageio:imageio-webp:${twelveMonkeysVersion}")
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ org.gradle.jvmargs=-Xmx1024M
quarkusPlatformVersion=3.15.1
quarkusPluginVersion=3.15.1
sentryVersion=7.14.0
javaXtVersion=2.1.9
twelveMonkeysVersion=3.11.0
spotlessPluginVersion=6.25.0
lombokPluginVersion=8.10
27 changes: 10 additions & 17 deletions src/main/java/app/fyreplace/api/services/ImageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
import app.fyreplace.api.services.mimetype.KnownFileType;
import io.quarkus.runtime.configuration.MemorySize;
import jakarta.enterprise.context.ApplicationScoped;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.NoSuchElementException;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javaxt.io.Image;
import org.eclipse.microprofile.config.inject.ConfigProperty;

@ApplicationScoped
Expand All @@ -30,7 +29,7 @@ public String getMimeType(final byte[] data) throws IOException {
.findFirst()
.orElseThrow()
.mime;
} catch (final NoSuchElementException ignored) {
} catch (final NoSuchElementException e) {
throw new IOException();
}
}
Expand Down Expand Up @@ -61,30 +60,24 @@ public void validate(final byte[] data) throws UnsupportedMediaTypeException {
}
}

public byte[] shrink(final byte[] data) throws IOException {
public byte[] shrink(final byte[] data) {
final var softMaxSize = fileMaxSize.asLongValue();

if (data.length <= softMaxSize) {
return data;
}

final var inputImage = new Image(data);
final var scaleFactor = Math.sqrt((double) softMaxSize / data.length);
final var reader = getFirstValidReader(data);
final var inputImage = ImageIO.read(new ByteArrayInputStream(data));
final var width = inputImage.getWidth() * scaleFactor;
final var height = inputImage.getHeight() * scaleFactor;
final var scaledImage = inputImage.getScaledInstance((int) width, (int) height, BufferedImage.SCALE_SMOOTH);
final var outputImage = new BufferedImage((int) width, (int) height, inputImage.getType());
outputImage.getGraphics().drawImage(scaledImage, 0, 0, null);
final var outputStream = new ByteArrayOutputStream();
ImageIO.write(outputImage, reader.getFormatName(), outputStream);
final var outputData = outputStream.toByteArray();

if (outputData.length > softMaxSize * 1.5) {
inputImage.resize((int) (inputImage.getWidth() * scaleFactor), (int) (inputImage.getHeight() * scaleFactor));
inputImage.rotate();
final var output = inputImage.getByteArray();

if (output.length > softMaxSize * 1.5) {
throw new RequestEntityTooLargeException();
}

return outputData;
return inputImage.getByteArray();
}

private ImageReader getFirstValidReader(final byte[] data) throws IOException {
Expand Down

0 comments on commit 4abc768

Please sign in to comment.