From 277faba22c9b19ceb4be57965c04001351005fed Mon Sep 17 00:00:00 2001 From: zhangheng <3226558941@qq.com> Date: Thu, 22 Aug 2024 15:20:26 +0800 Subject: [PATCH] =?UTF-8?q?=E7=B4=A0=E6=9D=90=E5=9B=BE=E7=89=87=E4=BF=AE?= =?UTF-8?q?=E5=BB=BA=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xinelu/common/utils/file/ImageResize.java | 41 +++++++++++++++++++ .../impl/FileUplooadServiceImpl.java | 8 +++- 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 postdischarge-common/src/main/java/com/xinelu/common/utils/file/ImageResize.java diff --git a/postdischarge-common/src/main/java/com/xinelu/common/utils/file/ImageResize.java b/postdischarge-common/src/main/java/com/xinelu/common/utils/file/ImageResize.java new file mode 100644 index 00000000..cf9f762c --- /dev/null +++ b/postdischarge-common/src/main/java/com/xinelu/common/utils/file/ImageResize.java @@ -0,0 +1,41 @@ +package com.xinelu.common.utils.file; + +import com.xinelu.common.config.SystemBusinessConfig; +import com.xinelu.common.constant.Constants; + +import javax.imageio.ImageIO; +import java.awt.*; +import java.awt.image.BufferedImage; +import java.io.File; + +/** + * 图片裁剪 + * + * @author: zh + * @create: 2024-08-22 + **/ +public class ImageResize { + + public static void scaledDown(String pictureName) { + try { + pictureName = pictureName.replaceAll(Constants.RESOURCE_PREFIX, SystemBusinessConfig.getProfile()); + File inputFile = new File(pictureName); + BufferedImage inputImage = ImageIO.read(inputFile); + if (inputImage.getWidth() < 1080) { + return; + } + // 新的宽度 + int newWidth = 1080; + int newHeight = (int) ((double) inputImage.getHeight() / inputImage.getWidth() * newWidth); + // 计算新的高度 + BufferedImage outputImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB); + Graphics2D g2d = outputImage.createGraphics(); + g2d.drawImage(inputImage, 0, 0, newWidth, newHeight, null); + g2d.dispose(); + File outputFile = new File(pictureName); + ImageIO.write(outputImage, "jpg", outputFile); + } catch (Exception e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/postdischarge-manage/src/main/java/com/xinelu/manage/service/signpatientinformed/impl/FileUplooadServiceImpl.java b/postdischarge-manage/src/main/java/com/xinelu/manage/service/signpatientinformed/impl/FileUplooadServiceImpl.java index bc55d8df..7e906b67 100644 --- a/postdischarge-manage/src/main/java/com/xinelu/manage/service/signpatientinformed/impl/FileUplooadServiceImpl.java +++ b/postdischarge-manage/src/main/java/com/xinelu/manage/service/signpatientinformed/impl/FileUplooadServiceImpl.java @@ -5,13 +5,15 @@ import com.xinelu.common.constant.FileUploadPathConstants; import com.xinelu.common.core.domain.AjaxResult; import com.xinelu.common.exception.ServiceException; import com.xinelu.common.utils.file.FileUploadUtils; +import com.xinelu.common.utils.file.ImageResize; import com.xinelu.common.utils.file.MimeTypeUtils; import com.xinelu.manage.service.signpatientinformed.IFileUploadService; -import javax.annotation.Resource; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; +import javax.annotation.Resource; + /** * @description: 文件上传Service业务层处理 * @author: haown @@ -63,6 +65,10 @@ public class FileUplooadServiceImpl implements IFileUploadService { if (StringUtils.isBlank(pictureName)) { throw new ServiceException("文件上传失败,请联系管理员!"); } + if (type.equals(FileUploadPathConstants.MATERIALS_COVER_TYPE)){ + //裁剪图片 + ImageResize.scaledDown(pictureName); + } AjaxResult ajax = AjaxResult.success("上传成功!"); ajax.put("imgUrl", pictureName); return ajax;