In Thumbnailator, i am making thumbnails.
If image size is 400*300 and if i do following thing,
Thumbnails.of(new File("original.jpg"))
.size(160, 160)
.toFile(new File("thumbnail.jpg"));
it create thumbnail of 160*120.
What i want is if i upload 400*300 image, it will center zoom so that i will become 300*300 and then it will thumbnail.
I gone through the documentation, Even i posted same thing over there in comment but no luck.
Sounds like a job for the sourceRegion
method which can be used to specify the region from which the thumbnail should be produced:
In your particular case, you'll want to try the following:
Thumbnails.of(new File("original.jpg"))
.sourceRegion(Positions.CENTER, 300, 300)
.size(160, 160)
.toFile(new File("thumbnail.jpg"));
The above code will:
original.jpg
,thumbnail.jpg
.It's possible to select different regions of the original image by changing Positions.CENTER
to, for example, Positions.TOP_LEFT
. For a complete list of pre-defined choices, please look at the documentation for the Positions
enum.
The following are some links to the Thumbnailator API documentation which may be of interest:
sourceRegion(int, int, int, int)
method
sourceRegion(Position, int, int)
method
Position
object as shown in the example code above.sourceRegion(Rectangle)
method
java.awt.Rectangle
object.Position
enum
Disclaimer: I am the maintainer of the Thumbnailator library.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With