coobird / thumbnailator

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

Add crop method usage to examples #211

Open aalster opened 8 months ago

aalster commented 8 months ago

It is a common use-case to crop an image to fit thumbnail size without leaving the gaps on sides. Currently examples page says

NOTE: Using this approach, the thumbnail will not necessarily be 200 pixel by 200 pixel dimensions

but doesn't provide a solution to force size keeping original aspect ratio.

Please add this example to Forcing thumbnails to be exactly the specified size section:

BufferedImage originalImage = ImageIO.read(new File("original.png"));

BufferedImage thumbnail = Thumbnails.of(originalImage)
        .crop(Positions.CENTER)
        .size(200, 200)
        .asBufferedImage();

This is a great feature of library but it is not easy to spot. Initially I was calculating sizes myself which required me to read bytes as BufferedImage to get original image size and then read bytes second time to make library determine output format automatically.

coobird commented 7 months ago

Thank you for the suggestion! That's a great idea!

sparkwan commented 6 months ago

I also need this feature @coobird. Another feature: crop an transparent/white backgroud image automatically using a specify margin at orignal aspect ratio? The basic idea is that to find the start and end of X/Y cordinate by comparing pixel