@egova-mobile/app-media-utils 0.0.5 → 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.
- package/dist/image/image-processor.d.ts +6 -1
- package/dist/index.cjs.js +37 -11
- package/dist/index.esm.mjs +37 -11
- package/package.json +10 -15
|
@@ -5,7 +5,8 @@ export declare class ResizeOptions {
|
|
|
5
5
|
}
|
|
6
6
|
export declare class CompressOptions {
|
|
7
7
|
photoFileSizeLimit: number;
|
|
8
|
-
|
|
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
|
@@ -1368,9 +1368,10 @@ function image2Canvas(img, type, width, height) {
|
|
|
1368
1368
|
canvas.height = _height;
|
|
1369
1369
|
const ctx = canvas.getContext("2d");
|
|
1370
1370
|
if (ctx) {
|
|
1371
|
-
|
|
1372
|
-
if (
|
|
1371
|
+
const needWhiteBackground = type === "image/jpeg" || type === "image/jpg" || type === "image/bmp";
|
|
1372
|
+
if (needWhiteBackground) {
|
|
1373
1373
|
ctx.fillStyle = "#fff";
|
|
1374
|
+
ctx.fillRect(0, 0, _width, _height);
|
|
1374
1375
|
}
|
|
1375
1376
|
ctx.drawImage(img, 0, 0, _width, _height);
|
|
1376
1377
|
return Promise.resolve(canvas);
|
|
@@ -1455,13 +1456,14 @@ var ResizeOptions = class {
|
|
|
1455
1456
|
}
|
|
1456
1457
|
};
|
|
1457
1458
|
var CompressOptions = class {
|
|
1458
|
-
//
|
|
1459
|
-
constructor(photoFileSizeLimit) {
|
|
1459
|
+
// 是否保持原始文件类型
|
|
1460
|
+
constructor(photoFileSizeLimit, keepFileType = false) {
|
|
1460
1461
|
if (checkIsUndefinedOrNull(photoFileSizeLimit)) {
|
|
1461
1462
|
this.photoFileSizeLimit = PHOTO_FILE_SIZE_LIMIT;
|
|
1462
1463
|
} else {
|
|
1463
1464
|
this.photoFileSizeLimit = photoFileSizeLimit;
|
|
1464
1465
|
}
|
|
1466
|
+
this.keepFileType = keepFileType;
|
|
1465
1467
|
}
|
|
1466
1468
|
};
|
|
1467
1469
|
var ImageProcessor = class {
|
|
@@ -1475,12 +1477,15 @@ var ImageProcessor = class {
|
|
|
1475
1477
|
process() {
|
|
1476
1478
|
const filename = this.file.name;
|
|
1477
1479
|
const { lastModified } = this.file;
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
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
|
+
}
|
|
1484
1489
|
}
|
|
1485
1490
|
return file2Image(this.file).then((img) => {
|
|
1486
1491
|
return resize(img, {
|
|
@@ -1492,7 +1497,13 @@ var ImageProcessor = class {
|
|
|
1492
1497
|
const canvas = result;
|
|
1493
1498
|
return yield renderWatermark(canvas, this.watermarkOptions);
|
|
1494
1499
|
})).then((canvas) => {
|
|
1495
|
-
|
|
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
|
+
}
|
|
1496
1507
|
return this.compress(
|
|
1497
1508
|
canvas,
|
|
1498
1509
|
exportFilename,
|
|
@@ -1502,6 +1513,21 @@ var ImageProcessor = class {
|
|
|
1502
1513
|
);
|
|
1503
1514
|
});
|
|
1504
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
|
+
}
|
|
1505
1531
|
compress(canvas, filename, lastModified, type, quality, lastCompressFileSize = -1) {
|
|
1506
1532
|
return canvas2File(canvas, filename, lastModified, type, quality).then(
|
|
1507
1533
|
(file) => {
|
package/dist/index.esm.mjs
CHANGED
|
@@ -1343,9 +1343,10 @@ function image2Canvas(img, type, width, height) {
|
|
|
1343
1343
|
canvas.height = _height;
|
|
1344
1344
|
const ctx = canvas.getContext("2d");
|
|
1345
1345
|
if (ctx) {
|
|
1346
|
-
|
|
1347
|
-
if (
|
|
1346
|
+
const needWhiteBackground = type === "image/jpeg" || type === "image/jpg" || type === "image/bmp";
|
|
1347
|
+
if (needWhiteBackground) {
|
|
1348
1348
|
ctx.fillStyle = "#fff";
|
|
1349
|
+
ctx.fillRect(0, 0, _width, _height);
|
|
1349
1350
|
}
|
|
1350
1351
|
ctx.drawImage(img, 0, 0, _width, _height);
|
|
1351
1352
|
return Promise.resolve(canvas);
|
|
@@ -1430,13 +1431,14 @@ var ResizeOptions = class {
|
|
|
1430
1431
|
}
|
|
1431
1432
|
};
|
|
1432
1433
|
var CompressOptions = class {
|
|
1433
|
-
//
|
|
1434
|
-
constructor(photoFileSizeLimit) {
|
|
1434
|
+
// 是否保持原始文件类型
|
|
1435
|
+
constructor(photoFileSizeLimit, keepFileType = false) {
|
|
1435
1436
|
if (checkIsUndefinedOrNull(photoFileSizeLimit)) {
|
|
1436
1437
|
this.photoFileSizeLimit = PHOTO_FILE_SIZE_LIMIT;
|
|
1437
1438
|
} else {
|
|
1438
1439
|
this.photoFileSizeLimit = photoFileSizeLimit;
|
|
1439
1440
|
}
|
|
1441
|
+
this.keepFileType = keepFileType;
|
|
1440
1442
|
}
|
|
1441
1443
|
};
|
|
1442
1444
|
var ImageProcessor = class {
|
|
@@ -1450,12 +1452,15 @@ var ImageProcessor = class {
|
|
|
1450
1452
|
process() {
|
|
1451
1453
|
const filename = this.file.name;
|
|
1452
1454
|
const { lastModified } = this.file;
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
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
|
+
}
|
|
1459
1464
|
}
|
|
1460
1465
|
return file2Image(this.file).then((img) => {
|
|
1461
1466
|
return resize(img, {
|
|
@@ -1467,7 +1472,13 @@ var ImageProcessor = class {
|
|
|
1467
1472
|
const canvas = result;
|
|
1468
1473
|
return yield renderWatermark(canvas, this.watermarkOptions);
|
|
1469
1474
|
})).then((canvas) => {
|
|
1470
|
-
|
|
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
|
+
}
|
|
1471
1482
|
return this.compress(
|
|
1472
1483
|
canvas,
|
|
1473
1484
|
exportFilename,
|
|
@@ -1477,6 +1488,21 @@ var ImageProcessor = class {
|
|
|
1477
1488
|
);
|
|
1478
1489
|
});
|
|
1479
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
|
+
}
|
|
1480
1506
|
compress(canvas, filename, lastModified, type, quality, lastCompressFileSize = -1) {
|
|
1481
1507
|
return canvas2File(canvas, filename, lastModified, type, quality).then(
|
|
1482
1508
|
(file) => {
|
package/package.json
CHANGED
|
@@ -1,23 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@egova-mobile/app-media-utils",
|
|
3
|
-
"version": "0.0.
|
|
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
|
+
}
|