From 34fb3645e89b7bb0799edeedc065b2fac72b54f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20D=C3=A9nari=C3=A9?= Date: Fri, 13 Sep 2024 17:58:06 +0200 Subject: [PATCH] fix: Image Resize not working for some specific images - EXO-74160 Before this fix, in some particular case, images cannot be resized. This fix add option to correctly write the image for resize Resolves meeds-io/meeds#2386 (cherry picked from commit eca91ab3b9c61036ba3dbef933a04f5b3b98c0bf) --- .../services/thumbnail/ImageResizeServiceImpl.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/commons-component-common/src/main/java/org/exoplatform/services/thumbnail/ImageResizeServiceImpl.java b/commons-component-common/src/main/java/org/exoplatform/services/thumbnail/ImageResizeServiceImpl.java index 99fd46a0bd..aaa36cb2f3 100644 --- a/commons-component-common/src/main/java/org/exoplatform/services/thumbnail/ImageResizeServiceImpl.java +++ b/commons-component-common/src/main/java/org/exoplatform/services/thumbnail/ImageResizeServiceImpl.java @@ -24,8 +24,10 @@ import javax.imageio.IIOImage; import javax.imageio.ImageIO; import javax.imageio.ImageReader; +import javax.imageio.ImageWriteParam; import javax.imageio.ImageWriter; import javax.imageio.metadata.IIOMetadata; +import javax.imageio.plugins.jpeg.JPEGImageWriteParam; import javax.imageio.stream.ImageInputStream; import org.imgscalr.Scalr; @@ -111,7 +113,11 @@ private byte[] toByteArray(BufferedImage targetBufferedImage, ImageReader source writer.setOutput(ImageIO.createImageOutputStream(byteArrayOutputStream)); IIOMetadata metadata = sourceImageReader.getImageMetadata(0); - writer.write(new IIOImage(targetBufferedImage, null, metadata)); + ImageWriteParam param = writer.getDefaultWriteParam(); + if (param instanceof JPEGImageWriteParam) { + ((JPEGImageWriteParam) param).setOptimizeHuffmanTables(true); + } + writer.write(null,new IIOImage(targetBufferedImage, null, metadata), param); writer.dispose(); return byteArrayOutputStream.toByteArray(); }