balibabu
commited on
Commit
·
bf8dab3
1
Parent(s):
e499529
Feat: Translate comments of file-util.ts to English #3749 (#3757)
Browse files### What problem does this PR solve?
Feat: Translate comments of file-util.ts to English #3749
### Type of change
- [x] New Feature (non-breaking change which adds functionality)
- web/src/utils/file-util.ts +13 -13
web/src/utils/file-util.ts
CHANGED
|
@@ -5,20 +5,20 @@ export const transformFile2Base64 = (val: any): Promise<any> => {
|
|
| 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 |
-
//
|
| 14 |
const canvas = document.createElement('canvas');
|
| 15 |
const ctx = canvas.getContext('2d');
|
| 16 |
-
|
| 17 |
-
//
|
| 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;
|
|
@@ -26,19 +26,19 @@ export const transformFile2Base64 = (val: any): Promise<any> => {
|
|
| 26 |
width = (width * maxSize) / height;
|
| 27 |
height = maxSize;
|
| 28 |
}
|
| 29 |
-
|
| 30 |
-
//
|
| 31 |
canvas.width = width;
|
| 32 |
canvas.height = height;
|
| 33 |
-
|
| 34 |
-
//
|
| 35 |
ctx?.drawImage(img, 0, 0, width, height);
|
| 36 |
-
|
| 37 |
-
//
|
| 38 |
const compressedBase64 = canvas.toDataURL('image/png');
|
| 39 |
resolve(compressedBase64);
|
| 40 |
};
|
| 41 |
-
|
| 42 |
img.onerror = reject;
|
| 43 |
};
|
| 44 |
reader.onerror = reject;
|
|
|
|
| 5 |
const reader = new FileReader();
|
| 6 |
reader.readAsDataURL(val);
|
| 7 |
reader.onload = (): void => {
|
| 8 |
+
// Create image object
|
| 9 |
const img = new Image();
|
| 10 |
img.src = reader.result as string;
|
| 11 |
+
|
| 12 |
img.onload = () => {
|
| 13 |
+
// Create canvas
|
| 14 |
const canvas = document.createElement('canvas');
|
| 15 |
const ctx = canvas.getContext('2d');
|
| 16 |
+
|
| 17 |
+
// Calculate compressed dimensions, set max width/height to 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;
|
|
|
|
| 26 |
width = (width * maxSize) / height;
|
| 27 |
height = maxSize;
|
| 28 |
}
|
| 29 |
+
|
| 30 |
+
// Set canvas dimensions
|
| 31 |
canvas.width = width;
|
| 32 |
canvas.height = height;
|
| 33 |
+
|
| 34 |
+
// Draw image
|
| 35 |
ctx?.drawImage(img, 0, 0, width, height);
|
| 36 |
+
|
| 37 |
+
// Convert to base64, maintain original format and transparency
|
| 38 |
const compressedBase64 = canvas.toDataURL('image/png');
|
| 39 |
resolve(compressedBase64);
|
| 40 |
};
|
| 41 |
+
|
| 42 |
img.onerror = reject;
|
| 43 |
};
|
| 44 |
reader.onerror = reject;
|