sculp-js 1.10.2 → 1.10.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. package/lib/cjs/array.js +1 -1
  2. package/lib/cjs/async.js +1 -1
  3. package/lib/cjs/base64.js +1 -1
  4. package/lib/cjs/clipboard.js +1 -1
  5. package/lib/cjs/cloneDeep.js +1 -1
  6. package/lib/cjs/cookie.js +1 -1
  7. package/lib/cjs/date.js +1 -1
  8. package/lib/cjs/dom.js +1 -1
  9. package/lib/cjs/download.js +1 -1
  10. package/lib/cjs/easing.js +1 -1
  11. package/lib/cjs/file.js +41 -20
  12. package/lib/cjs/func.js +1 -1
  13. package/lib/cjs/index.js +1 -1
  14. package/lib/cjs/isEqual.js +1 -1
  15. package/lib/cjs/math.js +1 -1
  16. package/lib/cjs/number.js +1 -1
  17. package/lib/cjs/object.js +1 -1
  18. package/lib/cjs/path.js +1 -1
  19. package/lib/cjs/qs.js +1 -1
  20. package/lib/cjs/random.js +1 -1
  21. package/lib/cjs/string.js +1 -1
  22. package/lib/cjs/tooltip.js +1 -1
  23. package/lib/cjs/tree.js +1 -1
  24. package/lib/cjs/type.js +1 -1
  25. package/lib/cjs/unique.js +1 -1
  26. package/lib/cjs/url.js +1 -1
  27. package/lib/cjs/validator.js +1 -1
  28. package/lib/cjs/variable.js +1 -1
  29. package/lib/cjs/watermark.js +1 -1
  30. package/lib/cjs/we-decode.js +1 -1
  31. package/lib/es/array.js +1 -1
  32. package/lib/es/async.js +1 -1
  33. package/lib/es/base64.js +1 -1
  34. package/lib/es/clipboard.js +1 -1
  35. package/lib/es/cloneDeep.js +1 -1
  36. package/lib/es/cookie.js +1 -1
  37. package/lib/es/date.js +1 -1
  38. package/lib/es/dom.js +1 -1
  39. package/lib/es/download.js +1 -1
  40. package/lib/es/easing.js +1 -1
  41. package/lib/es/file.js +42 -21
  42. package/lib/es/func.js +1 -1
  43. package/lib/es/index.js +1 -1
  44. package/lib/es/isEqual.js +1 -1
  45. package/lib/es/math.js +1 -1
  46. package/lib/es/number.js +1 -1
  47. package/lib/es/object.js +1 -1
  48. package/lib/es/path.js +1 -1
  49. package/lib/es/qs.js +1 -1
  50. package/lib/es/random.js +1 -1
  51. package/lib/es/string.js +1 -1
  52. package/lib/es/tooltip.js +1 -1
  53. package/lib/es/tree.js +1 -1
  54. package/lib/es/type.js +1 -1
  55. package/lib/es/unique.js +1 -1
  56. package/lib/es/url.js +1 -1
  57. package/lib/es/validator.js +1 -1
  58. package/lib/es/variable.js +1 -1
  59. package/lib/es/watermark.js +1 -1
  60. package/lib/es/we-decode.js +1 -1
  61. package/lib/index.d.ts +20 -5
  62. package/lib/umd/index.js +2 -3198
  63. package/package.json +2 -1
package/lib/cjs/array.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.2
2
+ * sculp-js v1.10.4
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/async.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.2
2
+ * sculp-js v1.10.4
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/base64.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.2
2
+ * sculp-js v1.10.4
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.2
2
+ * sculp-js v1.10.4
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.2
2
+ * sculp-js v1.10.4
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/cookie.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.2
2
+ * sculp-js v1.10.4
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/date.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.2
2
+ * sculp-js v1.10.4
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/dom.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.2
2
+ * sculp-js v1.10.4
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.2
2
+ * sculp-js v1.10.4
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/easing.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.2
2
+ * sculp-js v1.10.4
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/file.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.2
2
+ * sculp-js v1.10.4
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -64,22 +64,35 @@ function calculateSize({ maxWidth, maxHeight, originWidth, originHeight }) {
64
64
  /**
65
65
  * 根据原始图片的不同尺寸计算等比例缩放后的宽高尺寸
66
66
  *
67
- * @param {number} sizeKB
68
- * @param {number} originWidth
69
- * @param {number} originHeight
70
- * @returns {*}
67
+ * @param {number} sizeKB Image volume size, unit KB
68
+ * @param {number} maxSize Image max size
69
+ * @param {number} originWidth Image original width, unit px
70
+ * @param {number} originHeight Image original height, unit px
71
+ * @returns {*} {width, height}
71
72
  */
72
- function scalingByAspectRatio({ sizeKB, originWidth, originHeight }) {
73
+ function scalingByAspectRatio({ sizeKB, maxSize, originWidth, originHeight }) {
73
74
  let targetWidth = originWidth, targetHeight = originHeight;
74
- if (sizeKB <= 500) {
75
- // [50KB, 500KB]
75
+ if (type.isNumber(maxSize)) {
76
+ const { width, height } = calculateSize({ maxWidth: maxSize, maxHeight: maxSize, originWidth, originHeight });
77
+ targetWidth = width;
78
+ targetHeight = height;
79
+ }
80
+ else if (sizeKB < 500) {
81
+ // [50KB, 500KB)
76
82
  const maxWidth = 1200, maxHeight = 1200;
77
83
  const { width, height } = calculateSize({ maxWidth, maxHeight, originWidth, originHeight });
78
84
  targetWidth = width;
79
85
  targetHeight = height;
80
86
  }
87
+ else if (sizeKB < 5 * 1024) {
88
+ // [500KB, 5MB)
89
+ const maxWidth = 1400, maxHeight = 1400;
90
+ const { width, height } = calculateSize({ maxWidth, maxHeight, originWidth, originHeight });
91
+ targetWidth = width;
92
+ targetHeight = height;
93
+ }
81
94
  else if (sizeKB < 10 * 1024) {
82
- // (500KB, 10MB)
95
+ // [5MB, 10MB)
83
96
  const maxWidth = 1600, maxHeight = 1600;
84
97
  const { width, height } = calculateSize({ maxWidth, maxHeight, originWidth, originHeight });
85
98
  targetWidth = width;
@@ -95,27 +108,31 @@ function scalingByAspectRatio({ sizeKB, originWidth, originHeight }) {
95
108
  return { width: targetWidth, height: targetHeight };
96
109
  }
97
110
  /**
98
- * Web端:等比例压缩图片批量处理 (size小于200KB,不压缩)
111
+ * Web端:等比例压缩图片批量处理 (小于minFileSizeKB:50,不压缩), 支持压缩全景图或长截图
112
+ *
113
+ * 1. 默认根据图片原始size及宽高适当地调整quality、width、height
114
+ * 2. 可指定压缩的图片质量 quality(若不指定则根据原始图片大小来计算), 来适当调整width、height
115
+ * 3. 可指定压缩的图片最大宽高 maxSize(若不指定则根据原始图片宽高来计算), 满足大屏幕图片展示的场景
99
116
  *
100
117
  * @param {File | FileList} file 图片或图片数组
101
- * @param {ICompressOptions} options 压缩图片配置项,default: {mime:'image/jpeg'}
102
- * @returns {Promise<object> | undefined}
118
+ * @param {ICompressOptions} options 压缩图片配置项,default: {mime:'image/jpeg', minFileSizeKB: 50}
119
+ * @returns {Promise<ICompressImgResult | ICompressImgResult[] | null>}
103
120
  */
104
- function compressImg(file, options = { mime: 'image/jpeg' }) {
121
+ function compressImg(file, options = { mime: 'image/jpeg', minFileSizeKB: 50 }) {
105
122
  if (!(file instanceof File || file instanceof FileList)) {
106
123
  throw new Error(`${file} require be File or FileList`);
107
124
  }
108
125
  else if (!supportCanvas()) {
109
126
  throw new Error(`Current runtime environment not support Canvas`);
110
127
  }
111
- const { quality, mime = 'image/jpeg' } = type.isObject(options) ? options : {};
112
- let targetQuality = quality;
128
+ const { quality, mime = 'image/jpeg', maxSize: size, minFileSizeKB = 50 } = type.isObject(options) ? options : {};
129
+ let targetQuality = quality, maxSize;
113
130
  if (quality) {
114
131
  targetQuality = quality;
115
132
  }
116
133
  else if (file instanceof File) {
117
134
  const sizeKB = +parseInt((file.size / 1024).toFixed(2));
118
- if (sizeKB < 1 * 50) {
135
+ if (sizeKB < minFileSizeKB) {
119
136
  targetQuality = 1;
120
137
  }
121
138
  else if (sizeKB < 1 * 1024) {
@@ -128,8 +145,11 @@ function compressImg(file, options = { mime: 'image/jpeg' }) {
128
145
  targetQuality = 0.75;
129
146
  }
130
147
  }
148
+ if (type.isNumber(size)) {
149
+ maxSize = size >= 1200 ? size : 1200;
150
+ }
131
151
  if (file instanceof FileList) {
132
- return Promise.all(Array.from(file).map(el => compressImg(el, { mime: mime, quality: targetQuality }))); // 如果是 file 数组返回 Promise 数组
152
+ return Promise.all(Array.from(file).map(el => compressImg(el, { maxSize, mime: mime, quality: targetQuality }))); // 如果是 file 数组返回 Promise 数组
133
153
  }
134
154
  else if (file instanceof File) {
135
155
  return new Promise(resolve => {
@@ -140,7 +160,7 @@ function compressImg(file, options = { mime: 'image/jpeg' }) {
140
160
  };
141
161
  const fileName = [...file.name.split('.').slice(0, -1), ext[mime]].join('.');
142
162
  const sizeKB = +parseInt((file.size / 1024).toFixed(2));
143
- if (+(file.size / 1024).toFixed(2) < 50) {
163
+ if (sizeKB < minFileSizeKB) {
144
164
  resolve({
145
165
  file: file
146
166
  });
@@ -155,7 +175,7 @@ function compressImg(file, options = { mime: 'image/jpeg' }) {
155
175
  const context = canvas.getContext('2d');
156
176
  const originWidth = image.width;
157
177
  const originHeight = image.height;
158
- const { width, height } = scalingByAspectRatio({ sizeKB, originWidth, originHeight });
178
+ const { width, height } = scalingByAspectRatio({ sizeKB, maxSize, originWidth, originHeight });
159
179
  canvas.width = width;
160
180
  canvas.height = height;
161
181
  context.clearRect(0, 0, width, height);
@@ -176,7 +196,7 @@ function compressImg(file, options = { mime: 'image/jpeg' }) {
176
196
  origin: file,
177
197
  beforeSrc: src,
178
198
  afterSrc: canvasURL,
179
- beforeKB: Number((file.size / 1024).toFixed(2)),
199
+ beforeKB: sizeKB,
180
200
  afterKB: Number((miniFile.size / 1024).toFixed(2))
181
201
  });
182
202
  };
@@ -186,6 +206,7 @@ function compressImg(file, options = { mime: 'image/jpeg' }) {
186
206
  }
187
207
  });
188
208
  }
209
+ return Promise.resolve(null);
189
210
  }
190
211
 
191
212
  exports.chooseLocalFile = chooseLocalFile;
package/lib/cjs/func.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.2
2
+ * sculp-js v1.10.4
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.2
2
+ * sculp-js v1.10.4
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.2
2
+ * sculp-js v1.10.4
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/math.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.2
2
+ * sculp-js v1.10.4
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/number.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.2
2
+ * sculp-js v1.10.4
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/object.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.2
2
+ * sculp-js v1.10.4
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/path.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.2
2
+ * sculp-js v1.10.4
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/qs.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.2
2
+ * sculp-js v1.10.4
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/random.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.2
2
+ * sculp-js v1.10.4
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/string.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.2
2
+ * sculp-js v1.10.4
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.2
2
+ * sculp-js v1.10.4
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/tree.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.2
2
+ * sculp-js v1.10.4
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/type.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.2
2
+ * sculp-js v1.10.4
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/unique.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.2
2
+ * sculp-js v1.10.4
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/url.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.2
2
+ * sculp-js v1.10.4
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.2
2
+ * sculp-js v1.10.4
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.2
2
+ * sculp-js v1.10.4
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.2
2
+ * sculp-js v1.10.4
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.2
2
+ * sculp-js v1.10.4
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/array.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.2
2
+ * sculp-js v1.10.4
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/async.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.2
2
+ * sculp-js v1.10.4
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/base64.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.2
2
+ * sculp-js v1.10.4
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.2
2
+ * sculp-js v1.10.4
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.2
2
+ * sculp-js v1.10.4
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/cookie.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.2
2
+ * sculp-js v1.10.4
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/date.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.2
2
+ * sculp-js v1.10.4
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/dom.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.2
2
+ * sculp-js v1.10.4
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.2
2
+ * sculp-js v1.10.4
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/easing.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.2
2
+ * sculp-js v1.10.4
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/file.js CHANGED
@@ -1,10 +1,10 @@
1
1
  /*!
2
- * sculp-js v1.10.2
2
+ * sculp-js v1.10.4
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
6
6
 
7
- import { isObject } from './type.js';
7
+ import { isObject, isNumber } from './type.js';
8
8
 
9
9
  /**
10
10
  * 判断是否支持canvas
@@ -62,22 +62,35 @@ function calculateSize({ maxWidth, maxHeight, originWidth, originHeight }) {
62
62
  /**
63
63
  * 根据原始图片的不同尺寸计算等比例缩放后的宽高尺寸
64
64
  *
65
- * @param {number} sizeKB
66
- * @param {number} originWidth
67
- * @param {number} originHeight
68
- * @returns {*}
65
+ * @param {number} sizeKB Image volume size, unit KB
66
+ * @param {number} maxSize Image max size
67
+ * @param {number} originWidth Image original width, unit px
68
+ * @param {number} originHeight Image original height, unit px
69
+ * @returns {*} {width, height}
69
70
  */
70
- function scalingByAspectRatio({ sizeKB, originWidth, originHeight }) {
71
+ function scalingByAspectRatio({ sizeKB, maxSize, originWidth, originHeight }) {
71
72
  let targetWidth = originWidth, targetHeight = originHeight;
72
- if (sizeKB <= 500) {
73
- // [50KB, 500KB]
73
+ if (isNumber(maxSize)) {
74
+ const { width, height } = calculateSize({ maxWidth: maxSize, maxHeight: maxSize, originWidth, originHeight });
75
+ targetWidth = width;
76
+ targetHeight = height;
77
+ }
78
+ else if (sizeKB < 500) {
79
+ // [50KB, 500KB)
74
80
  const maxWidth = 1200, maxHeight = 1200;
75
81
  const { width, height } = calculateSize({ maxWidth, maxHeight, originWidth, originHeight });
76
82
  targetWidth = width;
77
83
  targetHeight = height;
78
84
  }
85
+ else if (sizeKB < 5 * 1024) {
86
+ // [500KB, 5MB)
87
+ const maxWidth = 1400, maxHeight = 1400;
88
+ const { width, height } = calculateSize({ maxWidth, maxHeight, originWidth, originHeight });
89
+ targetWidth = width;
90
+ targetHeight = height;
91
+ }
79
92
  else if (sizeKB < 10 * 1024) {
80
- // (500KB, 10MB)
93
+ // [5MB, 10MB)
81
94
  const maxWidth = 1600, maxHeight = 1600;
82
95
  const { width, height } = calculateSize({ maxWidth, maxHeight, originWidth, originHeight });
83
96
  targetWidth = width;
@@ -93,27 +106,31 @@ function scalingByAspectRatio({ sizeKB, originWidth, originHeight }) {
93
106
  return { width: targetWidth, height: targetHeight };
94
107
  }
95
108
  /**
96
- * Web端:等比例压缩图片批量处理 (size小于200KB,不压缩)
109
+ * Web端:等比例压缩图片批量处理 (小于minFileSizeKB:50,不压缩), 支持压缩全景图或长截图
110
+ *
111
+ * 1. 默认根据图片原始size及宽高适当地调整quality、width、height
112
+ * 2. 可指定压缩的图片质量 quality(若不指定则根据原始图片大小来计算), 来适当调整width、height
113
+ * 3. 可指定压缩的图片最大宽高 maxSize(若不指定则根据原始图片宽高来计算), 满足大屏幕图片展示的场景
97
114
  *
98
115
  * @param {File | FileList} file 图片或图片数组
99
- * @param {ICompressOptions} options 压缩图片配置项,default: {mime:'image/jpeg'}
100
- * @returns {Promise<object> | undefined}
116
+ * @param {ICompressOptions} options 压缩图片配置项,default: {mime:'image/jpeg', minFileSizeKB: 50}
117
+ * @returns {Promise<ICompressImgResult | ICompressImgResult[] | null>}
101
118
  */
102
- function compressImg(file, options = { mime: 'image/jpeg' }) {
119
+ function compressImg(file, options = { mime: 'image/jpeg', minFileSizeKB: 50 }) {
103
120
  if (!(file instanceof File || file instanceof FileList)) {
104
121
  throw new Error(`${file} require be File or FileList`);
105
122
  }
106
123
  else if (!supportCanvas()) {
107
124
  throw new Error(`Current runtime environment not support Canvas`);
108
125
  }
109
- const { quality, mime = 'image/jpeg' } = isObject(options) ? options : {};
110
- let targetQuality = quality;
126
+ const { quality, mime = 'image/jpeg', maxSize: size, minFileSizeKB = 50 } = isObject(options) ? options : {};
127
+ let targetQuality = quality, maxSize;
111
128
  if (quality) {
112
129
  targetQuality = quality;
113
130
  }
114
131
  else if (file instanceof File) {
115
132
  const sizeKB = +parseInt((file.size / 1024).toFixed(2));
116
- if (sizeKB < 1 * 50) {
133
+ if (sizeKB < minFileSizeKB) {
117
134
  targetQuality = 1;
118
135
  }
119
136
  else if (sizeKB < 1 * 1024) {
@@ -126,8 +143,11 @@ function compressImg(file, options = { mime: 'image/jpeg' }) {
126
143
  targetQuality = 0.75;
127
144
  }
128
145
  }
146
+ if (isNumber(size)) {
147
+ maxSize = size >= 1200 ? size : 1200;
148
+ }
129
149
  if (file instanceof FileList) {
130
- return Promise.all(Array.from(file).map(el => compressImg(el, { mime: mime, quality: targetQuality }))); // 如果是 file 数组返回 Promise 数组
150
+ return Promise.all(Array.from(file).map(el => compressImg(el, { maxSize, mime: mime, quality: targetQuality }))); // 如果是 file 数组返回 Promise 数组
131
151
  }
132
152
  else if (file instanceof File) {
133
153
  return new Promise(resolve => {
@@ -138,7 +158,7 @@ function compressImg(file, options = { mime: 'image/jpeg' }) {
138
158
  };
139
159
  const fileName = [...file.name.split('.').slice(0, -1), ext[mime]].join('.');
140
160
  const sizeKB = +parseInt((file.size / 1024).toFixed(2));
141
- if (+(file.size / 1024).toFixed(2) < 50) {
161
+ if (sizeKB < minFileSizeKB) {
142
162
  resolve({
143
163
  file: file
144
164
  });
@@ -153,7 +173,7 @@ function compressImg(file, options = { mime: 'image/jpeg' }) {
153
173
  const context = canvas.getContext('2d');
154
174
  const originWidth = image.width;
155
175
  const originHeight = image.height;
156
- const { width, height } = scalingByAspectRatio({ sizeKB, originWidth, originHeight });
176
+ const { width, height } = scalingByAspectRatio({ sizeKB, maxSize, originWidth, originHeight });
157
177
  canvas.width = width;
158
178
  canvas.height = height;
159
179
  context.clearRect(0, 0, width, height);
@@ -174,7 +194,7 @@ function compressImg(file, options = { mime: 'image/jpeg' }) {
174
194
  origin: file,
175
195
  beforeSrc: src,
176
196
  afterSrc: canvasURL,
177
- beforeKB: Number((file.size / 1024).toFixed(2)),
197
+ beforeKB: sizeKB,
178
198
  afterKB: Number((miniFile.size / 1024).toFixed(2))
179
199
  });
180
200
  };
@@ -184,6 +204,7 @@ function compressImg(file, options = { mime: 'image/jpeg' }) {
184
204
  }
185
205
  });
186
206
  }
207
+ return Promise.resolve(null);
187
208
  }
188
209
 
189
210
  export { chooseLocalFile, compressImg, supportCanvas };
package/lib/es/func.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.2
2
+ * sculp-js v1.10.4
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.2
2
+ * sculp-js v1.10.4
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/isEqual.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.2
2
+ * sculp-js v1.10.4
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/math.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.2
2
+ * sculp-js v1.10.4
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/number.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.2
2
+ * sculp-js v1.10.4
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/object.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.2
2
+ * sculp-js v1.10.4
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/path.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.2
2
+ * sculp-js v1.10.4
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */