Sfoglia il codice sorgente

图片压缩调整

seimin 3 mesi fa
parent
commit
ad7f47ce8f
4 ha cambiato i file con 79 aggiunte e 1 eliminazioni
  1. 28 0
      node_modules/.package-lock.json
  2. 29 0
      package-lock.json
  3. 1 0
      package.json
  4. 21 1
      share/useUploadFile.js

+ 28 - 0
node_modules/.package-lock.json

@@ -27,6 +27,12 @@
27 27
         "tweetnacl": "^0.14.3"
28 28
       }
29 29
     },
30
+    "node_modules/blueimp-canvas-to-blob": {
31
+      "version": "3.29.0",
32
+      "resolved": "https://registry.npmmirror.com/blueimp-canvas-to-blob/-/blueimp-canvas-to-blob-3.29.0.tgz",
33
+      "integrity": "sha512-0pcSSGxC0QxT+yVkivxIqW0Y4VlO2XSDPofBAqoJ1qJxgH9eiUDLv50Rixij2cDuEfx4M6DpD9UGZpRhT5Q8qg==",
34
+      "license": "MIT"
35
+    },
30 36
     "node_modules/brace-expansion": {
31 37
       "version": "1.1.11",
32 38
       "resolved": "https://registry.npmmirror.com/brace-expansion/-/brace-expansion-1.1.11.tgz",
@@ -43,6 +49,16 @@
43 49
       "integrity": "sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==",
44 50
       "dev": true
45 51
     },
52
+    "node_modules/compressorjs": {
53
+      "version": "1.2.1",
54
+      "resolved": "https://registry.npmmirror.com/compressorjs/-/compressorjs-1.2.1.tgz",
55
+      "integrity": "sha512-+geIjeRnPhQ+LLvvA7wxBQE5ddeLU7pJ3FsKFWirDw6veY3s9iLxAQEw7lXGHnhCJvBujEQWuNnGzZcvCvdkLQ==",
56
+      "license": "MIT",
57
+      "dependencies": {
58
+        "blueimp-canvas-to-blob": "^3.29.0",
59
+        "is-blob": "^2.1.0"
60
+      }
61
+    },
46 62
     "node_modules/concat-map": {
47 63
       "version": "0.0.1",
48 64
       "resolved": "https://registry.npmmirror.com/concat-map/-/concat-map-0.0.1.tgz",
@@ -156,6 +172,18 @@
156 172
         "node": ">= 0.10"
157 173
       }
158 174
     },
175
+    "node_modules/is-blob": {
176
+      "version": "2.1.0",
177
+      "resolved": "https://registry.npmmirror.com/is-blob/-/is-blob-2.1.0.tgz",
178
+      "integrity": "sha512-SZ/fTft5eUhQM6oF/ZaASFDEdbFVe89Imltn9uZr03wdKMcWNVYSMjQPFtg05QuNkt5l5c135ElvXEQG0rk4tw==",
179
+      "license": "MIT",
180
+      "engines": {
181
+        "node": ">=6"
182
+      },
183
+      "funding": {
184
+        "url": "https://github.com/sponsors/sindresorhus"
185
+      }
186
+    },
159 187
     "node_modules/is-core-module": {
160 188
       "version": "2.13.1",
161 189
       "resolved": "https://registry.npmmirror.com/is-core-module/-/is-core-module-2.13.1.tgz",

+ 29 - 0
package-lock.json

@@ -5,6 +5,7 @@
5 5
   "packages": {
6 6
     "": {
7 7
       "dependencies": {
8
+        "compressorjs": "^1.2.1",
8 9
         "crypto-js": "^4.2.0",
9 10
         "date-fns": "^3.6.0",
10 11
         "lodash-es": "^4.17.21",
@@ -40,6 +41,12 @@
40 41
         "tweetnacl": "^0.14.3"
41 42
       }
42 43
     },
44
+    "node_modules/blueimp-canvas-to-blob": {
45
+      "version": "3.29.0",
46
+      "resolved": "https://registry.npmmirror.com/blueimp-canvas-to-blob/-/blueimp-canvas-to-blob-3.29.0.tgz",
47
+      "integrity": "sha512-0pcSSGxC0QxT+yVkivxIqW0Y4VlO2XSDPofBAqoJ1qJxgH9eiUDLv50Rixij2cDuEfx4M6DpD9UGZpRhT5Q8qg==",
48
+      "license": "MIT"
49
+    },
43 50
     "node_modules/brace-expansion": {
44 51
       "version": "1.1.11",
45 52
       "resolved": "https://registry.npmmirror.com/brace-expansion/-/brace-expansion-1.1.11.tgz",
@@ -66,6 +73,16 @@
66 73
         "node": ">=10.0.0"
67 74
       }
68 75
     },
76
+    "node_modules/compressorjs": {
77
+      "version": "1.2.1",
78
+      "resolved": "https://registry.npmmirror.com/compressorjs/-/compressorjs-1.2.1.tgz",
79
+      "integrity": "sha512-+geIjeRnPhQ+LLvvA7wxBQE5ddeLU7pJ3FsKFWirDw6veY3s9iLxAQEw7lXGHnhCJvBujEQWuNnGzZcvCvdkLQ==",
80
+      "license": "MIT",
81
+      "dependencies": {
82
+        "blueimp-canvas-to-blob": "^3.29.0",
83
+        "is-blob": "^2.1.0"
84
+      }
85
+    },
69 86
     "node_modules/concat-map": {
70 87
       "version": "0.0.1",
71 88
       "resolved": "https://registry.npmmirror.com/concat-map/-/concat-map-0.0.1.tgz",
@@ -194,6 +211,18 @@
194 211
         "node": ">= 0.10"
195 212
       }
196 213
     },
214
+    "node_modules/is-blob": {
215
+      "version": "2.1.0",
216
+      "resolved": "https://registry.npmmirror.com/is-blob/-/is-blob-2.1.0.tgz",
217
+      "integrity": "sha512-SZ/fTft5eUhQM6oF/ZaASFDEdbFVe89Imltn9uZr03wdKMcWNVYSMjQPFtg05QuNkt5l5c135ElvXEQG0rk4tw==",
218
+      "license": "MIT",
219
+      "engines": {
220
+        "node": ">=6"
221
+      },
222
+      "funding": {
223
+        "url": "https://github.com/sponsors/sindresorhus"
224
+      }
225
+    },
197 226
     "node_modules/is-core-module": {
198 227
       "version": "2.13.1",
199 228
       "resolved": "https://registry.npmmirror.com/is-core-module/-/is-core-module-2.13.1.tgz",

+ 1 - 0
package.json

@@ -3,6 +3,7 @@
3 3
     "production": "node ./upload/production.js"
4 4
   },
5 5
   "dependencies": {
6
+    "compressorjs": "^1.2.1",
6 7
     "crypto-js": "^4.2.0",
7 8
     "date-fns": "^3.6.0",
8 9
     "lodash-es": "^4.17.21",

+ 21 - 1
share/useUploadFile.js

@@ -1,4 +1,5 @@
1 1
 import { api_uploadAttachment } from "@/http/api.js"
2
+import Compressor from 'compressorjs';
2 3
 export function useUploadFile() {
3 4
   /**
4 5
    * 压缩
@@ -20,6 +21,24 @@ export function useUploadFile() {
20 21
       }
21 22
     })
22 23
   }
24
+	/**
25
+	 * 压缩图片
26
+	 */
27
+	const compressImage = (file) => {
28
+		return new Promise((resolve,reject) => {
29
+			new Compressor(file, {
30
+				quality: 0.6, // 压缩质量,范围从0到1
31
+				maxWidth: 1500, // 最大宽度
32
+				maxHeight: 1500, // 最大高度
33
+				success: (result) => {
34
+					resolve(result); // 压缩后的图片
35
+				},
36
+				error: (err) => {
37
+					reject(err); // 压缩失败的错误信息
38
+				}
39
+			});
40
+		})
41
+	}
23 42
   /**
24 43
    * 上传图片
25 44
    */
@@ -50,7 +69,8 @@ export function useUploadFile() {
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({