Skip to content

Commit

Permalink
Make image scaling more resilient
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurentTreguier committed Oct 19, 2024
1 parent 0723318 commit f3ce671
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/main/java/app/fyreplace/api/services/ImageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,23 +61,29 @@ public void validate(final byte[] data) throws UnsupportedMediaTypeException {
}

public byte[] shrink(final byte[] data) {
final var softMaxSize = fileMaxSize.asLongValue();
return shrink(data, 1.5);
}

private byte[] shrink(final byte[] data, final double scaleFactorIncrease) {
final var maxSize = fileMaxSize.asLongValue();

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

final var inputImage = new Image(data);
final var scaleFactor = Math.sqrt((double) softMaxSize / data.length);
inputImage.resize((int) (inputImage.getWidth() * scaleFactor), (int) (inputImage.getHeight() * scaleFactor));
final var scaleFactor = Math.sqrt((double) data.length / maxSize) * scaleFactorIncrease;
inputImage.resize((int) (inputImage.getWidth() / scaleFactor), (int) (inputImage.getHeight() / scaleFactor));
inputImage.rotate();
final var output = inputImage.getByteArray();

if (output.length > softMaxSize * 3) {
if (output.length <= maxSize) {
return output;
} else if (scaleFactorIncrease >= 3) {
throw new RequestEntityTooLargeException();
} else {
return shrink(data, scaleFactorIncrease + 0.5);
}

return output;
}

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

0 comments on commit f3ce671

Please sign in to comment.