export async function prepareChatImage(file) { if (!['image/jpeg', 'image/png', 'image/webp'].includes(file.type)) throw new Error('Bitte ein JPEG-, PNG- oder WebP-Bild auswählen.') if (file.size > 10 * 1024 * 1024) throw new Error('Das Originalbild darf höchstens 10 MB groß sein.') const bitmap = await createImageBitmap(file) try { const scale = Math.min(1, 1600 / Math.max(bitmap.width, bitmap.height)) const canvas = document.createElement('canvas') canvas.width = Math.max(1, Math.round(bitmap.width * scale)); canvas.height = Math.max(1, Math.round(bitmap.height * scale)) canvas.getContext('2d').drawImage(bitmap, 0, 0, canvas.width, canvas.height) for (const quality of [0.85, 0.7, 0.55, 0.4]) { const dataUrl = canvas.toDataURL('image/webp', quality) if (dataUrl.length <= 480_000) return { dataUrl, name: (file.name || 'Bild').slice(0, 200) } } throw new Error('Dieses Bild ist zu groß. Bitte einen kleineren Ausschnitt verwenden.') } finally { bitmap.close() } }