// Wspoldzielone narzedzia do obrazow (kompresja przy wgrywaniu). /** * Kompresja obrazu przy wgrywaniu: jesli szerokosc przekracza maxWidth, skalujemy do maxWidth * (proporcjonalnie), inaczej zostawiamy oryginalny rozmiar. Zwraca dataURL JPEG. Gdy cos zawiedzie, * wraca obiektowy URL oryginalu, zeby dodawanie zdjec zawsze dzialalo. */ export async function compressImageFile(file: File, maxWidth = 1024, quality = 0.82): Promise { try { const bitmap = await createImageBitmap(file); const scale = Math.min(1, maxWidth / bitmap.width); const width = Math.max(1, Math.round(bitmap.width * scale)); const height = Math.max(1, Math.round(bitmap.height * scale)); const canvas = document.createElement('canvas'); canvas.width = width; canvas.height = height; const ctx = canvas.getContext('2d'); if (!ctx) { return URL.createObjectURL(file); } ctx.drawImage(bitmap, 0, 0, width, height); bitmap.close(); return canvas.toDataURL('image/jpeg', quality); } catch { return URL.createObjectURL(file); } }