refactor: 根据图片大小设置压缩比优化

This commit is contained in:
郝先瑞 2022-07-18 00:13:37 +08:00
parent 9a6c44ee42
commit 77c61b7bde
2 changed files with 13 additions and 7 deletions

View File

@ -36,18 +36,22 @@ public class ImgUtils {
/**
* 根据图片大小获取压缩比
* 根据图片大小设置压缩比
*
* @param size 图片大小(单位Bytes)
* @return 压缩比例
*/
public static float getCompressQuality(long size) {
if (size <= 0.1 * 1024 * 1024) {
return 0.5f;
} else if (size > 0.1 * 1024 * 1024 && size <= 1 * 1024 * 1024) {
return 0.3f;
if (size > 0.1 * 1024 * 1024 && size <= 0.5 * 1024 * 1024) {
return 0.8f;
} else if (size > 0.5 * 1024 * 1024 && size <= 1 * 1024 * 1024) {
return 0.6f;
} else if (size > 1 * 1024 * 1024 && size <= 2 * 1024 * 1024) {
return 0.4f;
} else if (size > 2 * 1024 * 1024 && size <= 5 * 1024 * 1024) {
return 0.2f;
} else {
// 大于1M
// 大于5M
return 0.1f;
}
}

View File

@ -19,7 +19,9 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.time.LocalDateTime;