@egova-mobile/app-media-utils 0.0.1 → 0.0.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/dist/image/image-processor.d.ts +11 -1
- package/dist/image/watermark.d.ts +11 -11
- package/dist/index.cjs.js +39 -8
- package/dist/index.esm.mjs +39 -8
- package/package.json +1 -1
|
@@ -14,5 +14,15 @@ export declare class ImageProcessor {
|
|
|
14
14
|
private compressOptions;
|
|
15
15
|
constructor(file: File, resizeOptions: ResizeOptions, watermarkOptions: WatermarkOptions, compressOptions: CompressOptions);
|
|
16
16
|
process(): Promise<void | File>;
|
|
17
|
-
compress(canvas: HTMLCanvasElement, filename: string, lastModified: number, type: string, quality: number): Promise<File>;
|
|
17
|
+
compress(canvas: HTMLCanvasElement, filename: string, lastModified: number, type: string, quality: number, lastCompressFileSize?: number): Promise<File>;
|
|
18
|
+
/**
|
|
19
|
+
* 浮点型数据比较大小
|
|
20
|
+
*/
|
|
21
|
+
compareFloats(a: number, b: number): number;
|
|
22
|
+
/**
|
|
23
|
+
* 浮点型数据保留小数(四舍五入)
|
|
24
|
+
* @param num 浮点型数据
|
|
25
|
+
* @param decimalPlaces 保留的小数位数
|
|
26
|
+
*/
|
|
27
|
+
roundToDecimalPlace(num: number, decimalPlaces: number): number;
|
|
18
28
|
}
|
|
@@ -15,15 +15,15 @@ export interface WatermarkContextParams {
|
|
|
15
15
|
height: string;
|
|
16
16
|
}
|
|
17
17
|
export declare class WatermarkContext {
|
|
18
|
-
address
|
|
19
|
-
watermarkTime
|
|
20
|
-
humanName
|
|
21
|
-
projectName
|
|
22
|
-
logo
|
|
23
|
-
applicationName
|
|
24
|
-
pixel
|
|
25
|
-
width
|
|
26
|
-
height
|
|
18
|
+
address?: string;
|
|
19
|
+
watermarkTime?: Date;
|
|
20
|
+
humanName?: string;
|
|
21
|
+
projectName?: string;
|
|
22
|
+
logo?: string;
|
|
23
|
+
applicationName?: string;
|
|
24
|
+
pixel?: string;
|
|
25
|
+
width?: string;
|
|
26
|
+
height?: string;
|
|
27
27
|
/**
|
|
28
28
|
* 打水印需要的环境信息
|
|
29
29
|
* @param address
|
|
@@ -41,8 +41,8 @@ export interface WatermarkOptionsParams {
|
|
|
41
41
|
}
|
|
42
42
|
export declare class WatermarkOptions {
|
|
43
43
|
enabled: boolean;
|
|
44
|
-
watermarkConfigString
|
|
45
|
-
context
|
|
44
|
+
watermarkConfigString?: string;
|
|
45
|
+
context?: WatermarkContext;
|
|
46
46
|
/**
|
|
47
47
|
* 水印配置项
|
|
48
48
|
* @param enabled, 是否开启水印
|
package/dist/index.cjs.js
CHANGED
|
@@ -50,9 +50,9 @@ var __async = (__this, __arguments, generator) => {
|
|
|
50
50
|
});
|
|
51
51
|
};
|
|
52
52
|
|
|
53
|
-
// ../../node_modules/.pnpm/dayjs@1.11.
|
|
53
|
+
// ../../node_modules/.pnpm/dayjs@1.11.13/node_modules/dayjs/dayjs.min.js
|
|
54
54
|
var require_dayjs_min = __commonJS({
|
|
55
|
-
"../../node_modules/.pnpm/dayjs@1.11.
|
|
55
|
+
"../../node_modules/.pnpm/dayjs@1.11.13/node_modules/dayjs/dayjs.min.js"(exports, module2) {
|
|
56
56
|
!function(t, e) {
|
|
57
57
|
"object" == typeof exports && "undefined" != typeof module2 ? module2.exports = e() : "function" == typeof define && define.amd ? define(e) : (t = "undefined" != typeof globalThis ? globalThis : t || self).dayjs = e();
|
|
58
58
|
}(exports, function() {
|
|
@@ -344,9 +344,9 @@ var require_dayjs_min = __commonJS({
|
|
|
344
344
|
}
|
|
345
345
|
});
|
|
346
346
|
|
|
347
|
-
// ../../node_modules/.pnpm/dayjs@1.11.
|
|
347
|
+
// ../../node_modules/.pnpm/dayjs@1.11.13/node_modules/dayjs/locale/zh-cn.js
|
|
348
348
|
var require_zh_cn = __commonJS({
|
|
349
|
-
"../../node_modules/.pnpm/dayjs@1.11.
|
|
349
|
+
"../../node_modules/.pnpm/dayjs@1.11.13/node_modules/dayjs/locale/zh-cn.js"(exports, module2) {
|
|
350
350
|
!function(e, _) {
|
|
351
351
|
"object" == typeof exports && "undefined" != typeof module2 ? module2.exports = _(require_dayjs_min()) : "function" == typeof define && define.amd ? define(["dayjs"], _) : (e = "undefined" != typeof globalThis ? globalThis : e || self).dayjs_locale_zh_cn = _(e.dayjs);
|
|
352
352
|
}(exports, function(e) {
|
|
@@ -1281,7 +1281,7 @@ function renderWatermark(canvas, options) {
|
|
|
1281
1281
|
if (!options.enabled || options.watermarkConfigString === "no") {
|
|
1282
1282
|
return Promise.resolve(canvas);
|
|
1283
1283
|
}
|
|
1284
|
-
let { watermarkConfigString } = options;
|
|
1284
|
+
let { watermarkConfigString = "" } = options;
|
|
1285
1285
|
if (watermarkConfigString === "default") {
|
|
1286
1286
|
watermarkConfigString = "50$lefttop$50*#*time###25$lefttop*#*date";
|
|
1287
1287
|
}
|
|
@@ -1502,16 +1502,28 @@ var ImageProcessor = class {
|
|
|
1502
1502
|
);
|
|
1503
1503
|
});
|
|
1504
1504
|
}
|
|
1505
|
-
compress(canvas, filename, lastModified, type, quality) {
|
|
1505
|
+
compress(canvas, filename, lastModified, type, quality, lastCompressFileSize = -1) {
|
|
1506
1506
|
return canvas2File(canvas, filename, lastModified, type, quality).then(
|
|
1507
1507
|
(file) => {
|
|
1508
|
-
|
|
1508
|
+
let qualityStep = JPEG_QUALITY_STEP;
|
|
1509
|
+
if (file.size > this.compressOptions.photoFileSizeLimit * 1024) {
|
|
1510
|
+
let factor = Math.floor(file.size / (1024 * this.compressOptions.photoFileSizeLimit));
|
|
1511
|
+
if (factor > 1 && quality > MIN_JPEG_QUALITY) {
|
|
1512
|
+
qualityStep = this.roundToDecimalPlace(JPEG_QUALITY_STEP * factor, 2);
|
|
1513
|
+
let maxQualityStep = this.roundToDecimalPlace(quality - MIN_JPEG_QUALITY, 2);
|
|
1514
|
+
qualityStep = Math.min(qualityStep, maxQualityStep);
|
|
1515
|
+
}
|
|
1516
|
+
}
|
|
1517
|
+
let compressFileSizeChanged = lastCompressFileSize === -1 || file.size < lastCompressFileSize;
|
|
1518
|
+
let nextCompressQuality = this.roundToDecimalPlace(quality - qualityStep, 2);
|
|
1519
|
+
if (compressFileSizeChanged && file.size > this.compressOptions.photoFileSizeLimit * 1024 && this.compareFloats(nextCompressQuality, MIN_JPEG_QUALITY) >= 0) {
|
|
1509
1520
|
return this.compress(
|
|
1510
1521
|
canvas,
|
|
1511
1522
|
filename,
|
|
1512
1523
|
lastModified,
|
|
1513
1524
|
type,
|
|
1514
|
-
|
|
1525
|
+
this.roundToDecimalPlace(nextCompressQuality, 2),
|
|
1526
|
+
file.size
|
|
1515
1527
|
);
|
|
1516
1528
|
} else {
|
|
1517
1529
|
return Promise.resolve(file);
|
|
@@ -1519,4 +1531,23 @@ var ImageProcessor = class {
|
|
|
1519
1531
|
}
|
|
1520
1532
|
);
|
|
1521
1533
|
}
|
|
1534
|
+
/**
|
|
1535
|
+
* 浮点型数据比较大小
|
|
1536
|
+
*/
|
|
1537
|
+
compareFloats(a, b) {
|
|
1538
|
+
const diff = Math.abs(a - b);
|
|
1539
|
+
if (diff < Number.EPSILON) {
|
|
1540
|
+
return 0;
|
|
1541
|
+
}
|
|
1542
|
+
return a - b > 0 ? 1 : -1;
|
|
1543
|
+
}
|
|
1544
|
+
/**
|
|
1545
|
+
* 浮点型数据保留小数(四舍五入)
|
|
1546
|
+
* @param num 浮点型数据
|
|
1547
|
+
* @param decimalPlaces 保留的小数位数
|
|
1548
|
+
*/
|
|
1549
|
+
roundToDecimalPlace(num, decimalPlaces) {
|
|
1550
|
+
const factor = Math.pow(10, decimalPlaces);
|
|
1551
|
+
return Math.round(num * factor) / factor;
|
|
1552
|
+
}
|
|
1522
1553
|
};
|
package/dist/index.esm.mjs
CHANGED
|
@@ -44,9 +44,9 @@ var __async = (__this, __arguments, generator) => {
|
|
|
44
44
|
});
|
|
45
45
|
};
|
|
46
46
|
|
|
47
|
-
// ../../node_modules/.pnpm/dayjs@1.11.
|
|
47
|
+
// ../../node_modules/.pnpm/dayjs@1.11.13/node_modules/dayjs/dayjs.min.js
|
|
48
48
|
var require_dayjs_min = __commonJS({
|
|
49
|
-
"../../node_modules/.pnpm/dayjs@1.11.
|
|
49
|
+
"../../node_modules/.pnpm/dayjs@1.11.13/node_modules/dayjs/dayjs.min.js"(exports, module) {
|
|
50
50
|
!function(t, e) {
|
|
51
51
|
"object" == typeof exports && "undefined" != typeof module ? module.exports = e() : "function" == typeof define && define.amd ? define(e) : (t = "undefined" != typeof globalThis ? globalThis : t || self).dayjs = e();
|
|
52
52
|
}(exports, function() {
|
|
@@ -338,9 +338,9 @@ var require_dayjs_min = __commonJS({
|
|
|
338
338
|
}
|
|
339
339
|
});
|
|
340
340
|
|
|
341
|
-
// ../../node_modules/.pnpm/dayjs@1.11.
|
|
341
|
+
// ../../node_modules/.pnpm/dayjs@1.11.13/node_modules/dayjs/locale/zh-cn.js
|
|
342
342
|
var require_zh_cn = __commonJS({
|
|
343
|
-
"../../node_modules/.pnpm/dayjs@1.11.
|
|
343
|
+
"../../node_modules/.pnpm/dayjs@1.11.13/node_modules/dayjs/locale/zh-cn.js"(exports, module) {
|
|
344
344
|
!function(e, _) {
|
|
345
345
|
"object" == typeof exports && "undefined" != typeof module ? module.exports = _(require_dayjs_min()) : "function" == typeof define && define.amd ? define(["dayjs"], _) : (e = "undefined" != typeof globalThis ? globalThis : e || self).dayjs_locale_zh_cn = _(e.dayjs);
|
|
346
346
|
}(exports, function(e) {
|
|
@@ -1256,7 +1256,7 @@ function renderWatermark(canvas, options) {
|
|
|
1256
1256
|
if (!options.enabled || options.watermarkConfigString === "no") {
|
|
1257
1257
|
return Promise.resolve(canvas);
|
|
1258
1258
|
}
|
|
1259
|
-
let { watermarkConfigString } = options;
|
|
1259
|
+
let { watermarkConfigString = "" } = options;
|
|
1260
1260
|
if (watermarkConfigString === "default") {
|
|
1261
1261
|
watermarkConfigString = "50$lefttop$50*#*time###25$lefttop*#*date";
|
|
1262
1262
|
}
|
|
@@ -1477,16 +1477,28 @@ var ImageProcessor = class {
|
|
|
1477
1477
|
);
|
|
1478
1478
|
});
|
|
1479
1479
|
}
|
|
1480
|
-
compress(canvas, filename, lastModified, type, quality) {
|
|
1480
|
+
compress(canvas, filename, lastModified, type, quality, lastCompressFileSize = -1) {
|
|
1481
1481
|
return canvas2File(canvas, filename, lastModified, type, quality).then(
|
|
1482
1482
|
(file) => {
|
|
1483
|
-
|
|
1483
|
+
let qualityStep = JPEG_QUALITY_STEP;
|
|
1484
|
+
if (file.size > this.compressOptions.photoFileSizeLimit * 1024) {
|
|
1485
|
+
let factor = Math.floor(file.size / (1024 * this.compressOptions.photoFileSizeLimit));
|
|
1486
|
+
if (factor > 1 && quality > MIN_JPEG_QUALITY) {
|
|
1487
|
+
qualityStep = this.roundToDecimalPlace(JPEG_QUALITY_STEP * factor, 2);
|
|
1488
|
+
let maxQualityStep = this.roundToDecimalPlace(quality - MIN_JPEG_QUALITY, 2);
|
|
1489
|
+
qualityStep = Math.min(qualityStep, maxQualityStep);
|
|
1490
|
+
}
|
|
1491
|
+
}
|
|
1492
|
+
let compressFileSizeChanged = lastCompressFileSize === -1 || file.size < lastCompressFileSize;
|
|
1493
|
+
let nextCompressQuality = this.roundToDecimalPlace(quality - qualityStep, 2);
|
|
1494
|
+
if (compressFileSizeChanged && file.size > this.compressOptions.photoFileSizeLimit * 1024 && this.compareFloats(nextCompressQuality, MIN_JPEG_QUALITY) >= 0) {
|
|
1484
1495
|
return this.compress(
|
|
1485
1496
|
canvas,
|
|
1486
1497
|
filename,
|
|
1487
1498
|
lastModified,
|
|
1488
1499
|
type,
|
|
1489
|
-
|
|
1500
|
+
this.roundToDecimalPlace(nextCompressQuality, 2),
|
|
1501
|
+
file.size
|
|
1490
1502
|
);
|
|
1491
1503
|
} else {
|
|
1492
1504
|
return Promise.resolve(file);
|
|
@@ -1494,6 +1506,25 @@ var ImageProcessor = class {
|
|
|
1494
1506
|
}
|
|
1495
1507
|
);
|
|
1496
1508
|
}
|
|
1509
|
+
/**
|
|
1510
|
+
* 浮点型数据比较大小
|
|
1511
|
+
*/
|
|
1512
|
+
compareFloats(a, b) {
|
|
1513
|
+
const diff = Math.abs(a - b);
|
|
1514
|
+
if (diff < Number.EPSILON) {
|
|
1515
|
+
return 0;
|
|
1516
|
+
}
|
|
1517
|
+
return a - b > 0 ? 1 : -1;
|
|
1518
|
+
}
|
|
1519
|
+
/**
|
|
1520
|
+
* 浮点型数据保留小数(四舍五入)
|
|
1521
|
+
* @param num 浮点型数据
|
|
1522
|
+
* @param decimalPlaces 保留的小数位数
|
|
1523
|
+
*/
|
|
1524
|
+
roundToDecimalPlace(num, decimalPlaces) {
|
|
1525
|
+
const factor = Math.pow(10, decimalPlaces);
|
|
1526
|
+
return Math.round(num * factor) / factor;
|
|
1527
|
+
}
|
|
1497
1528
|
};
|
|
1498
1529
|
export {
|
|
1499
1530
|
CompressOptions,
|