From 46bb2c2a2ce60bbd6a5a805484cf5925235a421b 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 --- .../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 c88ed183af..503f70faa6 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; @@ -119,7 +121,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(); }