Ver Fonte

图片压缩

xiongzhu há 4 anos atrás
pai
commit
e18aace3b0

+ 50 - 3
src/main/java/com/izouma/nineth/web/FileUploadController.java

@@ -124,8 +124,12 @@ public class FileUploadController {
     }
 
     @PostMapping("/fileObject")
-    public FileObject uploadFileObject(@RequestParam("file") MultipartFile file) throws IOException {
-        String ext = Optional.ofNullable(FilenameUtils.getExtension(file.getOriginalFilename())).orElse("");
+    public FileObject uploadFileObject(@RequestParam("file") MultipartFile file,
+                                       @RequestParam(value = "compress", defaultValue = "false") boolean compress,
+                                       @RequestParam(value = "width", required = false) Integer width,
+                                       @RequestParam(value = "height", required = false) Integer height) throws IOException {
+        String ext = Optional.ofNullable(FilenameUtils.getExtension(file.getOriginalFilename())).orElse("")
+                .toLowerCase().replace("jpeg", "jpg");
         String[] extList = new String[]{"jpg", "jpeg", "png", "gif", "mp4"};
         if (!ArrayUtils.contains(extList, ext.toLowerCase())) {
             throw new BusinessException("仅支持" + StringUtils.join(extList, "、") + "格式");
@@ -135,8 +139,51 @@ public class FileUploadController {
         String path = "nft/" + new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date())
                 + RandomStringUtils.randomAlphabetic(8)
                 + "." + ext;
-        String url = storageService.uploadFromInputStream(new FileInputStream(tmpFile), path);
+        String url;
+
+
+        if (Pattern.matches("(jpg|png)", ext) && compress) {
+            if (width == null && height == null) {
+                width = Integer.MAX_VALUE;
+                height = Integer.MAX_VALUE;
+            } else if (height == null) {
+                height = Integer.MAX_VALUE;
+            } else if (width == null) {
+                width = Integer.MAX_VALUE;
+            }
+            BufferedImage img = null;
+            if ("jpg".equals(ext)) {
+                img = ImageUtils.resizeJpg(new FileInputStream(tmpFile), width, height);
+            } else {
+                img = ImageUtils.resizePng(new FileInputStream(tmpFile), width, height, true);
+            }
+            ByteArrayOutputStream os = new ByteArrayOutputStream();
+            ImageIO.write(img, ext, os);
+            InputStream is = new ByteArrayInputStream(os.toByteArray());
+            url = storageService.uploadFromInputStream(is, path);
+        } else if (width != null || height != null) {
+            if (height == null) {
+                height = Integer.MAX_VALUE;
+            } else if (width == null) {
+                width = Integer.MAX_VALUE;
+            }
+            BufferedImage img = null;
+            if ("jpg".equals(ext)) {
+                img = ImageUtils.resizeJpg(new FileInputStream(tmpFile), width, height);
+            } else {
+                img = ImageUtils.resizePng(new FileInputStream(tmpFile), width, height, false);
+            }
+            ByteArrayOutputStream os = new ByteArrayOutputStream();
+            ImageIO.write(img, ext, os);
+            InputStream is = new ByteArrayInputStream(os.toByteArray());
+            url = storageService.uploadFromInputStream(is, path);
+        } else {
+            url = storageService.uploadFromInputStream(new FileInputStream(tmpFile), path);
+        }
+
         String thumbUrl = null;
+
+
         if ("mp4".equalsIgnoreCase(ext)) {
             FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber(tmpFile);
             frameGrabber.start();

+ 21 - 2
src/main/vue/src/components/ObjectUpload.vue

@@ -24,7 +24,7 @@
     <el-upload
         v-else
         class="avatar-uploader"
-        :action="uploadUrl"
+        :action="finalUploadUrl"
         :show-file-list="false"
         :on-success="onSuccess"
         :before-upload="beforeUpload"
@@ -38,13 +38,20 @@
 </template>
 <script>
 import resolveUrl from 'resolve-url';
+import qs from 'qs';
 export default {
     props: {
         value: {},
         disabled: {
             default: false,
             type: Boolean
-        }
+        },
+        compress: {
+            default: false,
+            type: Boolean
+        },
+        width: {},
+        height: {}
     },
     data() {
         return {
@@ -57,6 +64,18 @@ export default {
         this.uploadUrl = resolveUrl(this.$baseUrl, 'upload/fileObject');
         this.fileObject = this.value || {};
     },
+    computed: {
+        finalUploadUrl() {
+            let data = { compress: this.compress };
+            if (this.width) {
+                data.width = this.width;
+            }
+            if (this.height) {
+                data.height = this.height;
+            }
+            return this.uploadUrl + '?' + qs.stringify(data);
+        }
+    },
     methods: {
         onSuccess(e) {
             console.log(e);

+ 1 - 1
src/main/vue/src/views/CollectionEdit.vue

@@ -20,7 +20,7 @@
                         <el-input v-model="formData.name" :disabled="!canEdit"></el-input>
                     </el-form-item>
                     <el-form-item prop="pics" label="图片">
-                        <object-upload v-model="formData.pics[0]" :disabled="!canEdit"></object-upload>
+                        <object-upload v-model="formData.pics[0]" :disabled="!canEdit" compress></object-upload>
                         <div class="tip">支持JPG、PNG、GIF、MP4,推荐长宽比1:1</div>
                     </el-form-item>
                     <el-form-item prop="minterId" label="铸造者">