Skip to content

Commit

Permalink
fix: Fix Gif Illustration generation - MEED-7435 - Meeds-io/meeds#2369 (
Browse files Browse the repository at this point in the history
#641)

Prior to this change, the Gif Image Reader doesn't allow to generate an
illustration with Metadata. This fix will generate the illustration
without Metadata and then write the Metadata in the generated PNG Image.
  • Loading branch information
boubaker authored Sep 10, 2024
1 parent 3c42e63 commit f1a02fc
Showing 1 changed file with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ public byte[] scaleImage(byte[] image,
return image;
}

ImageReader imageReader = getImageReader(image);

if (width == 0) {
bufferedImage = Scalr.resize(bufferedImage, resizeMethod, Scalr.Mode.FIT_TO_HEIGHT, width, height, Scalr.OP_ANTIALIAS);
} else if (height == 0) {
Expand All @@ -79,9 +77,16 @@ public byte[] scaleImage(byte[] image,
bufferedImage = Scalr.resize(bufferedImage, resizeMethod, fitMode, width, height, Scalr.OP_ANTIALIAS);
}

ImageReader imageReader = getImageReader(image);
byte[] response = toByteArray(bufferedImage, imageReader);
if (response.length == 0) {
response = toByteArray(bufferedImage);
// Use PNG Image Readear instead of Gif
imageReader = getImageReader(response);
response = toByteArray(bufferedImage, imageReader);
}

if (!fitExact && response.length > image.length) {
if (!fitExact && (response.length == 0 || response.length > image.length)) {
// if the original image is smaller in weight from the resized image, we
// must keep the original image
return image;
Expand Down Expand Up @@ -119,4 +124,10 @@ private byte[] toByteArray(BufferedImage targetBufferedImage, ImageReader source
return byteArrayOutputStream.toByteArray();
}

private byte[] toByteArray(BufferedImage bufferedImage) throws IOException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "png", byteArrayOutputStream);
return byteArrayOutputStream.toByteArray();
}

}

0 comments on commit f1a02fc

Please sign in to comment.