vue+java实现图片批量压缩

最近做了一个功能批量图片压缩,在不对接第三方api的条件下应该怎么做

先看看效果:https://office.zjdiante.com/imageTool/compress

1.安装 ImageMagick,我用的版本是7

2.后端代码工具类

java 复制代码
package com.ruoyi.basicTool.imageTool.utils;

import cn.hutool.core.io.IoUtil;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.TimeUnit;

/**
 * 调用本机 ImageMagick 7 命令行(默认可执行文件名为 {@code magick})。
 * <p>解析顺序:系统属性 {@code imagemagick.binary}(可由 {@code application.yml} 中 {@code imagemagick.binary} 在启动时注入)
 * → 环境变量 {@code IMAGEMAGICK_BINARY} → Windows 下默认 {@code magick.exe} → 其他系统默认 {@code magick}。</p>
 * <p>Windows 若未将安装目录加入 PATH,请将 {@code imagemagick.binary} 设为 {@code magick.exe} 的绝对路径,例如
 * {@code C:/Program Files/ImageMagick-7.1.1-Q16-HDRI/magick.exe}。</p>
 */
public final class ImageMagickRunUtils {

    private static final int MAGICK_TIMEOUT_SECONDS = 120;

    private ImageMagickRunUtils() {
    }

    public static String magickBinary() {
        String p = System.getProperty("imagemagick.binary");
        if (StringUtils.isNotEmpty(p)) {
            return p.trim();
        }
        String env = System.getenv("IMAGEMAGICK_BINARY");
        if (StringUtils.isNotEmpty(env)) {
            return env.trim();
        }
        if (System.getProperty("os.name", "").toLowerCase(Locale.ROOT).contains("windows")) {
            return "magick.exe";
        }
        return "magick";
    }

    public static void runMagick(Path expectedOut, List<String> cmd) throws Exception {
        ProcessBuilder pb = new ProcessBuilder(cmd);
        pb.redirectErrorStream(true);
        Process proc;
        try {
            proc = pb.start();
        } catch (IOException e) {
            if (e.getMessage() != null && (e.getMessage().contains("error=2") || e.getMessage().contains("Cannot run program"))) {
                throw new IOException(
                        "无法启动 ImageMagick(进程找不到可执行文件)。CMD 能运行 magick 不代表 JVM 的 PATH 相同;"
                                + "请在 CMD 执行 where magick 得到 magick.exe 绝对路径,写入 application.yml 的 imagemagick.binary,"
                                + "或设置环境变量 IMAGEMAGICK_BINARY。",
                        e);
            }
            throw e;
        }
        String output;
        try (InputStream es = proc.getInputStream()) {
            if (!proc.waitFor(MAGICK_TIMEOUT_SECONDS, TimeUnit.SECONDS)) {
                proc.destroyForcibly();
                throw new IOException("ImageMagick 执行超时");
            }
            output = IoUtil.readUtf8(es);
        }
        if (proc.exitValue() != 0) {
            throw new IOException("ImageMagick 失败: " + truncate(output, 800));
        }
        if (!Files.exists(expectedOut) || Files.size(expectedOut) == 0) {
            throw new IOException("ImageMagick 未生成有效输出: " + truncate(output, 800));
        }
    }

    private static String truncate(String s, int max) {
        if (s == null) {
            return "";
        }
        String t = s.replace("\r", " ").replace("\n", " ").trim();
        return t.length() <= max ? t : t.substring(0, max) + "...";
    }
}

有想要做类似不明白的小伙伴,可以点开:https://office.zjdiante.com/ 联系我

相关推荐
PedroQue997 分钟前
Vite插件v0.2.6:架构优化与自动化升级
前端·vite
threerocks2 小时前
什么?我连 A2A、MCP 都没学会,现在又来了 AG-UI、A2UI.
前端·aigc·ai编程
牛奶2 小时前
如何自己写一个浏览器插件?
前端·chrome·浏览器
亿元程序员3 小时前
为什么Cocos都4.0了还有人用2.x?
前端
MomentYY3 小时前
AI 到底是“懂”,还是在“猜”?
前端·人工智能·ai编程
鹏毓网络科技3 小时前
Cursor Rules 文件配置实战:3 个隐藏参数让我每月少写 40% 样板代码
前端·github
没烦恼3013 小时前
无痕模式下 HTTP\-First 拦截引发的“页面刷新”误判
前端
ZhengEnCi3 小时前
Q02-Vue-React-index.html完全指南
vue.js·react.js·html
文心快码BaiduComate3 小时前
从个人提效到组织提效:Comate辅助构建自我进化的AI研发系统
前端·程序员
未秃头的程序猿3 小时前
Java 26正式发布!这3个新特性,让代码量直接减半
java·后端·面试