Skip to content

Commit

Permalink
fix: Fix preserving Image EXIF Orientation Metadata after scaling - M…
Browse files Browse the repository at this point in the history
…EED-2471 - Meeds-io/meeds#1126 (#633)

Prior to this change, when attaching a JPEG image took by Camera in
landscape and then attach it in an activity, then the image preview
orientation will change. This change will preserve Image metadata after
scaling in order to fix the lack of EXIF information in resulted Resized
Image.

(cherry picked from commit 7d9d259)
  • Loading branch information
boubaker authored and Jihed525 committed Aug 23, 2024
1 parent 6dbde06 commit 8287601
Showing 1 changed file with 35 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,23 @@
*/
package org.exoplatform.services.thumbnail;

import org.exoplatform.services.log.ExoLogger;
import org.exoplatform.services.log.Log;
import org.imgscalr.Scalr;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Iterator;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.ImageWriter;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.stream.ImageInputStream;

import org.imgscalr.Scalr;

import org.exoplatform.services.log.ExoLogger;
import org.exoplatform.services.log.Log;

public class ImageResizeServiceImpl implements ImageResizeService {

Expand All @@ -49,7 +57,7 @@ public byte[] scaleImage(byte[] image, int width, int height, boolean fitExact,
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 @@ -67,15 +75,26 @@ public byte[] scaleImage(byte[] image, int width, int height, boolean fitExact,
bufferedImage = Scalr.resize(bufferedImage, resizeMethod, fitMode, width, height, Scalr.OP_ANTIALIAS);
}

byte[] response = toByteArray(bufferedImage);
if (response.length > image.length) {
//if the original image is smaller in weight from the resized image, we must keep the original image

byte[] response = toByteArray(bufferedImage, imageReader);

if (!fitExact && response.length > image.length) {
// if the original image is smaller in weight from the resized image, we
// must keep the original image
return image;
} else {
return response;
}
}

private ImageReader getImageReader(byte[] bytes) throws IOException {
ImageInputStream imageInputStream = ImageIO.createImageInputStream(new ByteArrayInputStream(bytes));
Iterator<ImageReader> readers = ImageIO.getImageReaders(imageInputStream);
ImageReader reader = readers.next();
reader.setInput(imageInputStream);
return reader;
}

private BufferedImage toBufferedImage(byte[] imageBytes) {
try {
ByteArrayInputStream bis = new ByteArrayInputStream(imageBytes);
Expand All @@ -86,9 +105,14 @@ private BufferedImage toBufferedImage(byte[] imageBytes) {
}
}

private byte[] toByteArray(BufferedImage bufferedImage) throws IOException {
private byte[] toByteArray(BufferedImage targetBufferedImage, ImageReader sourceImageReader) throws IOException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "png", byteArrayOutputStream);
ImageWriter writer = ImageIO.getImageWriter(sourceImageReader);
writer.setOutput(ImageIO.createImageOutputStream(byteArrayOutputStream));

IIOMetadata metadata = sourceImageReader.getImageMetadata(0);
writer.write(new IIOImage(targetBufferedImage, null, metadata));
writer.dispose();
return byteArrayOutputStream.toByteArray();
}

Expand Down

0 comments on commit 8287601

Please sign in to comment.