学习链接
SpringBoot2.x 整合 thumbnailator 图片处理
用Java给您的图片瘦身之Thumbnailator技术
代码
引入依赖
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.8</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
代码
@Test
public void test001() throws IOException {
/* 按照指定的大小的宽高去生成缩略图, 但是生成的图片长宽比与原图保持不变 */
Thumbnails.of(new File("C:\\Users\\zzhua195\\Desktop\\ps.jpg"))
.size(160, 160)
.toFile(new File("C:\\Users\\zzhua195\\Desktop\\ps_60.jpg"));
}
@Test
public void test002() throws IOException {
/* 生成图片的宽和高都是指定的大小, 从中间缩放, 并且保持比例 */
Thumbnails.of(new File("D:\\Projects\\vue-prism\\src\\assets\\talk\\img10.jpg"))
.size(100, 100)
.crop(Positions.CENTER)
.toFile(new File("D:\\Projects\\vue-prism\\src\\assets\\talk\\[email protected]"));
}