|
@@ -0,0 +1,68 @@
|
|
|
+package com.gis.util;
|
|
|
+
|
|
|
+import cn.hutool.core.io.FileUtil;
|
|
|
+import com.gis.constant.CmdConstant;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
+import java.io.IOException;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by owen on 2022/3/23 0023 17:48
|
|
|
+ *
|
|
|
+ * 图片工具类
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+public class ImgUtil {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 202-03-27
|
|
|
+ * 压缩图片 使用convert 工具
|
|
|
+ * 固定名称: 名称固定是xxx.jpg
|
|
|
+ * @param inPath
|
|
|
+ * @param outFileName /aa/xxx.jpg
|
|
|
+ * @param width 宽
|
|
|
+ * @param height 高
|
|
|
+ * @return 完整的oss访问地址
|
|
|
+ */
|
|
|
+ public static void compressImg(String inPath, String outFileName, int width, int height){
|
|
|
+ String basePath = StringUtils.substringBeforeLast(inPath, "/");
|
|
|
+ // 保存图片位置
|
|
|
+ String saveCompressImgPath = basePath + outFileName;
|
|
|
+
|
|
|
+ // 使用convert压缩图片
|
|
|
+ String cmd = CmdConstant.CONVERT;
|
|
|
+ String size = width + "x" + height;
|
|
|
+ cmd = cmd.replace("@size", size);
|
|
|
+ cmd = cmd.replace("@input", inPath);
|
|
|
+ cmd = cmd.replace("@output", saveCompressImgPath);
|
|
|
+
|
|
|
+ // 开始压缩
|
|
|
+ CmdUtils.callCmd(cmd);
|
|
|
+ log.info("压缩图片保存位置: {}", saveCompressImgPath);
|
|
|
+ boolean file = FileUtil.isFile(saveCompressImgPath);
|
|
|
+ BaseRuntimeException.isHas(!file, null, "压缩图片不存在:" + saveCompressImgPath);
|
|
|
+ log.info("压缩图片完成");
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 检查2:1图片
|
|
|
+ * @param file
|
|
|
+ */
|
|
|
+ public static void checkImgRatio(MultipartFile file){
|
|
|
+ try {
|
|
|
+ BufferedImage read = cn.hutool.core.img.ImgUtil.read(file.getInputStream());
|
|
|
+ int width = read.getWidth();
|
|
|
+ int height = read.getHeight();
|
|
|
+ BaseRuntimeException.isHas(!(width/height == 2), null, "非2:1图片");
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|