sculp-js 1.10.2 → 1.10.3
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.
- package/lib/cjs/array.js +1 -1
- package/lib/cjs/async.js +1 -1
- package/lib/cjs/base64.js +1 -1
- package/lib/cjs/clipboard.js +1 -1
- package/lib/cjs/cloneDeep.js +1 -1
- package/lib/cjs/cookie.js +1 -1
- package/lib/cjs/date.js +1 -1
- package/lib/cjs/dom.js +1 -1
- package/lib/cjs/download.js +1 -1
- package/lib/cjs/easing.js +1 -1
- package/lib/cjs/file.js +35 -15
- package/lib/cjs/func.js +1 -1
- package/lib/cjs/index.js +1 -1
- package/lib/cjs/isEqual.js +1 -1
- package/lib/cjs/math.js +1 -1
- package/lib/cjs/number.js +1 -1
- package/lib/cjs/object.js +1 -1
- package/lib/cjs/path.js +1 -1
- package/lib/cjs/qs.js +1 -1
- package/lib/cjs/random.js +1 -1
- package/lib/cjs/string.js +1 -1
- package/lib/cjs/tooltip.js +1 -1
- package/lib/cjs/tree.js +1 -1
- package/lib/cjs/type.js +1 -1
- package/lib/cjs/unique.js +1 -1
- package/lib/cjs/url.js +1 -1
- package/lib/cjs/validator.js +1 -1
- package/lib/cjs/variable.js +1 -1
- package/lib/cjs/watermark.js +1 -1
- package/lib/cjs/we-decode.js +1 -1
- package/lib/es/array.js +1 -1
- package/lib/es/async.js +1 -1
- package/lib/es/base64.js +1 -1
- package/lib/es/clipboard.js +1 -1
- package/lib/es/cloneDeep.js +1 -1
- package/lib/es/cookie.js +1 -1
- package/lib/es/date.js +1 -1
- package/lib/es/dom.js +1 -1
- package/lib/es/download.js +1 -1
- package/lib/es/easing.js +1 -1
- package/lib/es/file.js +36 -16
- package/lib/es/func.js +1 -1
- package/lib/es/index.js +1 -1
- package/lib/es/isEqual.js +1 -1
- package/lib/es/math.js +1 -1
- package/lib/es/number.js +1 -1
- package/lib/es/object.js +1 -1
- package/lib/es/path.js +1 -1
- package/lib/es/qs.js +1 -1
- package/lib/es/random.js +1 -1
- package/lib/es/string.js +1 -1
- package/lib/es/tooltip.js +1 -1
- package/lib/es/tree.js +1 -1
- package/lib/es/type.js +1 -1
- package/lib/es/unique.js +1 -1
- package/lib/es/url.js +1 -1
- package/lib/es/validator.js +1 -1
- package/lib/es/variable.js +1 -1
- package/lib/es/watermark.js +1 -1
- package/lib/es/we-decode.js +1 -1
- package/lib/index.d.ts +6 -1
- package/lib/umd/index.js +35 -15
- package/package.json +1 -1
package/lib/cjs/array.js
CHANGED
package/lib/cjs/async.js
CHANGED
package/lib/cjs/base64.js
CHANGED
package/lib/cjs/clipboard.js
CHANGED
package/lib/cjs/cloneDeep.js
CHANGED
package/lib/cjs/cookie.js
CHANGED
package/lib/cjs/date.js
CHANGED
package/lib/cjs/dom.js
CHANGED
package/lib/cjs/download.js
CHANGED
package/lib/cjs/easing.js
CHANGED
package/lib/cjs/file.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* sculp-js v1.10.
|
|
2
|
+
* sculp-js v1.10.3
|
|
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}
|
|
69
|
-
* @param {number}
|
|
70
|
-
* @
|
|
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 (
|
|
75
|
-
|
|
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
|
-
//
|
|
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,7 +108,11 @@ function scalingByAspectRatio({ sizeKB, originWidth, originHeight }) {
|
|
|
95
108
|
return { width: targetWidth, height: targetHeight };
|
|
96
109
|
}
|
|
97
110
|
/**
|
|
98
|
-
* Web端:等比例压缩图片批量处理 (size小于
|
|
111
|
+
* Web端:等比例压缩图片批量处理 (size小于50KB,不压缩), 支持压缩全景图或长截图
|
|
112
|
+
*
|
|
113
|
+
* 1. 默认根据图片原始size及宽高适当地调整quality、width、height
|
|
114
|
+
* 2. 可指定压缩的图片质量 quality(若不指定则根据原始图片大小来计算), 来适当调整width、height
|
|
115
|
+
* 3. 可指定压缩的图片最大宽高 maxSize(若不指定则根据原始图片宽高来计算), 满足大屏幕图片展示的场景
|
|
99
116
|
*
|
|
100
117
|
* @param {File | FileList} file 图片或图片数组
|
|
101
118
|
* @param {ICompressOptions} options 压缩图片配置项,default: {mime:'image/jpeg'}
|
|
@@ -108,8 +125,8 @@ function compressImg(file, options = { mime: 'image/jpeg' }) {
|
|
|
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 } = type.isObject(options) ? options : {};
|
|
129
|
+
let targetQuality = quality, maxSize;
|
|
113
130
|
if (quality) {
|
|
114
131
|
targetQuality = quality;
|
|
115
132
|
}
|
|
@@ -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 (
|
|
163
|
+
if (sizeKB < 50) {
|
|
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);
|
package/lib/cjs/func.js
CHANGED
package/lib/cjs/index.js
CHANGED
package/lib/cjs/isEqual.js
CHANGED
package/lib/cjs/math.js
CHANGED
package/lib/cjs/number.js
CHANGED
package/lib/cjs/object.js
CHANGED
package/lib/cjs/path.js
CHANGED
package/lib/cjs/qs.js
CHANGED
package/lib/cjs/random.js
CHANGED
package/lib/cjs/string.js
CHANGED
package/lib/cjs/tooltip.js
CHANGED
package/lib/cjs/tree.js
CHANGED
package/lib/cjs/type.js
CHANGED
package/lib/cjs/unique.js
CHANGED
package/lib/cjs/url.js
CHANGED
package/lib/cjs/validator.js
CHANGED
package/lib/cjs/variable.js
CHANGED
package/lib/cjs/watermark.js
CHANGED
package/lib/cjs/we-decode.js
CHANGED
package/lib/es/array.js
CHANGED
package/lib/es/async.js
CHANGED
package/lib/es/base64.js
CHANGED
package/lib/es/clipboard.js
CHANGED
package/lib/es/cloneDeep.js
CHANGED
package/lib/es/cookie.js
CHANGED
package/lib/es/date.js
CHANGED
package/lib/es/dom.js
CHANGED
package/lib/es/download.js
CHANGED
package/lib/es/easing.js
CHANGED
package/lib/es/file.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* sculp-js v1.10.
|
|
2
|
+
* sculp-js v1.10.3
|
|
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}
|
|
67
|
-
* @param {number}
|
|
68
|
-
* @
|
|
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 (
|
|
73
|
-
|
|
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
|
-
//
|
|
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,7 +106,11 @@ function scalingByAspectRatio({ sizeKB, originWidth, originHeight }) {
|
|
|
93
106
|
return { width: targetWidth, height: targetHeight };
|
|
94
107
|
}
|
|
95
108
|
/**
|
|
96
|
-
* Web端:等比例压缩图片批量处理 (size小于
|
|
109
|
+
* Web端:等比例压缩图片批量处理 (size小于50KB,不压缩), 支持压缩全景图或长截图
|
|
110
|
+
*
|
|
111
|
+
* 1. 默认根据图片原始size及宽高适当地调整quality、width、height
|
|
112
|
+
* 2. 可指定压缩的图片质量 quality(若不指定则根据原始图片大小来计算), 来适当调整width、height
|
|
113
|
+
* 3. 可指定压缩的图片最大宽高 maxSize(若不指定则根据原始图片宽高来计算), 满足大屏幕图片展示的场景
|
|
97
114
|
*
|
|
98
115
|
* @param {File | FileList} file 图片或图片数组
|
|
99
116
|
* @param {ICompressOptions} options 压缩图片配置项,default: {mime:'image/jpeg'}
|
|
@@ -106,8 +123,8 @@ function compressImg(file, options = { mime: 'image/jpeg' }) {
|
|
|
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 } = isObject(options) ? options : {};
|
|
127
|
+
let targetQuality = quality, maxSize;
|
|
111
128
|
if (quality) {
|
|
112
129
|
targetQuality = quality;
|
|
113
130
|
}
|
|
@@ -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 (
|
|
161
|
+
if (sizeKB < 50) {
|
|
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);
|
package/lib/es/func.js
CHANGED
package/lib/es/index.js
CHANGED
package/lib/es/isEqual.js
CHANGED
package/lib/es/math.js
CHANGED
package/lib/es/number.js
CHANGED
package/lib/es/object.js
CHANGED
package/lib/es/path.js
CHANGED
package/lib/es/qs.js
CHANGED
package/lib/es/random.js
CHANGED
package/lib/es/string.js
CHANGED
package/lib/es/tooltip.js
CHANGED
package/lib/es/tree.js
CHANGED
package/lib/es/type.js
CHANGED
package/lib/es/unique.js
CHANGED
package/lib/es/url.js
CHANGED
package/lib/es/validator.js
CHANGED
package/lib/es/variable.js
CHANGED
package/lib/es/watermark.js
CHANGED
package/lib/es/we-decode.js
CHANGED
package/lib/index.d.ts
CHANGED
|
@@ -603,9 +603,14 @@ interface ICompressOptions {
|
|
|
603
603
|
quality?: number;
|
|
604
604
|
/** 图片类型 */
|
|
605
605
|
mime?: ImageType;
|
|
606
|
+
maxSize?: number;
|
|
606
607
|
}
|
|
607
608
|
/**
|
|
608
|
-
* Web端:等比例压缩图片批量处理 (size小于
|
|
609
|
+
* Web端:等比例压缩图片批量处理 (size小于50KB,不压缩), 支持压缩全景图或长截图
|
|
610
|
+
*
|
|
611
|
+
* 1. 默认根据图片原始size及宽高适当地调整quality、width、height
|
|
612
|
+
* 2. 可指定压缩的图片质量 quality(若不指定则根据原始图片大小来计算), 来适当调整width、height
|
|
613
|
+
* 3. 可指定压缩的图片最大宽高 maxSize(若不指定则根据原始图片宽高来计算), 满足大屏幕图片展示的场景
|
|
609
614
|
*
|
|
610
615
|
* @param {File | FileList} file 图片或图片数组
|
|
611
616
|
* @param {ICompressOptions} options 压缩图片配置项,default: {mime:'image/jpeg'}
|
package/lib/umd/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* sculp-js v1.10.
|
|
2
|
+
* sculp-js v1.10.3
|
|
3
3
|
* (c) 2023-present chandq
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
@@ -1433,22 +1433,35 @@
|
|
|
1433
1433
|
/**
|
|
1434
1434
|
* 根据原始图片的不同尺寸计算等比例缩放后的宽高尺寸
|
|
1435
1435
|
*
|
|
1436
|
-
* @param {number} sizeKB
|
|
1437
|
-
* @param {number}
|
|
1438
|
-
* @param {number}
|
|
1439
|
-
* @
|
|
1436
|
+
* @param {number} sizeKB Image volume size, unit KB
|
|
1437
|
+
* @param {number} maxSize Image max size
|
|
1438
|
+
* @param {number} originWidth Image original width, unit px
|
|
1439
|
+
* @param {number} originHeight Image original height, unit px
|
|
1440
|
+
* @returns {*} {width, height}
|
|
1440
1441
|
*/
|
|
1441
|
-
function scalingByAspectRatio({ sizeKB, originWidth, originHeight }) {
|
|
1442
|
+
function scalingByAspectRatio({ sizeKB, maxSize, originWidth, originHeight }) {
|
|
1442
1443
|
let targetWidth = originWidth, targetHeight = originHeight;
|
|
1443
|
-
if (
|
|
1444
|
-
|
|
1444
|
+
if (isNumber(maxSize)) {
|
|
1445
|
+
const { width, height } = calculateSize({ maxWidth: maxSize, maxHeight: maxSize, originWidth, originHeight });
|
|
1446
|
+
targetWidth = width;
|
|
1447
|
+
targetHeight = height;
|
|
1448
|
+
}
|
|
1449
|
+
else if (sizeKB < 500) {
|
|
1450
|
+
// [50KB, 500KB)
|
|
1445
1451
|
const maxWidth = 1200, maxHeight = 1200;
|
|
1446
1452
|
const { width, height } = calculateSize({ maxWidth, maxHeight, originWidth, originHeight });
|
|
1447
1453
|
targetWidth = width;
|
|
1448
1454
|
targetHeight = height;
|
|
1449
1455
|
}
|
|
1456
|
+
else if (sizeKB < 5 * 1024) {
|
|
1457
|
+
// [500KB, 5MB)
|
|
1458
|
+
const maxWidth = 1400, maxHeight = 1400;
|
|
1459
|
+
const { width, height } = calculateSize({ maxWidth, maxHeight, originWidth, originHeight });
|
|
1460
|
+
targetWidth = width;
|
|
1461
|
+
targetHeight = height;
|
|
1462
|
+
}
|
|
1450
1463
|
else if (sizeKB < 10 * 1024) {
|
|
1451
|
-
//
|
|
1464
|
+
// [5MB, 10MB)
|
|
1452
1465
|
const maxWidth = 1600, maxHeight = 1600;
|
|
1453
1466
|
const { width, height } = calculateSize({ maxWidth, maxHeight, originWidth, originHeight });
|
|
1454
1467
|
targetWidth = width;
|
|
@@ -1464,7 +1477,11 @@
|
|
|
1464
1477
|
return { width: targetWidth, height: targetHeight };
|
|
1465
1478
|
}
|
|
1466
1479
|
/**
|
|
1467
|
-
* Web端:等比例压缩图片批量处理 (size小于
|
|
1480
|
+
* Web端:等比例压缩图片批量处理 (size小于50KB,不压缩), 支持压缩全景图或长截图
|
|
1481
|
+
*
|
|
1482
|
+
* 1. 默认根据图片原始size及宽高适当地调整quality、width、height
|
|
1483
|
+
* 2. 可指定压缩的图片质量 quality(若不指定则根据原始图片大小来计算), 来适当调整width、height
|
|
1484
|
+
* 3. 可指定压缩的图片最大宽高 maxSize(若不指定则根据原始图片宽高来计算), 满足大屏幕图片展示的场景
|
|
1468
1485
|
*
|
|
1469
1486
|
* @param {File | FileList} file 图片或图片数组
|
|
1470
1487
|
* @param {ICompressOptions} options 压缩图片配置项,default: {mime:'image/jpeg'}
|
|
@@ -1477,8 +1494,8 @@
|
|
|
1477
1494
|
else if (!supportCanvas()) {
|
|
1478
1495
|
throw new Error(`Current runtime environment not support Canvas`);
|
|
1479
1496
|
}
|
|
1480
|
-
const { quality, mime = 'image/jpeg' } = isObject(options) ? options : {};
|
|
1481
|
-
let targetQuality = quality;
|
|
1497
|
+
const { quality, mime = 'image/jpeg', maxSize: size } = isObject(options) ? options : {};
|
|
1498
|
+
let targetQuality = quality, maxSize;
|
|
1482
1499
|
if (quality) {
|
|
1483
1500
|
targetQuality = quality;
|
|
1484
1501
|
}
|
|
@@ -1497,8 +1514,11 @@
|
|
|
1497
1514
|
targetQuality = 0.75;
|
|
1498
1515
|
}
|
|
1499
1516
|
}
|
|
1517
|
+
if (isNumber(size)) {
|
|
1518
|
+
maxSize = size >= 1200 ? size : 1200;
|
|
1519
|
+
}
|
|
1500
1520
|
if (file instanceof FileList) {
|
|
1501
|
-
return Promise.all(Array.from(file).map(el => compressImg(el, { mime: mime, quality: targetQuality }))); // 如果是 file 数组返回 Promise 数组
|
|
1521
|
+
return Promise.all(Array.from(file).map(el => compressImg(el, { maxSize, mime: mime, quality: targetQuality }))); // 如果是 file 数组返回 Promise 数组
|
|
1502
1522
|
}
|
|
1503
1523
|
else if (file instanceof File) {
|
|
1504
1524
|
return new Promise(resolve => {
|
|
@@ -1509,7 +1529,7 @@
|
|
|
1509
1529
|
};
|
|
1510
1530
|
const fileName = [...file.name.split('.').slice(0, -1), ext[mime]].join('.');
|
|
1511
1531
|
const sizeKB = +parseInt((file.size / 1024).toFixed(2));
|
|
1512
|
-
if (
|
|
1532
|
+
if (sizeKB < 50) {
|
|
1513
1533
|
resolve({
|
|
1514
1534
|
file: file
|
|
1515
1535
|
});
|
|
@@ -1524,7 +1544,7 @@
|
|
|
1524
1544
|
const context = canvas.getContext('2d');
|
|
1525
1545
|
const originWidth = image.width;
|
|
1526
1546
|
const originHeight = image.height;
|
|
1527
|
-
const { width, height } = scalingByAspectRatio({ sizeKB, originWidth, originHeight });
|
|
1547
|
+
const { width, height } = scalingByAspectRatio({ sizeKB, maxSize, originWidth, originHeight });
|
|
1528
1548
|
canvas.width = width;
|
|
1529
1549
|
canvas.height = height;
|
|
1530
1550
|
context.clearRect(0, 0, width, height);
|