Skip to content
Snippets Groups Projects
Commit 7a7b2bb9 authored by Michael Hamann's avatar Michael Hamann
Browse files

XWIKI-22411: Image inserted with height or width parameter isn't displayed in...

XWIKI-22411: Image inserted with height or width parameter isn't displayed in the orientation provided by EXIF data

* Use Thumbnailator to read the image which correctly interprets the
  EXIF data.
* Add a test with a test image.

(cherry picked from commit 5f134afb)
parent 56feb9b6
No related branches found
No related tags found
No related merge requests found
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage; import java.awt.image.RenderedImage;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.Iterator; import java.util.Iterator;
...@@ -34,6 +35,7 @@ ...@@ -34,6 +35,7 @@
import org.xwiki.component.annotation.Component; import org.xwiki.component.annotation.Component;
import net.coobird.thumbnailator.Thumbnails; import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.resizers.Resizers;
/** /**
* Image processor implementation based on the Thumbnailator library. * Image processor implementation based on the Thumbnailator library.
...@@ -47,6 +49,21 @@ ...@@ -47,6 +49,21 @@
@Named("thumbnailator") @Named("thumbnailator")
public class ThumbnailatorImageProcessor extends DefaultImageProcessor public class ThumbnailatorImageProcessor extends DefaultImageProcessor
{ {
@Override
public Image readImage(InputStream inputStream) throws IOException
{
try {
// Use Thumbnailator to read the image to correctly interpret the orientation that is set in Exif metadata.
return Thumbnails.of(inputStream)
.scale(1)
// Ensure that nothing is actually resized and there is thus no quality loss.
.resizer(Resizers.NULL)
.asBufferedImage();
} catch (Exception e) {
return super.readImage(inputStream);
}
}
@Override @Override
public void writeImage(RenderedImage image, String mimeType, float quality, OutputStream out) throws IOException public void writeImage(RenderedImage image, String mimeType, float quality, OutputStream out) throws IOException
{ {
......
...@@ -24,6 +24,8 @@ ...@@ -24,6 +24,8 @@
import java.awt.Image; import java.awt.Image;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage; import java.awt.image.RenderedImage;
import java.io.IOException;
import java.io.InputStream;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.xwiki.test.junit5.mockito.ComponentTest; import org.xwiki.test.junit5.mockito.ComponentTest;
...@@ -42,6 +44,19 @@ class ThumbnailatorImageProcessorTest ...@@ -42,6 +44,19 @@ class ThumbnailatorImageProcessorTest
@InjectMockComponents @InjectMockComponents
private ThumbnailatorImageProcessor imageProcessor; private ThumbnailatorImageProcessor imageProcessor;
@Test
void readImage() throws IOException
{
// Example image from https://github.com/recurser/exif-orientation-examples,
// Copyright (c) 2010 Dave Perrett, http://recursive-design.com/, licensed under the MIT license.
// Original photo by John Salvino https://unsplash.com/photos/1PPpwrTNkJI
try (InputStream imageInput = getClass().getResourceAsStream("/Portrait_5.jpg")) {
Image image = this.imageProcessor.readImage(imageInput);
assertEquals(1200, image.getWidth(null));
assertEquals(1800, image.getHeight(null));
}
}
@Test @Test
void changeAspectRatio() void changeAspectRatio()
{ {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment