szld-libs 0.2.24 → 0.2.26

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.
@@ -0,0 +1,8 @@
1
+ export declare const fileToDataURL: (file: Blob | File) => Promise<any>;
2
+ export declare const dataURLToImage: (dataURL: string) => Promise<HTMLImageElement>;
3
+ export declare const canvastoFile: (canvas: HTMLCanvasElement, type: string, quality: number) => Promise<Blob | null>;
4
+ export declare const scaleWidthHeight: (img: HTMLImageElement) => {
5
+ width: number;
6
+ height: number;
7
+ };
8
+ export declare const convertQualityToBit: (quality: number) => number;
@@ -1,16 +1,18 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
3
  const fileToDataURL = (file) => {
4
- return new Promise((resolve) => {
4
+ return new Promise((resolve, reject) => {
5
5
  const reader = new FileReader();
6
6
  reader.onloadend = (e) => resolve(e.target.result);
7
+ reader.onerror = (e) => reject(e);
7
8
  reader.readAsDataURL(file);
8
9
  });
9
10
  };
10
11
  const dataURLToImage = (dataURL) => {
11
- return new Promise((resolve) => {
12
+ return new Promise((resolve, reject) => {
12
13
  const img = new Image();
13
14
  img.onload = () => resolve(img);
15
+ img.onerror = (e) => reject(e);
14
16
  img.src = dataURL;
15
17
  });
16
18
  };
@@ -20,8 +22,8 @@ const canvastoFile = (canvas, type, quality) => {
20
22
  );
21
23
  };
22
24
  const scaleWidthHeight = (img) => {
23
- const maxWidth = 1400;
24
- const maxHeight = 1400;
25
+ const maxWidth = 2e3;
26
+ const maxHeight = 2e3;
25
27
  const originWidth = img.width;
26
28
  const originHeight = img.height;
27
29
  let targetWidth = img.width;
@@ -40,26 +42,17 @@ const scaleWidthHeight = (img) => {
40
42
  height: targetHeight
41
43
  };
42
44
  };
43
- const compressionFile = async (file, type = "image/jpeg", quality = 0.5, limit = 300) => {
44
- const fileName = file.name;
45
- if (file.size / 1024 < limit) {
46
- return file;
45
+ const convertQualityToBit = (quality) => {
46
+ let bit = 0;
47
+ if (quality > 1 || quality < 0) {
48
+ bit = 0;
49
+ } else {
50
+ bit = !quality ? 0 : quality * 256;
47
51
  }
48
- const canvas = document.createElement("canvas");
49
- const context = canvas.getContext("2d");
50
- const base64 = await fileToDataURL(file);
51
- const img = await dataURLToImage(base64);
52
- const target = scaleWidthHeight(img);
53
- canvas.width = target.width;
54
- canvas.height = target.height;
55
- context.clearRect(0, 0, target.width, target.height);
56
- context.drawImage(img, 0, 0, target.width, target.height);
57
- const blob = await canvastoFile(canvas, type, quality);
58
- const newFile = new File([blob], fileName, {
59
- type
60
- });
61
- return newFile;
52
+ return bit;
62
53
  };
63
- exports.compressionFile = compressionFile;
54
+ exports.canvastoFile = canvastoFile;
55
+ exports.convertQualityToBit = convertQualityToBit;
64
56
  exports.dataURLToImage = dataURLToImage;
65
57
  exports.fileToDataURL = fileToDataURL;
58
+ exports.scaleWidthHeight = scaleWidthHeight;
@@ -0,0 +1,9 @@
1
+ /**
2
+ * 图片压缩方法
3
+ * @param {Object} file 图片文件
4
+ * @param {Nubmber} quality 压缩质量参数 0-1
5
+ * @param {limit} limit 最小限制大小,小于该值时不进行压缩
6
+ * @returns 压缩后的新图片
7
+ */
8
+ declare const compressionImage: (file: File, quality?: number, limit?: number) => Promise<File>;
9
+ export default compressionImage;
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ const UPNG = require("./UPNG");
3
+ const image = require("./image");
4
+ const compressionImage = async (file, quality = 0.5, limit = 300) => {
5
+ const fileName = file.name;
6
+ if (file.size / 1024 < limit) {
7
+ return file;
8
+ }
9
+ try {
10
+ const canvas = document.createElement("canvas");
11
+ const context = canvas.getContext("2d");
12
+ const base64 = await image.fileToDataURL(file);
13
+ const img = await image.dataURLToImage(base64);
14
+ const target = image.scaleWidthHeight(img);
15
+ canvas.width = target.width;
16
+ canvas.height = target.height;
17
+ context.clearRect(0, 0, target.width, target.height);
18
+ context.drawImage(img, 0, 0, target.width, target.height);
19
+ if (["image/jpeg", "image/jpg"].includes(file.type)) {
20
+ const blob = await image.canvastoFile(canvas, file.type, quality);
21
+ const newFile = new File([blob], fileName, {
22
+ type: file.type
23
+ });
24
+ return newFile;
25
+ } else {
26
+ const imageData = context.getImageData(0, 0, target.width, target.height).data;
27
+ const bit = image.convertQualityToBit(quality);
28
+ const png = UPNG.encode([imageData.buffer], target.width, target.height, bit);
29
+ const newFile = new File([png], file.name, {
30
+ type: file.type
31
+ });
32
+ return newFile;
33
+ }
34
+ } catch (error) {
35
+ return file;
36
+ }
37
+ };
38
+ module.exports = compressionImage;
@@ -0,0 +1 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="31.88" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 257"><defs><linearGradient id="IconifyId1813088fe1fbc01fb466" x1="-.828%" x2="57.636%" y1="7.652%" y2="78.411%"><stop offset="0%" stop-color="#41D1FF"></stop><stop offset="100%" stop-color="#BD34FE"></stop></linearGradient><linearGradient id="IconifyId1813088fe1fbc01fb467" x1="43.376%" x2="50.316%" y1="2.242%" y2="89.03%"><stop offset="0%" stop-color="#FFEA83"></stop><stop offset="8.333%" stop-color="#FFDD35"></stop><stop offset="100%" stop-color="#FFA800"></stop></linearGradient></defs><path fill="url(#IconifyId1813088fe1fbc01fb466)" d="M255.153 37.938L134.897 252.976c-2.483 4.44-8.862 4.466-11.382.048L.875 37.958c-2.746-4.814 1.371-10.646 6.827-9.67l120.385 21.517a6.537 6.537 0 0 0 2.322-.004l117.867-21.483c5.438-.991 9.574 4.796 6.877 9.62Z"></path><path fill="url(#IconifyId1813088fe1fbc01fb467)" d="M185.432.063L96.44 17.501a3.268 3.268 0 0 0-2.634 3.014l-5.474 92.456a3.268 3.268 0 0 0 3.997 3.378l24.777-5.718c2.318-.535 4.413 1.507 3.936 3.838l-7.361 36.047c-.495 2.426 1.782 4.5 4.151 3.78l15.304-4.649c2.372-.72 4.652 1.36 4.15 3.788l-11.698 56.621c-.732 3.542 3.979 5.473 5.943 2.437l1.313-2.028l72.516-144.72c1.215-2.423-.88-5.186-3.54-4.672l-25.505 4.922c-2.396.462-4.435-1.77-3.759-4.114l16.646-57.705c.677-2.35-1.37-4.583-3.769-4.113Z"></path></svg>
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "szld-libs",
3
3
  "private": false,
4
- "version": "0.2.24",
4
+ "version": "0.2.26",
5
5
  "type": "module",
6
6
  "scripts": {
7
7
  "dev": "vite",
@@ -13,6 +13,7 @@
13
13
  "antd": "^5.15.1",
14
14
  "crypto-js": "^4.2.0",
15
15
  "lodash": "^4.17.21",
16
+ "pako": "^2.1.0",
16
17
  "react": "^18.2.0",
17
18
  "react-dom": "^18.2.0",
18
19
  "react-router-dom": "^6.6.1",
@@ -1,11 +0,0 @@
1
- export declare const fileToDataURL: (file: Blob | File) => Promise<any>;
2
- export declare const dataURLToImage: (dataURL: string) => Promise<HTMLImageElement>;
3
- /**
4
- * 图片压缩方法
5
- * @param {Object} file 图片文件
6
- * @param {String} type 想压缩成的文件类型
7
- * @param {Nubmber} quality 压缩质量参数
8
- * @param {limit} limit 最小限制大小,小于该值时不进行压缩
9
- * @returns 压缩后的新图片
10
- */
11
- export declare const compressionFile: (file: File, type?: string, quality?: number, limit?: number) => Promise<File>;
@@ -1,11 +0,0 @@
1
- export declare const fileToDataURL: (file: Blob | File) => Promise<any>;
2
- export declare const dataURLToImage: (dataURL: string) => Promise<HTMLImageElement>;
3
- /**
4
- * 图片压缩方法
5
- * @param {Object} file 图片文件
6
- * @param {String} type 想压缩成的文件类型
7
- * @param {Nubmber} quality 压缩质量参数
8
- * @param {limit} limit 最小限制大小,小于该值时不进行压缩
9
- * @returns 压缩后的新图片
10
- */
11
- export declare const compressionFile: (file: File, type?: string, quality?: number, limit?: number) => Promise<File>;