素材图片修建修改
This commit is contained in:
parent
06e6109018
commit
277faba22c
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user