kunkeji commited on
Commit
e499529
·
1 Parent(s): ca5d709

Image compression (#3749)

Browse files

### What problem does this PR solve?

The uploaded avatar has been compressed to preserve transparency while
meeting the length requirements for the 'text' type. The current
compressed size is 100x100 pixels.

### Type of change

- [x] Bug Fix (non-breaking change which fixes an issue)

Files changed (1) hide show
  1. web/src/utils/file-util.ts +41 -1
web/src/utils/file-util.ts CHANGED
@@ -5,7 +5,41 @@ export const transformFile2Base64 = (val: any): Promise<any> => {
5
  const reader = new FileReader();
6
  reader.readAsDataURL(val);
7
  reader.onload = (): void => {
8
- resolve(reader.result);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  };
10
  reader.onerror = reject;
11
  });
@@ -15,6 +49,7 @@ export const transformBase64ToFile = (
15
  dataUrl: string,
16
  filename: string = 'file',
17
  ) => {
 
18
  let arr = dataUrl.split(','),
19
  bstr = atob(arr[1]),
20
  n = bstr.length,
@@ -30,6 +65,7 @@ export const transformBase64ToFile = (
30
  };
31
 
32
  export const normFile = (e: any) => {
 
33
  if (Array.isArray(e)) {
34
  return e;
35
  }
@@ -37,6 +73,7 @@ export const normFile = (e: any) => {
37
  };
38
 
39
  export const getUploadFileListFromBase64 = (avatar: string) => {
 
40
  let fileList: UploadFile[] = [];
41
 
42
  if (avatar) {
@@ -47,6 +84,7 @@ export const getUploadFileListFromBase64 = (avatar: string) => {
47
  };
48
 
49
  export const getBase64FromUploadFileList = async (fileList?: UploadFile[]) => {
 
50
  if (Array.isArray(fileList) && fileList.length > 0) {
51
  const file = fileList[0];
52
  const originFileObj = file.originFileObj;
@@ -71,6 +109,7 @@ export const downloadFile = ({
71
  filename?: string;
72
  target?: string;
73
  }) => {
 
74
  const downloadElement = document.createElement('a');
75
  downloadElement.style.display = 'none';
76
  downloadElement.href = url;
@@ -89,6 +128,7 @@ export const downloadFile = ({
89
  const Units = ['bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
90
 
91
  export const formatBytes = (x: string | number) => {
 
92
  let l = 0,
93
  n = (typeof x === 'string' ? parseInt(x, 10) : x) || 0;
94
 
 
5
  const reader = new FileReader();
6
  reader.readAsDataURL(val);
7
  reader.onload = (): void => {
8
+ // 创建图片对象
9
+ const img = new Image();
10
+ img.src = reader.result as string;
11
+
12
+ img.onload = () => {
13
+ // 创建canvas
14
+ const canvas = document.createElement('canvas');
15
+ const ctx = canvas.getContext('2d');
16
+
17
+ // 计算压缩后的尺寸,最大宽高设为800px
18
+ let width = img.width;
19
+ let height = img.height;
20
+ const maxSize = 100;
21
+
22
+ if (width > height && width > maxSize) {
23
+ height = (height * maxSize) / width;
24
+ width = maxSize;
25
+ } else if (height > maxSize) {
26
+ width = (width * maxSize) / height;
27
+ height = maxSize;
28
+ }
29
+
30
+ // 设置canvas尺寸
31
+ canvas.width = width;
32
+ canvas.height = height;
33
+
34
+ // 绘制图片
35
+ ctx?.drawImage(img, 0, 0, width, height);
36
+
37
+ // 转换为base64,保持原始格式和透明度
38
+ const compressedBase64 = canvas.toDataURL('image/png');
39
+ resolve(compressedBase64);
40
+ };
41
+
42
+ img.onerror = reject;
43
  };
44
  reader.onerror = reject;
45
  });
 
49
  dataUrl: string,
50
  filename: string = 'file',
51
  ) => {
52
+ console.log('transformBase64ToFile', dataUrl);
53
  let arr = dataUrl.split(','),
54
  bstr = atob(arr[1]),
55
  n = bstr.length,
 
65
  };
66
 
67
  export const normFile = (e: any) => {
68
+ console.log('normFile', e);
69
  if (Array.isArray(e)) {
70
  return e;
71
  }
 
73
  };
74
 
75
  export const getUploadFileListFromBase64 = (avatar: string) => {
76
+ console.log('getUploadFileListFromBase64', avatar);
77
  let fileList: UploadFile[] = [];
78
 
79
  if (avatar) {
 
84
  };
85
 
86
  export const getBase64FromUploadFileList = async (fileList?: UploadFile[]) => {
87
+ console.log('getBase64FromUploadFileList', fileList);
88
  if (Array.isArray(fileList) && fileList.length > 0) {
89
  const file = fileList[0];
90
  const originFileObj = file.originFileObj;
 
109
  filename?: string;
110
  target?: string;
111
  }) => {
112
+ console.log('downloadFile', url);
113
  const downloadElement = document.createElement('a');
114
  downloadElement.style.display = 'none';
115
  downloadElement.href = url;
 
128
  const Units = ['bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
129
 
130
  export const formatBytes = (x: string | number) => {
131
+ console.log('formatBytes', x);
132
  let l = 0,
133
  n = (typeof x === 'string' ? parseInt(x, 10) : x) || 0;
134