fixed photo upoload

This commit is contained in:
Claudio 2025-12-03 10:38:43 +01:00
parent 77e5820dcc
commit f9737fdf73

View File

@ -1152,6 +1152,50 @@ if (!empty($_GET['ajax'])) {
const beep = $('#beepSound')[0];
let timers = {};
// --- FUNZIONE PER RIDIMENSIONARE LE IMMAGINI PRIMA DELL'UPLOAD ---
async function resizeImage(file, maxSize, quality = 0.7) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = event => {
const img = new Image();
img.src = event.target.result;
img.onload = () => {
let width = img.width;
let height = img.height;
// scala mantenendo proporzioni
if (width > height && width > maxSize) {
height = height * (maxSize / width);
width = maxSize;
} else if (height > maxSize) {
width = width * (maxSize / height);
height = maxSize;
}
const canvas = document.createElement("canvas");
canvas.width = width;
canvas.height = height;
const ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0, width, height);
canvas.toBlob(
blob => resolve(blob),
"image/jpeg",
quality
);
};
};
reader.onerror = error => reject(error);
});
}
function playBeep() {
beep.currentTime = 0;
beep.play().catch(() => {});
@ -1275,17 +1319,33 @@ if (!empty($_GET['ajax'])) {
});
// --- SUBMIT FORM FOTO ---
$("#photoForm").off("submit").on("submit", function(e) {
// --- SUBMIT FORM FOTO (con resize + upload) ---
$("#photoForm").off("submit").on("submit", async function(e) {
e.preventDefault();
let formData = new FormData(this);
const fileInput = document.getElementById("photoInput");
const file = fileInput.files[0];
// 🔥 mostra loader, disabilita submit
if (!file) {
showPhotoError("Seleziona una foto prima di caricare.");
return;
}
// mostra loader, disabilita submit
$("#photoMessageSuccess, #photoMessageError").hide();
$("#photoLoading").show();
$("#photoForm .modal-confirm").prop("disabled", true);
try {
// ridimensiona lato lungo max 1600 px, qualità 0.7
const resizedBlob = await resizeImage(file, 1600, 0.7);
const formData = new FormData();
formData.append("production_id", $("#photoProductionId").val());
formData.append("photo_type", $("#photoType").val());
formData.append("param_position", $("#photoParamPosition").val());
formData.append("photo", resizedBlob, file.name);
$.ajax({
url: "upload_photo.php",
type: "POST",
@ -1314,11 +1374,15 @@ if (!empty($_GET['ajax'])) {
showPhotoError("Errore di comunicazione con il server.");
}
});
} catch (err) {
console.error(err);
$("#photoLoading").hide();
$("#photoForm .modal-confirm").prop("disabled", false);
showPhotoError("Errore durante l'elaborazione dell'immagine.");
}
});
$('.record-card.in-production').each(function() {
const $card = $(this);
const $expanded = $card.find('.record-expanded');
@ -1747,11 +1811,15 @@ if (!empty($_GET['ajax'])) {
$("#photoInput").click();
});
// mostra solo il nome file scelto (opzionale)
// mostra nome file scelto E fa partire subito l'upload
$(document).on("change", "#photoInput", function() {
const file = this.files[0];
if (!file) return;
$("#selectedPhotoName").text(file.name);
// 🔥 auto-submit → parte resize + upload
$("#photoForm").submit();
});
</script>