@egova-mobile/app-media-utils 0.0.4 → 0.0.6

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.
@@ -5,7 +5,8 @@ export declare class ResizeOptions {
5
5
  }
6
6
  export declare class CompressOptions {
7
7
  photoFileSizeLimit: number;
8
- constructor(photoFileSizeLimit: number);
8
+ keepFileType: boolean;
9
+ constructor(photoFileSizeLimit: number, keepFileType?: boolean);
9
10
  }
10
11
  export declare class ImageProcessor {
11
12
  private file;
@@ -14,6 +15,10 @@ export declare class ImageProcessor {
14
15
  private compressOptions;
15
16
  constructor(file: File, resizeOptions: ResizeOptions, watermarkOptions: WatermarkOptions, compressOptions: CompressOptions);
16
17
  process(): Promise<void | File>;
18
+ /**
19
+ * 根据 MIME 类型获取文件扩展名
20
+ */
21
+ private getExtensionFromMimeType;
17
22
  compress(canvas: HTMLCanvasElement, filename: string, lastModified: number, type: string, quality: number, lastCompressFileSize?: number): Promise<File>;
18
23
  /**
19
24
  * 浮点型数据比较大小
package/dist/index.cjs.js CHANGED
@@ -1216,7 +1216,7 @@ function drawGroupedWatermarks(canvas, watermarks, groupPosition) {
1216
1216
  const tile = buildTile(canvas, watermark);
1217
1217
  if (tile) {
1218
1218
  tiles.push(tile);
1219
- totalHeight += tile.size.height + CONTENT_PADDING;
1219
+ totalHeight += tile.size.height + tile.style.paddingTop + tile.style.paddingBottom;
1220
1220
  }
1221
1221
  }
1222
1222
  if (totalHeight <= 0) {
@@ -1226,19 +1226,15 @@ 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;
1230
1229
  if (groupPosition === "center" /* Center */) {
1231
1230
  baseOrigin = new Point(
1232
1231
  contentPadding,
1233
1232
  Math.round((canvas.height - totalHeight) / 2)
1234
1233
  );
1235
1234
  } else if (groupPosition === "bottom" /* Bottom */) {
1236
- tiles.forEach((item) => {
1237
- titlePadding += item.style.paddingTop + item.style.paddingBottom;
1238
- });
1239
1235
  baseOrigin = new Point(
1240
1236
  contentPadding,
1241
- canvas.height - totalHeight - contentPadding - titlePadding
1237
+ canvas.height - totalHeight - contentPadding
1242
1238
  );
1243
1239
  } else if (groupPosition === "top" /* Top */) {
1244
1240
  baseOrigin = new Point(contentPadding, contentPadding);
@@ -1372,9 +1368,10 @@ function image2Canvas(img, type, width, height) {
1372
1368
  canvas.height = _height;
1373
1369
  const ctx = canvas.getContext("2d");
1374
1370
  if (ctx) {
1375
- ctx.fillStyle = "transparent";
1376
- if (type === "image/jpeg") {
1371
+ const needWhiteBackground = type === "image/jpeg" || type === "image/jpg" || type === "image/bmp";
1372
+ if (needWhiteBackground) {
1377
1373
  ctx.fillStyle = "#fff";
1374
+ ctx.fillRect(0, 0, _width, _height);
1378
1375
  }
1379
1376
  ctx.drawImage(img, 0, 0, _width, _height);
1380
1377
  return Promise.resolve(canvas);
@@ -1459,13 +1456,14 @@ var ResizeOptions = class {
1459
1456
  }
1460
1457
  };
1461
1458
  var CompressOptions = class {
1462
- // 图片大小限制,单位kb
1463
- constructor(photoFileSizeLimit) {
1459
+ // 是否保持原始文件类型
1460
+ constructor(photoFileSizeLimit, keepFileType = false) {
1464
1461
  if (checkIsUndefinedOrNull(photoFileSizeLimit)) {
1465
1462
  this.photoFileSizeLimit = PHOTO_FILE_SIZE_LIMIT;
1466
1463
  } else {
1467
1464
  this.photoFileSizeLimit = photoFileSizeLimit;
1468
1465
  }
1466
+ this.keepFileType = keepFileType;
1469
1467
  }
1470
1468
  };
1471
1469
  var ImageProcessor = class {
@@ -1479,12 +1477,15 @@ var ImageProcessor = class {
1479
1477
  process() {
1480
1478
  const filename = this.file.name;
1481
1479
  const { lastModified } = this.file;
1482
- let fileType = this.file.type;
1483
- if (this.file.size > this.compressOptions.photoFileSizeLimit * 1024) {
1484
- fileType = "image/jpeg";
1485
- }
1486
- if (fileType === "image/gif") {
1487
- fileType = "image/png";
1480
+ const originalFileType = this.file.type;
1481
+ let fileType = originalFileType;
1482
+ if (!this.compressOptions.keepFileType) {
1483
+ if (this.file.size > this.compressOptions.photoFileSizeLimit * 1024) {
1484
+ fileType = "image/jpeg";
1485
+ }
1486
+ if (fileType === "image/gif") {
1487
+ fileType = "image/png";
1488
+ }
1488
1489
  }
1489
1490
  return file2Image(this.file).then((img) => {
1490
1491
  return resize(img, {
@@ -1496,7 +1497,13 @@ var ImageProcessor = class {
1496
1497
  const canvas = result;
1497
1498
  return yield renderWatermark(canvas, this.watermarkOptions);
1498
1499
  })).then((canvas) => {
1499
- const exportFilename = filename.replace(/\./g, "-") + ".jpg";
1500
+ let exportFilename;
1501
+ if (this.compressOptions.keepFileType) {
1502
+ const ext = this.getExtensionFromMimeType(fileType);
1503
+ exportFilename = filename.replace(/\.[^.]+$/, "") + "." + ext;
1504
+ } else {
1505
+ exportFilename = filename.replace(/\./g, "-") + ".jpg";
1506
+ }
1500
1507
  return this.compress(
1501
1508
  canvas,
1502
1509
  exportFilename,
@@ -1506,6 +1513,21 @@ var ImageProcessor = class {
1506
1513
  );
1507
1514
  });
1508
1515
  }
1516
+ /**
1517
+ * 根据 MIME 类型获取文件扩展名
1518
+ */
1519
+ getExtensionFromMimeType(mimeType) {
1520
+ const mimeToExt = {
1521
+ "image/jpeg": "jpg",
1522
+ "image/jpg": "jpg",
1523
+ "image/png": "png",
1524
+ "image/gif": "gif",
1525
+ "image/webp": "webp",
1526
+ "image/bmp": "bmp",
1527
+ "image/svg+xml": "svg"
1528
+ };
1529
+ return mimeToExt[mimeType] || "jpg";
1530
+ }
1509
1531
  compress(canvas, filename, lastModified, type, quality, lastCompressFileSize = -1) {
1510
1532
  return canvas2File(canvas, filename, lastModified, type, quality).then(
1511
1533
  (file) => {
@@ -1191,7 +1191,7 @@ function drawGroupedWatermarks(canvas, watermarks, groupPosition) {
1191
1191
  const tile = buildTile(canvas, watermark);
1192
1192
  if (tile) {
1193
1193
  tiles.push(tile);
1194
- totalHeight += tile.size.height + CONTENT_PADDING;
1194
+ totalHeight += tile.size.height + tile.style.paddingTop + tile.style.paddingBottom;
1195
1195
  }
1196
1196
  }
1197
1197
  if (totalHeight <= 0) {
@@ -1201,19 +1201,15 @@ 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;
1205
1204
  if (groupPosition === "center" /* Center */) {
1206
1205
  baseOrigin = new Point(
1207
1206
  contentPadding,
1208
1207
  Math.round((canvas.height - totalHeight) / 2)
1209
1208
  );
1210
1209
  } else if (groupPosition === "bottom" /* Bottom */) {
1211
- tiles.forEach((item) => {
1212
- titlePadding += item.style.paddingTop + item.style.paddingBottom;
1213
- });
1214
1210
  baseOrigin = new Point(
1215
1211
  contentPadding,
1216
- canvas.height - totalHeight - contentPadding - titlePadding
1212
+ canvas.height - totalHeight - contentPadding
1217
1213
  );
1218
1214
  } else if (groupPosition === "top" /* Top */) {
1219
1215
  baseOrigin = new Point(contentPadding, contentPadding);
@@ -1347,9 +1343,10 @@ function image2Canvas(img, type, width, height) {
1347
1343
  canvas.height = _height;
1348
1344
  const ctx = canvas.getContext("2d");
1349
1345
  if (ctx) {
1350
- ctx.fillStyle = "transparent";
1351
- if (type === "image/jpeg") {
1346
+ const needWhiteBackground = type === "image/jpeg" || type === "image/jpg" || type === "image/bmp";
1347
+ if (needWhiteBackground) {
1352
1348
  ctx.fillStyle = "#fff";
1349
+ ctx.fillRect(0, 0, _width, _height);
1353
1350
  }
1354
1351
  ctx.drawImage(img, 0, 0, _width, _height);
1355
1352
  return Promise.resolve(canvas);
@@ -1434,13 +1431,14 @@ var ResizeOptions = class {
1434
1431
  }
1435
1432
  };
1436
1433
  var CompressOptions = class {
1437
- // 图片大小限制,单位kb
1438
- constructor(photoFileSizeLimit) {
1434
+ // 是否保持原始文件类型
1435
+ constructor(photoFileSizeLimit, keepFileType = false) {
1439
1436
  if (checkIsUndefinedOrNull(photoFileSizeLimit)) {
1440
1437
  this.photoFileSizeLimit = PHOTO_FILE_SIZE_LIMIT;
1441
1438
  } else {
1442
1439
  this.photoFileSizeLimit = photoFileSizeLimit;
1443
1440
  }
1441
+ this.keepFileType = keepFileType;
1444
1442
  }
1445
1443
  };
1446
1444
  var ImageProcessor = class {
@@ -1454,12 +1452,15 @@ var ImageProcessor = class {
1454
1452
  process() {
1455
1453
  const filename = this.file.name;
1456
1454
  const { lastModified } = this.file;
1457
- let fileType = this.file.type;
1458
- if (this.file.size > this.compressOptions.photoFileSizeLimit * 1024) {
1459
- fileType = "image/jpeg";
1460
- }
1461
- if (fileType === "image/gif") {
1462
- fileType = "image/png";
1455
+ const originalFileType = this.file.type;
1456
+ let fileType = originalFileType;
1457
+ if (!this.compressOptions.keepFileType) {
1458
+ if (this.file.size > this.compressOptions.photoFileSizeLimit * 1024) {
1459
+ fileType = "image/jpeg";
1460
+ }
1461
+ if (fileType === "image/gif") {
1462
+ fileType = "image/png";
1463
+ }
1463
1464
  }
1464
1465
  return file2Image(this.file).then((img) => {
1465
1466
  return resize(img, {
@@ -1471,7 +1472,13 @@ var ImageProcessor = class {
1471
1472
  const canvas = result;
1472
1473
  return yield renderWatermark(canvas, this.watermarkOptions);
1473
1474
  })).then((canvas) => {
1474
- const exportFilename = filename.replace(/\./g, "-") + ".jpg";
1475
+ let exportFilename;
1476
+ if (this.compressOptions.keepFileType) {
1477
+ const ext = this.getExtensionFromMimeType(fileType);
1478
+ exportFilename = filename.replace(/\.[^.]+$/, "") + "." + ext;
1479
+ } else {
1480
+ exportFilename = filename.replace(/\./g, "-") + ".jpg";
1481
+ }
1475
1482
  return this.compress(
1476
1483
  canvas,
1477
1484
  exportFilename,
@@ -1481,6 +1488,21 @@ var ImageProcessor = class {
1481
1488
  );
1482
1489
  });
1483
1490
  }
1491
+ /**
1492
+ * 根据 MIME 类型获取文件扩展名
1493
+ */
1494
+ getExtensionFromMimeType(mimeType) {
1495
+ const mimeToExt = {
1496
+ "image/jpeg": "jpg",
1497
+ "image/jpg": "jpg",
1498
+ "image/png": "png",
1499
+ "image/gif": "gif",
1500
+ "image/webp": "webp",
1501
+ "image/bmp": "bmp",
1502
+ "image/svg+xml": "svg"
1503
+ };
1504
+ return mimeToExt[mimeType] || "jpg";
1505
+ }
1484
1506
  compress(canvas, filename, lastModified, type, quality, lastCompressFileSize = -1) {
1485
1507
  return canvas2File(canvas, filename, lastModified, type, quality).then(
1486
1508
  (file) => {
package/package.json CHANGED
@@ -1,23 +1,10 @@
1
1
  {
2
2
  "name": "@egova-mobile/app-media-utils",
3
- "version": "0.0.4",
3
+ "version": "0.0.6",
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
- },
17
- "publishConfig": {
18
- "access": "public",
19
- "registry": "https://registry.npmjs.org/"
20
- },
21
8
  "exports": {
22
9
  ".": {
23
10
  "import": "./dist/index.esm.mjs",
@@ -41,5 +28,13 @@
41
28
  },
42
29
  "peerDependencies": {
43
30
  "dayjs": "^1.11.8"
31
+ },
32
+ "scripts": {
33
+ "clean": "rimraf ./dist",
34
+ "dev": "node ./build.js -w",
35
+ "build:types": "tsc -p ./tsconfig.json --emitDeclarationOnly",
36
+ "build:bundle": "node ./build.js",
37
+ "build": "pnpm clean && pnpm build:bundle && pnpm build:types",
38
+ "release": "zartui-mobile-cli release"
44
39
  }
45
- }
40
+ }