缩略图开源库 Thumbnailator 的使用

缩略图开源库 Thumbnailator 可根据已有图片,截取指定范围并缩放,还支持添加水印。本文总结了它的用法。

作者:王克锋
出处:https://kefeng.wang/2017/04/09/thumbnailator/
版权:自由转载-非商用-非衍生-保持署名,转载请标明作者和出处。

1 概述

功能需求:对于特定的原始图片,要求输出为 480*360 的图片。算法为:

  • 宽高等比缩放至 width=480(当纵向内容多余时) 或 height=360(当横向内容多余时);
  • 从图片中心点向四周截取 480*360 大小的图片,四边多余内容丢掉;
  • 最好能在右下角打上水印。

2 开源库 thumbnailator(最佳)

特色: 不依赖外部库,轻便高效,任何平台适用,支持缩放、旋转、截取,特别还支持【水印】。
示例: https://github.com/coobird/thumbnailator/wiki/Examples
源码: https://github.com/coobird/thumbnailator
文档: https://coobird.github.io/thumbnailator/javadoc/0.4.8/

2.1 Maven 依赖

Maven: https://mvnrepository.com/artifact/net.coobird/thumbnailator

<dependency>
    <groupId>net.coobird</groupId>
    <artifactId>thumbnailator</artifactId>
    <version>0.4.8</version>
</dependency>

2.2 代码

public boolean createImage(String input, String output) {
    try {
        BufferedImage image = ImageIO.read(new File(input));
        // BufferedImage image = ImageIO.read(imageFile.getInputStream());

        // 确保原图够大
        if (image.getWidth() < DEST_WIDTH || image.getHeight() < DEST_HEIGHT) {
            logger.warn("ImageFile.createImage({}, {}) too small.", image.getWidth(), image.getHeight());
            return false;
        }

        // 按比例缩小至目标尺寸
        Thumbnails.Builder<BufferedImage> builder = Thumbnails.of(image);
        builder.crop(Positions.CENTER).size(DEST_WIDTH, DEST_HEIGHT);

        // 添加水印
        BufferedImage watermarkImage = ImageIO.read(new File(watermarkfile));
        builder.watermark(Positions.BOTTOM_RIGHT, watermarkImage, 0.3f);

        // 保存
        String fileExt = FilenameUtils.getExtension(output);
        builder.outputFormat(fileExt).toFile(output);
        logger.info("ImageFile.createImage(\"{}\") OK.", output);
        return true;
    } catch (IOException e) {
        logger.warn(e.getMessage());
    }

    return false;
}

3 开源库 imgscalr

特色: 全部基于 Java 2D,不依赖外部库,轻便高效,任何平台适用,支持缩放、旋转、截取,不支持水印。
源码: https://github.com/rkalla/imgscalr

3.1 Maven 依赖

Maven: http://mvnrepository.com/artifact/org.imgscalr/imgscalr-lib

<dependency>
    <groupId>org.imgscalr</groupId>
    <artifactId>imgscalr-lib</artifactId>
    <version>4.2</version>
</dependency>

3.2 代码

public boolean createImage(String input, String output) {
    try {
        BufferedImage image = ImageIO.read(new File(input));
        // BufferedImage image = ImageIO.read(imageFile.getInputStream());

        // 确保原图够大
        if (image.getWidth() < DEST_WIDTH || image.getHeight() < DEST_HEIGHT) {
            logger.warn("ImageFile.createImage({}, {}) too small.", image.getWidth(), image.getHeight());
            return false;
        }

        // 按比例缩小至目标尺寸
        int x = 0, y = 0;
        BufferedImage resized = null;
        double widthRate = (double) image.getWidth() / DEST_WIDTH;
        double heightRate = (double) image.getHeight() / DEST_HEIGHT;
        if (widthRate > heightRate) { // 宽度过剩
            resized = Scalr.resize(image, Scalr.Method.QUALITY, Scalr.Mode.FIT_TO_HEIGHT, DEST_WIDTH, DEST_HEIGHT, Scalr.OP_ANTIALIAS);
            x = (resized.getWidth() - DEST_WIDTH) / 2;
        } else { // 高度过剩
            resized = Scalr.resize(image, Scalr.Method.QUALITY, Scalr.Mode.FIT_TO_WIDTH, DEST_WIDTH, DEST_HEIGHT, Scalr.OP_ANTIALIAS);
            y = (resized.getHeight() - DEST_HEIGHT) / 2;
        }

        // 截取中间部分
        BufferedImage thumbnail = Scalr.crop(resized, x, y, DEST_WIDTH, DEST_HEIGHT);

        // 保存
        String fileExt = FilenameUtils.getExtension(output);
        ImageIO.write(thumbnail, fileExt, new File(output));
        logger.info("ImageFile.createImage(\"{}\") OK.", output);
        return true;
    } catch (IOException e) {
        logger.warn(e.getMessage());
    }

    return false;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Swiper 7中,你可以使用缩略图功能来实现轮播图和缩略图的联动效果。下面是一个示例代码,演示了如何在Swiper 7中使用缩略图: ```html <div class="swiper-container"> <div class="swiper-wrapper"> <!-- 轮播图的内容 --> <div class="swiper-slide">Slide 1</div> <div class="swiper-slide">Slide 2</div> <div class="swiper-slide">Slide 3</div> <div class="swiper-slide">Slide 4</div> <div class="swiper-slide">Slide 5</div> </div> <div class="swiper-pagination"></div> </div> <div class="swiper-container-thumbnails"> <div class="swiper-wrapper"> <!-- 缩略图的内容 --> <div class="swiper-slide"><img src="thumbnail1.jpg" alt=""></div> <div class="swiper-slide"><img src="thumbnail2.jpg" alt=""></div> <div class="swiper-slide"><img src="thumbnail3.jpg" alt=""></div> <div class="swiper-slide"><img src="thumbnail4.jpg" alt=""></div> <div class="swiper-slide"><img src="thumbnail5.jpg" alt=""></div> </div> </div> <script> const swiper = new Swiper('.swiper-container', { // 轮播图的配置 pagination: { el: '.swiper-pagination', }, thumbs: { swiper: { el: '.swiper-container-thumbnails', slidesPerView: 5, // 缩略图一次展示的数量 }, }, }); </script> ``` 在上面的代码中,我们使用了两个Swiper实例,一个用于轮播图,一个用于缩略图。通过`thumbs`选项,我们将缩略图Swiper实例与轮播图Swiper实例关联起来。在缩略图Swiper实例中,我们设置了`slidesPerView`选项来指定一次展示的缩略图数量。 你可以根据自己的需求修改轮播图和缩略图的内容和样式,以及配置其他的Swiper选项来实现更多的效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值