Selaa lähdekoodia

检查2:1图片

wuweihao 3 vuotta sitten
vanhempi
commit
81ca2374e9

+ 22 - 0
720yun_local_manage/gis_common/src/main/java/com/gis/common/util/ImgUtil.java

@@ -5,6 +5,10 @@ import com.gis.common.base.exception.BaseRuntimeException;
 import com.gis.common.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
@@ -44,4 +48,22 @@ public class ImgUtil {
         BaseRuntimeException.isHas(!file, null, "预览图不存在");
 
     }
+
+    /**
+     * 检查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();
+        }
+
+    }
+
+
 }

+ 15 - 2
720yun_local_manage/gis_pano_producer/src/main/java/com/gis/cms/service/impl/ProduceServiceImpl.java

@@ -18,6 +18,7 @@ import org.springframework.stereotype.Service;
 import org.springframework.web.multipart.MultipartFile;
 
 import javax.servlet.http.HttpServletRequest;
+import java.awt.image.BufferedImage;
 import java.io.File;
 import java.io.IOException;
 import java.util.List;
@@ -49,7 +50,7 @@ public class ProduceServiceImpl implements ProducerService {
         Long userId = JwtUtil.getUserId(request.getHeader("token"));
         long start = System.currentTimeMillis();
         // 全景图只支持jpg图片格式
-        checkImgType(file);
+        checkImg(file);
 
         long kb = fileUtils.getSize(file, "kB");
         BaseRuntimeException.isHas((kb/1024) >=120, null, "全景图文件不能超过120MB");
@@ -103,7 +104,11 @@ public class ProduceServiceImpl implements ProducerService {
         return Result.success(fodderMapper.selectList(wrapper));
     }
 
-    private void checkImgType(MultipartFile file){
+    /**
+     * 检查图片类型
+     * @param file
+     */
+    private void checkImg(MultipartFile file){
         // 次方法可以读取到图片内部结构, 可以检验非全景图
         try {
             String imgType = FileTypeUtil.getType(file.getInputStream());
@@ -113,8 +118,16 @@ public class ProduceServiceImpl implements ProducerService {
             boolean jpg = imgType.equalsIgnoreCase("jpg");
             BaseRuntimeException.isHas(!jpg, null,  msg);
 
+            // 检查2:1图片
+            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();
         }
     }
+
+
 }