@egova-mobile/app-media-utils 0.0.2 → 0.0.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.
@@ -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) {
@@ -1226,15 +1226,19 @@ function drawGroupedWatermarks(canvas, watermarks, groupPosition) {
1226
1226
  }
1227
1227
  const contentPadding = getCanvasScale(canvas, CONTENT_PADDING);
1228
1228
  let baseOrigin = new Point(contentPadding, contentPadding);
1229
+ let titlePadding = 0;
1229
1230
  if (groupPosition === "center" /* Center */) {
1230
1231
  baseOrigin = new Point(
1231
1232
  contentPadding,
1232
1233
  Math.round((canvas.height - totalHeight) / 2)
1233
1234
  );
1234
1235
  } else if (groupPosition === "bottom" /* Bottom */) {
1236
+ tiles.forEach((item) => {
1237
+ titlePadding += item.style.paddingTop + item.style.paddingBottom;
1238
+ });
1235
1239
  baseOrigin = new Point(
1236
1240
  contentPadding,
1237
- canvas.height - totalHeight - contentPadding
1241
+ canvas.height - totalHeight - contentPadding - titlePadding
1238
1242
  );
1239
1243
  } else if (groupPosition === "top" /* Top */) {
1240
1244
  baseOrigin = new Point(contentPadding, contentPadding);
@@ -1502,16 +1506,28 @@ var ImageProcessor = class {
1502
1506
  );
1503
1507
  });
1504
1508
  }
1505
- compress(canvas, filename, lastModified, type, quality) {
1509
+ compress(canvas, filename, lastModified, type, quality, lastCompressFileSize = -1) {
1506
1510
  return canvas2File(canvas, filename, lastModified, type, quality).then(
1507
1511
  (file) => {
1508
- if (file.size > this.compressOptions.photoFileSizeLimit * 1024 && quality - JPEG_QUALITY_STEP > MIN_JPEG_QUALITY) {
1512
+ let qualityStep = JPEG_QUALITY_STEP;
1513
+ if (file.size > this.compressOptions.photoFileSizeLimit * 1024) {
1514
+ let factor = Math.floor(file.size / (1024 * this.compressOptions.photoFileSizeLimit));
1515
+ if (factor > 1 && quality > MIN_JPEG_QUALITY) {
1516
+ qualityStep = this.roundToDecimalPlace(JPEG_QUALITY_STEP * factor, 2);
1517
+ let maxQualityStep = this.roundToDecimalPlace(quality - MIN_JPEG_QUALITY, 2);
1518
+ qualityStep = Math.min(qualityStep, maxQualityStep);
1519
+ }
1520
+ }
1521
+ let compressFileSizeChanged = lastCompressFileSize === -1 || file.size < lastCompressFileSize;
1522
+ let nextCompressQuality = this.roundToDecimalPlace(quality - qualityStep, 2);
1523
+ if (compressFileSizeChanged && file.size > this.compressOptions.photoFileSizeLimit * 1024 && this.compareFloats(nextCompressQuality, MIN_JPEG_QUALITY) >= 0) {
1509
1524
  return this.compress(
1510
1525
  canvas,
1511
1526
  filename,
1512
1527
  lastModified,
1513
1528
  type,
1514
- quality - JPEG_QUALITY_STEP
1529
+ this.roundToDecimalPlace(nextCompressQuality, 2),
1530
+ file.size
1515
1531
  );
1516
1532
  } else {
1517
1533
  return Promise.resolve(file);
@@ -1519,4 +1535,23 @@ var ImageProcessor = class {
1519
1535
  }
1520
1536
  );
1521
1537
  }
1538
+ /**
1539
+ * 浮点型数据比较大小
1540
+ */
1541
+ compareFloats(a, b) {
1542
+ const diff = Math.abs(a - b);
1543
+ if (diff < Number.EPSILON) {
1544
+ return 0;
1545
+ }
1546
+ return a - b > 0 ? 1 : -1;
1547
+ }
1548
+ /**
1549
+ * 浮点型数据保留小数(四舍五入)
1550
+ * @param num 浮点型数据
1551
+ * @param decimalPlaces 保留的小数位数
1552
+ */
1553
+ roundToDecimalPlace(num, decimalPlaces) {
1554
+ const factor = Math.pow(10, decimalPlaces);
1555
+ return Math.round(num * factor) / factor;
1556
+ }
1522
1557
  };
@@ -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) {
@@ -1201,15 +1201,19 @@ function drawGroupedWatermarks(canvas, watermarks, groupPosition) {
1201
1201
  }
1202
1202
  const contentPadding = getCanvasScale(canvas, CONTENT_PADDING);
1203
1203
  let baseOrigin = new Point(contentPadding, contentPadding);
1204
+ let titlePadding = 0;
1204
1205
  if (groupPosition === "center" /* Center */) {
1205
1206
  baseOrigin = new Point(
1206
1207
  contentPadding,
1207
1208
  Math.round((canvas.height - totalHeight) / 2)
1208
1209
  );
1209
1210
  } else if (groupPosition === "bottom" /* Bottom */) {
1211
+ tiles.forEach((item) => {
1212
+ titlePadding += item.style.paddingTop + item.style.paddingBottom;
1213
+ });
1210
1214
  baseOrigin = new Point(
1211
1215
  contentPadding,
1212
- canvas.height - totalHeight - contentPadding
1216
+ canvas.height - totalHeight - contentPadding - titlePadding
1213
1217
  );
1214
1218
  } else if (groupPosition === "top" /* Top */) {
1215
1219
  baseOrigin = new Point(contentPadding, contentPadding);
@@ -1477,16 +1481,28 @@ var ImageProcessor = class {
1477
1481
  );
1478
1482
  });
1479
1483
  }
1480
- compress(canvas, filename, lastModified, type, quality) {
1484
+ compress(canvas, filename, lastModified, type, quality, lastCompressFileSize = -1) {
1481
1485
  return canvas2File(canvas, filename, lastModified, type, quality).then(
1482
1486
  (file) => {
1483
- if (file.size > this.compressOptions.photoFileSizeLimit * 1024 && quality - JPEG_QUALITY_STEP > MIN_JPEG_QUALITY) {
1487
+ let qualityStep = JPEG_QUALITY_STEP;
1488
+ if (file.size > this.compressOptions.photoFileSizeLimit * 1024) {
1489
+ let factor = Math.floor(file.size / (1024 * this.compressOptions.photoFileSizeLimit));
1490
+ if (factor > 1 && quality > MIN_JPEG_QUALITY) {
1491
+ qualityStep = this.roundToDecimalPlace(JPEG_QUALITY_STEP * factor, 2);
1492
+ let maxQualityStep = this.roundToDecimalPlace(quality - MIN_JPEG_QUALITY, 2);
1493
+ qualityStep = Math.min(qualityStep, maxQualityStep);
1494
+ }
1495
+ }
1496
+ let compressFileSizeChanged = lastCompressFileSize === -1 || file.size < lastCompressFileSize;
1497
+ let nextCompressQuality = this.roundToDecimalPlace(quality - qualityStep, 2);
1498
+ if (compressFileSizeChanged && file.size > this.compressOptions.photoFileSizeLimit * 1024 && this.compareFloats(nextCompressQuality, MIN_JPEG_QUALITY) >= 0) {
1484
1499
  return this.compress(
1485
1500
  canvas,
1486
1501
  filename,
1487
1502
  lastModified,
1488
1503
  type,
1489
- quality - JPEG_QUALITY_STEP
1504
+ this.roundToDecimalPlace(nextCompressQuality, 2),
1505
+ file.size
1490
1506
  );
1491
1507
  } else {
1492
1508
  return Promise.resolve(file);
@@ -1494,6 +1510,25 @@ var ImageProcessor = class {
1494
1510
  }
1495
1511
  );
1496
1512
  }
1513
+ /**
1514
+ * 浮点型数据比较大小
1515
+ */
1516
+ compareFloats(a, b) {
1517
+ const diff = Math.abs(a - b);
1518
+ if (diff < Number.EPSILON) {
1519
+ return 0;
1520
+ }
1521
+ return a - b > 0 ? 1 : -1;
1522
+ }
1523
+ /**
1524
+ * 浮点型数据保留小数(四舍五入)
1525
+ * @param num 浮点型数据
1526
+ * @param decimalPlaces 保留的小数位数
1527
+ */
1528
+ roundToDecimalPlace(num, decimalPlaces) {
1529
+ const factor = Math.pow(10, decimalPlaces);
1530
+ return Math.round(num * factor) / factor;
1531
+ }
1497
1532
  };
1498
1533
  export {
1499
1534
  CompressOptions,
package/package.json CHANGED
@@ -1,10 +1,19 @@
1
1
  {
2
2
  "name": "@egova-mobile/app-media-utils",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "description": "",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/index.esm.mjs",
7
7
  "typings": "dist/index.d.ts",
8
+ "scripts": {
9
+ "clean": "rimraf ./dist",
10
+ "dev": "node ./build.js -w",
11
+ "build:types": "tsc -p ./tsconfig.json --emitDeclarationOnly",
12
+ "build:bundle": "node ./build.js",
13
+ "build": "pnpm clean && pnpm build:bundle && pnpm build:types",
14
+ "release": "zartui-mobile-cli release",
15
+ "prepare": "pnpm build"
16
+ },
8
17
  "publishConfig": {
9
18
  "access": "public",
10
19
  "registry": "https://registry.npmjs.org/"
@@ -32,13 +41,5 @@
32
41
  },
33
42
  "peerDependencies": {
34
43
  "dayjs": "^1.11.8"
35
- },
36
- "scripts": {
37
- "clean": "rimraf ./dist",
38
- "dev": "node ./build.js -w",
39
- "build:types": "tsc -p ./tsconfig.json --emitDeclarationOnly",
40
- "build:bundle": "node ./build.js",
41
- "build": "pnpm clean && pnpm build:bundle && pnpm build:types",
42
- "release": "zartui-mobile-cli release"
43
44
  }
44
- }
45
+ }