1. Thumbnails 对图片进行先缩放,再剪裁
Thumbnails 首先对图片进行缩放,缩放之后再进行剪裁出想要的大小
Thumbnails.of(inputStream).size(width,height).addFilter(new Canvas(width, height, Positions.CENTER)).outputQuality(0.8f).toOutputStream(response.getOutputStream());
主要是new Canvas(width, height, Positions.CENTER) 这个做剪裁
参考: https://github.com/coobird/thumbnailator/issues/22
关键原文:
If what is desired is to merely crop an image, Thumbnailator does not provide a
direct way to do so, however there is a `Canvas` image filter which can perform
image cropping. (Please refer the javadocs at
http://thumbnailator.googlecode.com/hg/javadoc/net/coobird/thumbnailator/filters
/Canvas.html)
> 3. I will add empty area around to have exactly 100x100
The aforementioned `Canvas` image filter has a feature to forcefully make
thumbnails a certain size.
For example, say we take a 200x400 image and scale it down to 50x100, then we
want to have an empty area to make a 100x100 thumbnail. The code can be written
as follows:
Thumbnails.of("path/to/image-200x400")
.size(100, 100)
.addFilter(new Canvas(100, 100, Positions.CENTER))
.toFile("path/to/thumbnail");
Note: `size(100, 100)` will preserve the aspect ratio of the original, so the
thumbnail without the `Canvas` filter would be 50x100.
https://github.com/springcloud-zym/minio-util