@egova-mobile/app-media-utils 0.0.2 → 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.
@@ -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
  }
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.10/node_modules/dayjs/dayjs.min.js
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.10/node_modules/dayjs/dayjs.min.js"(exports, module2) {
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.10/node_modules/dayjs/locale/zh-cn.js
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.10/node_modules/dayjs/locale/zh-cn.js"(exports, module2) {
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) {
@@ -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
- if (file.size > this.compressOptions.photoFileSizeLimit * 1024 && quality - JPEG_QUALITY_STEP > MIN_JPEG_QUALITY) {
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
- quality - JPEG_QUALITY_STEP
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
  };
@@ -44,9 +44,9 @@ var __async = (__this, __arguments, generator) => {
44
44
  });
45
45
  };
46
46
 
47
- // ../../node_modules/.pnpm/dayjs@1.11.10/node_modules/dayjs/dayjs.min.js
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.10/node_modules/dayjs/dayjs.min.js"(exports, module) {
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.10/node_modules/dayjs/locale/zh-cn.js
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.10/node_modules/dayjs/locale/zh-cn.js"(exports, module) {
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) {
@@ -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
- if (file.size > this.compressOptions.photoFileSizeLimit * 1024 && quality - JPEG_QUALITY_STEP > MIN_JPEG_QUALITY) {
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
- quality - JPEG_QUALITY_STEP
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,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@egova-mobile/app-media-utils",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "description": "",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/index.esm.mjs",