coobird / thumbnailator

Thumbnailator - a thumbnail generation library for Java
MIT License
5.08k stars 780 forks source link

After resize the image is rotated #205

Closed BaolCristian closed 10 months ago

BaolCristian commented 1 year ago

Expected behavior

Please describe what you are expecting the library to perform. When I resize an image I don't want rotation

Actual behavior

Please describe the actual behavior you are experiencing, including stack trace and other information which would help diagnose the issue. The image is rotated (sometimes +90°, sometimes -90°)

Steps to reproduce the behavior

Please enter step-by-step instructions for reproducing the actual behavior. Including code can be helpful in diagnosing issue, but please keep the code to a minimal that will reproduce the behavior. InputStream is = new BufferedInputStream(img.getBinaryStream()) BufferedImage bi = ImageIO.read(is); def result=Thumbnails.of(bi).size(600, 600).asBufferedImage()

Environment

Please provide vendor and version information for the Operating System, JDK, and Thumbnailator. Please feel free to add any other information which may be pertinent.

I'm using GRAILS 4.0.1

Try with this image: thumbnail_doc01756120221219103145_001

Result

thumbnail_doc01756120221219103145_001 (1)

Maczaj commented 1 year ago

Inspecting with exiftools shows that orientation information is removed as a result.

coobird commented 1 year ago

@BaolCristian, the issue here is that the JPEG file has Exif metadata that contains the orientation information, so the original pixels stored in the image is actually rotated, as seen in the image shrunken by Thumbnailator.

The reason for this is that you're reading the image to a BufferedImage before handing it to Thumbnails.of. Thumbnailator will detect the Exif metadata and apply proper rotation to give the correct orientation as long as the JPEG file is given to Thumbnails.of.

Therefore, if the code was rewritten as:

InputStream is = new BufferedInputStream(img.getBinaryStream())
def result=Thumbnails.of(is).size(600, 600).asBufferedImage()

This should end up with the correct orientation for the thumbnail.

coobird commented 10 months ago

I will be closing this issue as a resolution has been provided.