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/ 联系我

相关推荐
古法老登瞎折腾5 小时前
大数据智能运维实战(十一):运维 MCP 的工程化——权限、限流、审计与错误处理
java·大数据·运维·spring ai·mcp
康小汪5 小时前
AI 写 Vue 总写成同一个味道:给它装 Skills,装的不是知识是边界
前端·vue.js·人工智能·ai编程·最佳实践
智能运维指南6 小时前
研制过程“两张皮“怎么破:研制一体化支撑平台的定位、能力边界与落地前提
java·前端·人工智能·嘉为蓝鲸
yz_aiks7 小时前
Spring AI 2.1 ChatMemory 已过时用什么替代:迁移到 Session Memory 实战(JDBC 存储与会话压缩)
java·spring boot·大模型·ai agent·spring ai
金銀銅鐵7 小时前
[Java] 用 GUI 展示 EnumMap 增删键值对时的内部变化
java·后端·ai编程
学长毕设7 小时前
基于SpringBoot的上门洗车服务系统设计与实现
java·spring boot·后端
jason成都7 小时前
RTKLIB-Java 多基线联合RTK:FIX率从0%到91.5%的三层根因修复
java·gnss·rtklib·rtklib_java
鱼宵7 小时前
Stream 终端操作一次讲透:collect、count、reduce 怎么选
java·stream·终端操作
白山编程大哥7 小时前
Java 后端如何优雅地把 List 集合返回给前端
java·前端·list
Linux运维技术栈8 小时前
CVE-2026-91403 高危漏洞修复:Rocky Linux 9 多版本 JDK 环境清理实操
java·linux·运维