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

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.
@@ -19,7 +19,7 @@ export declare class ImageProcessor {
19
19
  * 根据 MIME 类型获取文件扩展名
20
20
  */
21
21
  private getExtensionFromMimeType;
22
- compress(canvas: HTMLCanvasElement, filename: string, lastModified: number, type: string, quality: number, lastCompressFileSize?: number): Promise<File>;
22
+ compress(canvas: HTMLCanvasElement, filename: string, lastModified: number, type: string, quality: number, lastCompressFileSize?: number, originalFileType?: string): Promise<File>;
23
23
  /**
24
24
  * 浮点型数据比较大小
25
25
  */
@@ -33,7 +33,7 @@ export declare function canvas2Image(canvas: HTMLCanvasElement, type?: string, q
33
33
  * @param quality
34
34
  * @see {@link https://meshworld.in/convert-canvas-to-an-image-using-javascript/}
35
35
  */
36
- export declare function canvas2File(canvas: HTMLCanvasElement, fileName: string, lastModified: number, type?: string, quality?: number): Promise<File>;
36
+ export declare function canvas2File(canvas: HTMLCanvasElement, fileName: string, lastModified: number, type?: string, quality?: number, forceFileType?: string): Promise<File>;
37
37
  export type ResizeResultType = 'image' | 'canvas';
38
38
  /**
39
39
  * 缩放图片
package/dist/index.cjs.js CHANGED
@@ -1384,18 +1384,20 @@ function canvas2Image(canvas, type = "image/jpeg", quality = 0.8) {
1384
1384
  image.src = canvas.toDataURL(type, quality);
1385
1385
  return Promise.resolve(image);
1386
1386
  }
1387
- function canvas2File(canvas, fileName, lastModified, type = "image/jpeg", quality = 0.8) {
1387
+ function canvas2File(canvas, fileName, lastModified, type = "image/jpeg", quality = 0.8, forceFileType) {
1388
1388
  return new Promise((resolve, reject) => {
1389
+ const blobType = type === "image/gif" ? "image/png" : type;
1389
1390
  canvas.toBlob(
1390
1391
  (blob) => {
1391
1392
  if (!blob) {
1392
1393
  reject(new Error("canvas文件导出失败"));
1393
1394
  } else {
1394
- const file = new File([blob], fileName, { lastModified, type });
1395
+ const fileType = forceFileType || type;
1396
+ const file = new File([blob], fileName, { lastModified, type: fileType });
1395
1397
  resolve(file);
1396
1398
  }
1397
1399
  },
1398
- type,
1400
+ blobType,
1399
1401
  quality
1400
1402
  );
1401
1403
  });
@@ -1509,7 +1511,10 @@ var ImageProcessor = class {
1509
1511
  exportFilename,
1510
1512
  lastModified,
1511
1513
  fileType,
1512
- DEFAULT_JPEG_QUALITY
1514
+ DEFAULT_JPEG_QUALITY,
1515
+ -1,
1516
+ originalFileType
1517
+ // 传入原始文件类型,用于GIF等特殊格式
1513
1518
  );
1514
1519
  });
1515
1520
  }
@@ -1528,8 +1533,9 @@ var ImageProcessor = class {
1528
1533
  };
1529
1534
  return mimeToExt[mimeType] || "jpg";
1530
1535
  }
1531
- compress(canvas, filename, lastModified, type, quality, lastCompressFileSize = -1) {
1532
- return canvas2File(canvas, filename, lastModified, type, quality).then(
1536
+ compress(canvas, filename, lastModified, type, quality, lastCompressFileSize = -1, originalFileType) {
1537
+ const forceFileType = originalFileType === "image/gif" ? originalFileType : void 0;
1538
+ return canvas2File(canvas, filename, lastModified, type, quality, forceFileType).then(
1533
1539
  (file) => {
1534
1540
  let qualityStep = JPEG_QUALITY_STEP;
1535
1541
  if (file.size > this.compressOptions.photoFileSizeLimit * 1024) {
@@ -1549,7 +1555,8 @@ var ImageProcessor = class {
1549
1555
  lastModified,
1550
1556
  type,
1551
1557
  this.roundToDecimalPlace(nextCompressQuality, 2),
1552
- file.size
1558
+ file.size,
1559
+ originalFileType
1553
1560
  );
1554
1561
  } else {
1555
1562
  return Promise.resolve(file);
@@ -1359,18 +1359,20 @@ function canvas2Image(canvas, type = "image/jpeg", quality = 0.8) {
1359
1359
  image.src = canvas.toDataURL(type, quality);
1360
1360
  return Promise.resolve(image);
1361
1361
  }
1362
- function canvas2File(canvas, fileName, lastModified, type = "image/jpeg", quality = 0.8) {
1362
+ function canvas2File(canvas, fileName, lastModified, type = "image/jpeg", quality = 0.8, forceFileType) {
1363
1363
  return new Promise((resolve, reject) => {
1364
+ const blobType = type === "image/gif" ? "image/png" : type;
1364
1365
  canvas.toBlob(
1365
1366
  (blob) => {
1366
1367
  if (!blob) {
1367
1368
  reject(new Error("canvas文件导出失败"));
1368
1369
  } else {
1369
- const file = new File([blob], fileName, { lastModified, type });
1370
+ const fileType = forceFileType || type;
1371
+ const file = new File([blob], fileName, { lastModified, type: fileType });
1370
1372
  resolve(file);
1371
1373
  }
1372
1374
  },
1373
- type,
1375
+ blobType,
1374
1376
  quality
1375
1377
  );
1376
1378
  });
@@ -1484,7 +1486,10 @@ var ImageProcessor = class {
1484
1486
  exportFilename,
1485
1487
  lastModified,
1486
1488
  fileType,
1487
- DEFAULT_JPEG_QUALITY
1489
+ DEFAULT_JPEG_QUALITY,
1490
+ -1,
1491
+ originalFileType
1492
+ // 传入原始文件类型,用于GIF等特殊格式
1488
1493
  );
1489
1494
  });
1490
1495
  }
@@ -1503,8 +1508,9 @@ var ImageProcessor = class {
1503
1508
  };
1504
1509
  return mimeToExt[mimeType] || "jpg";
1505
1510
  }
1506
- compress(canvas, filename, lastModified, type, quality, lastCompressFileSize = -1) {
1507
- return canvas2File(canvas, filename, lastModified, type, quality).then(
1511
+ compress(canvas, filename, lastModified, type, quality, lastCompressFileSize = -1, originalFileType) {
1512
+ const forceFileType = originalFileType === "image/gif" ? originalFileType : void 0;
1513
+ return canvas2File(canvas, filename, lastModified, type, quality, forceFileType).then(
1508
1514
  (file) => {
1509
1515
  let qualityStep = JPEG_QUALITY_STEP;
1510
1516
  if (file.size > this.compressOptions.photoFileSizeLimit * 1024) {
@@ -1524,7 +1530,8 @@ var ImageProcessor = class {
1524
1530
  lastModified,
1525
1531
  type,
1526
1532
  this.roundToDecimalPlace(nextCompressQuality, 2),
1527
- file.size
1533
+ file.size,
1534
+ originalFileType
1528
1535
  );
1529
1536
  } else {
1530
1537
  return Promise.resolve(file);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@egova-mobile/app-media-utils",
3
- "version": "0.0.6",
3
+ "version": "0.0.7",
4
4
  "description": "",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/index.esm.mjs",