coobird / thumbnailator

Thumbnailator - a thumbnail generation library for Java
MIT License
5.14k stars 784 forks source link

Thumbnail image is always rotated, useExifOrientation parameter does not matter #159

Closed dmitry-weirdo closed 3 years ago

dmitry-weirdo commented 3 years ago

Expected behavior

Image thumbnail is NOT rotated 90 degrees left.

Actual behavior

If image has height more than width, it is rotated 90 degrees left.

20200425_142028

20200425_142028-thumbnail

Whatever combinations of size, width, useExifOrientation, keepAspectRatio I set, the thumbnail is rotated. Image size is also always the same.

Code example:

    public static void generateThumbnail(File imageFile, File thumbnailFile, Integer width) {
        try {
            // First convert to BufferedImage
            BufferedImage bufferedImage = ImageIO.read(imageFile);

            // Make thumbnail
            Thumbnails
                .of(bufferedImage)
//                .size(THUMBNAIL_WIDTH, THUMBNAIL_WIDTH)
//                .keepAspectRatio(true)
//                .useExifOrientation(true)
                .width(width)
//                .height(width)
                .useExifOrientation(true)
                .outputFormat("jpg")
                .toFile(thumbnailFile);

            logger.debug("Thumbnail of image file {} generated to file {}", imageFile.getPath(), thumbnailFile.getPath());
        }
        catch (IOException e) {
            logger.error(String.format("Error while generating thumbnail of file %s to thumbnail file %s", imageFile.getPath(), thumbnailFile.getPath()), e);

            throw new RuntimeException(e);
        }
    }

Steps to reproduce the behavior

See the "Actual Behavior" section

Environment

dmitry-weirdo commented 3 years ago

Looks like it's working for File and InputStream, but not working for BufferedImage of this file.

            BufferedImage bufferedImage = ImageIO.read(imageFile);

            InputStream inputStream = new FileInputStream(imageFile);

            // NOT rotated!           
            Thumbnails.of(inputStream)
                .width(width)
                .outputFormat("jpg")
                .toFile("c:\\java\\thumbnail-input-stream.jpg");

            // NOT rotated!           
            Thumbnails.of(imageFile)
                .width(width)
                .outputFormat("jpg")
                .toFile("c:\\java\\thumbnail-image-file.jpg");

            // Rotated :( 
            Thumbnails.of(bufferedImage)
                .width(width)
                .outputFormat("jpg")
                .toFile("c:\\java\\thumbnail-buffered-image.jpg");

thumbnail-buffered-image thumbnail-image-file thumbnail-input-stream

coobird commented 3 years ago

@dmitry-weirdo, Exif is metadata that's stored in certain image files. A BufferedImage does not contain Exif information. If you want to use Exif-based rotation, you will need to use either the File or InputStream as the input.

coobird commented 3 years ago

I'm closing this issue as it is expected behavior. Please refer to my previous comment for why rotation based on Exif won't work in the code you originally posted.

Boldbayar commented 2 years ago

IT is also happening when i work with FIle aswell, Thumbnails.of(targetLocation.toFile()) .useExifOrientation(true) .size(320, 320) .keepAspectRatio(true) .outputFormat("jpg") .toFiles(Rename.PREFIX_DOT_THUMBNAIL); response.setThumbnailPath("thumbnail." + fileName);