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
][1] 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:
- Open the
original.jpg
, - Use the central 300 x 300 region of the original image, and
- Resize that region to a 160 x 160 thumbnail, and
- Writes to the
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)
][4] method- Used to specify an exact region from which to create a thumbnail.
- [
sourceRegion(Position, int, int)
][5] method- Uses relative positioning using the
Position
object as shown in the example code above.
- Uses relative positioning using the
sourceRegion(Rectangle)
method- Used to specify an exact region from which to create a thumbnail, using a
java.awt.Rectangle
object.
- Used to specify an exact region from which to create a thumbnail, using a
Position
enum- Provides pre-defined positions which can be used to specify the relative position of the region from which to create a thumbnail.
Disclaimer: I am the maintainer of the Thumbnailator library.
[1]: https://web.archive.org/web/20160511015754/http://thumbnailator.googlecode.com:80/hg/javadoc/net/coobird/thumbnailator/Thumbnails.Builder.html#sourceRegion(net.coobird.thumbnailator.geometry.Position, int, int) [4]: https://web.archive.org/web/20160511015754/http://thumbnailator.googlecode.com:80/hg/javadoc/net/coobird/thumbnailator/Thumbnails.Builder.html#sourceRegion(int, int, int, int) [5]: https://web.archive.org/web/20160511015754/http://thumbnailator.googlecode.com:80/hg/javadoc/net/coobird/thumbnailator/Thumbnails.Builder.html#sourceRegion(net.coobird.thumbnailator.geometry.Position, int, int)
来源:https://stackoverflow.com/questions/9579004/using-thumbnailator-can-i-make-thumbnail-with-same-height-and-width-regardless