| export const getBase64 = async (file: File): Promise<string> => |
| new Promise((resolve) => { |
| const reader = new FileReader() |
| reader.readAsDataURL(file) |
| reader.onload = () => { |
| const baseURL = reader.result |
| resolve(baseURL as string) |
| } |
| }) |
|
|
| export function compressImage( |
| base64Image: string, |
| size: number |
| ): Promise<string> { |
| |
| const canvas = document.createElement('canvas') |
| const ctx = canvas.getContext('2d') |
|
|
| |
| const image = new Image() |
|
|
| |
| image.src = base64Image |
|
|
| return new Promise((resolve) => { |
| |
| image.onload = () => { |
| |
| const width = Math.min(size, image.width) |
| const height = (image.height / image.width) * width |
|
|
| canvas.width = width |
| canvas.height = height |
|
|
| |
| ctx?.drawImage(image, 0, 0, canvas.width, canvas.height) |
|
|
| |
| const compressedBase64Image = canvas.toDataURL(`image/jpeg`, 1) |
|
|
| |
| return resolve(compressedBase64Image) |
| } |
| }) |
| } |
|
|