Browse Source

图片压缩调整

seimin 3 months ago
parent
commit
5d65e285f2
3 changed files with 51 additions and 1 deletions
  1. 29 0
      package-lock.json
  2. 1 0
      package.json
  3. 21 1
      tools/uploadFile.js

+ 29 - 0
package-lock.json

@@ -10,6 +10,7 @@
10 10
       "license": "ISC",
11 11
       "dependencies": {
12 12
         "big.js": "^6.2.1",
13
+        "compressorjs": "^1.2.1",
13 14
         "crypto-js": "^4.1.1",
14 15
         "dayjs": "^1.11.10",
15 16
         "js-base64": "^2.6.4",
@@ -57,6 +58,12 @@
57 58
         "url": "https://opencollective.com/bigjs"
58 59
       }
59 60
     },
61
+    "node_modules/blueimp-canvas-to-blob": {
62
+      "version": "3.29.0",
63
+      "resolved": "https://registry.npmmirror.com/blueimp-canvas-to-blob/-/blueimp-canvas-to-blob-3.29.0.tgz",
64
+      "integrity": "sha512-0pcSSGxC0QxT+yVkivxIqW0Y4VlO2XSDPofBAqoJ1qJxgH9eiUDLv50Rixij2cDuEfx4M6DpD9UGZpRhT5Q8qg==",
65
+      "license": "MIT"
66
+    },
60 67
     "node_modules/brace-expansion": {
61 68
       "version": "1.1.11",
62 69
       "resolved": "https://registry.npmmirror.com/brace-expansion/-/brace-expansion-1.1.11.tgz",
@@ -83,6 +90,16 @@
83 90
         "node": ">=10.0.0"
84 91
       }
85 92
     },
93
+    "node_modules/compressorjs": {
94
+      "version": "1.2.1",
95
+      "resolved": "https://registry.npmmirror.com/compressorjs/-/compressorjs-1.2.1.tgz",
96
+      "integrity": "sha512-+geIjeRnPhQ+LLvvA7wxBQE5ddeLU7pJ3FsKFWirDw6veY3s9iLxAQEw7lXGHnhCJvBujEQWuNnGzZcvCvdkLQ==",
97
+      "license": "MIT",
98
+      "dependencies": {
99
+        "blueimp-canvas-to-blob": "^3.29.0",
100
+        "is-blob": "^2.1.0"
101
+      }
102
+    },
86 103
     "node_modules/concat-map": {
87 104
       "version": "0.0.1",
88 105
       "resolved": "https://registry.npmmirror.com/concat-map/-/concat-map-0.0.1.tgz",
@@ -209,6 +226,18 @@
209 226
         "node": ">= 0.10"
210 227
       }
211 228
     },
229
+    "node_modules/is-blob": {
230
+      "version": "2.1.0",
231
+      "resolved": "https://registry.npmmirror.com/is-blob/-/is-blob-2.1.0.tgz",
232
+      "integrity": "sha512-SZ/fTft5eUhQM6oF/ZaASFDEdbFVe89Imltn9uZr03wdKMcWNVYSMjQPFtg05QuNkt5l5c135ElvXEQG0rk4tw==",
233
+      "license": "MIT",
234
+      "engines": {
235
+        "node": ">=6"
236
+      },
237
+      "funding": {
238
+        "url": "https://github.com/sponsors/sindresorhus"
239
+      }
240
+    },
212 241
     "node_modules/is-core-module": {
213 242
       "version": "2.13.1",
214 243
       "resolved": "https://registry.npmmirror.com/is-core-module/-/is-core-module-2.13.1.tgz",

+ 1 - 0
package.json

@@ -15,6 +15,7 @@
15 15
   "license": "ISC",
16 16
   "dependencies": {
17 17
     "big.js": "^6.2.1",
18
+    "compressorjs": "^1.2.1",
18 19
     "crypto-js": "^4.1.1",
19 20
     "dayjs": "^1.11.10",
20 21
     "js-base64": "^2.6.4",

+ 21 - 1
tools/uploadFile.js

@@ -1,4 +1,5 @@
1 1
 import { api_uploadAttachment } from "@/http/http.js"
2
+import Compressor from 'compressorjs';
2 3
 /**
3 4
  * 压缩
4 5
  */
@@ -20,6 +21,24 @@ const newImage = (img) => {
20 21
   })
21 22
 }
22 23
 /**
24
+	 * 压缩图片
25
+	 */
26
+const compressImage = (file) => {
27
+  return new Promise((resolve,reject) => {
28
+    new Compressor(file, {
29
+      quality: 0.6, // 压缩质量,范围从0到1
30
+      maxWidth: 1500, // 最大宽度
31
+      maxHeight: 1500, // 最大高度
32
+      success: (result) => {
33
+        resolve(result); // 压缩后的图片
34
+      },
35
+      error: (err) => {
36
+        reject(err); // 压缩失败的错误信息
37
+      }
38
+    });
39
+  })
40
+}
41
+/**
23 42
  * 上传图片
24 43
  */
25 44
 export const uploadFile = async (imgObj, type, incidentId) => {
@@ -50,7 +69,8 @@ export const uploadFile = async (imgObj, type, incidentId) => {
50 69
     await newImage(img)
51 70
     ctx.drawImage(img, 0, 0, canvasWidth, canvasHeight)
52 71
 
53
-    const fileSrc = await toBlob(canvas, imgObj)
72
+    const blob = await toBlob(canvas, imgObj)
73
+    const fileSrc = await compressImage(blob)
54 74
     let tp = window.URL.createObjectURL(fileSrc)
55 75
     console.log('压缩后', tp);
56 76
     return uni.uploadFile({