Thumbnailator缩略图处理

该文章介绍了如何在SpringBoot项目中使用thumbnailator库进行图片处理,包括按比例生成缩略图和居中裁剪以适应指定尺寸。示例代码展示了创建160x160和100x100尺寸图片的方法。
摘要由CSDN通过智能技术生成

学习链接

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\\img10@100x100.jpg"));
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值