Fix image uploads and reduce flyer request sizes

This commit is contained in:
kai
2026-08-27 07:34:45 +02:00
parent 41b2c5cd61
commit fd8c216cc8
4 changed files with 96 additions and 5 deletions
+39
View File
@@ -133,6 +133,45 @@
</main>
{% if can_edit_details %}
<script>
const editConcertForm = document.querySelector('form[enctype="multipart/form-data"]');
const editFlyerInput = editConcertForm?.querySelector('input[name="flyer"]');
async function optimizeEditFlyer(file) {
const bitmap = await createImageBitmap(file);
const maxSide = 1800;
const scale = Math.min(1, maxSide / 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);
bitmap.close();
let quality = 0.86;
let blob;
do {
blob = await new Promise(resolve => canvas.toBlob(resolve, "image/jpeg", quality));
quality -= 0.08;
} while (blob && blob.size > 900 * 1024 && quality >= 0.5);
if (!blob) throw new Error("Bild konnte nicht verarbeitet werden");
return new File([blob], "flyer.jpg", {type: "image/jpeg"});
}
editConcertForm?.addEventListener("submit", async event => {
if (editConcertForm.dataset.flyerOptimized === "true") return;
const file = editFlyerInput?.files[0];
if (!file) return;
event.preventDefault();
try {
const transfer = new DataTransfer();
transfer.items.add(await optimizeEditFlyer(file));
editFlyerInput.files = transfer.files;
editConcertForm.dataset.flyerOptimized = "true";
editConcertForm.requestSubmit();
} catch (error) {
console.error("Flyer optimization failed:", error);
editConcertForm.dataset.flyerOptimized = "true";
editConcertForm.requestSubmit();
}
});
const eventType = document.getElementById("event-type");
const parentEventField = document.getElementById("parent-event-field");
const parentEventId = document.getElementById("parent-event-id");