Spaces:
Paused
Paused
| import imageCompression from 'browser-image-compression'; | |
| export const compressImage = async (file: File): Promise<File> => { | |
| const options = { | |
| maxSizeMB: 0.5, // Compress to 500KB | |
| maxWidthOrHeight: 1080, // Resize to HD mobile width | |
| useWebWorker: true, // Fast CPU processing | |
| fileType: 'image/webp' // Efficient format | |
| }; | |
| try { | |
| const compressedFile = await imageCompression(file, options); | |
| console.log(`Compressed: ${(file.size / 1024 / 1024).toFixed(2)}MB -> ${(compressedFile.size / 1024 / 1024).toFixed(2)}MB`); | |
| return compressedFile; | |
| } catch (error) { | |
| console.error("Compression failed:", error); | |
| return file; // Return original if compression fails | |
| } | |
| }; | |