fixed photo upoload
This commit is contained in:
parent
77e5820dcc
commit
f9737fdf73
@ -1152,6 +1152,50 @@ if (!empty($_GET['ajax'])) {
|
|||||||
const beep = $('#beepSound')[0];
|
const beep = $('#beepSound')[0];
|
||||||
let timers = {};
|
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() {
|
function playBeep() {
|
||||||
beep.currentTime = 0;
|
beep.currentTime = 0;
|
||||||
beep.play().catch(() => {});
|
beep.play().catch(() => {});
|
||||||
@ -1275,50 +1319,70 @@ if (!empty($_GET['ajax'])) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
// --- SUBMIT FORM FOTO ---
|
// --- SUBMIT FORM FOTO (con resize + upload) ---
|
||||||
$("#photoForm").off("submit").on("submit", function(e) {
|
$("#photoForm").off("submit").on("submit", async function(e) {
|
||||||
e.preventDefault();
|
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();
|
$("#photoMessageSuccess, #photoMessageError").hide();
|
||||||
$("#photoLoading").show();
|
$("#photoLoading").show();
|
||||||
$("#photoForm .modal-confirm").prop("disabled", true);
|
$("#photoForm .modal-confirm").prop("disabled", true);
|
||||||
|
|
||||||
$.ajax({
|
try {
|
||||||
url: "upload_photo.php",
|
// ridimensiona lato lungo max 1600 px, qualità 0.7
|
||||||
type: "POST",
|
const resizedBlob = await resizeImage(file, 1600, 0.7);
|
||||||
data: formData,
|
|
||||||
processData: false,
|
|
||||||
contentType: false,
|
|
||||||
dataType: "json",
|
|
||||||
success: function(r) {
|
|
||||||
$("#photoLoading").hide();
|
|
||||||
$("#photoForm .modal-confirm").prop("disabled", false);
|
|
||||||
|
|
||||||
if (r.success) {
|
const formData = new FormData();
|
||||||
showPhotoSuccess();
|
formData.append("production_id", $("#photoProductionId").val());
|
||||||
loadPhotoGallery(
|
formData.append("photo_type", $("#photoType").val());
|
||||||
$("#photoProductionId").val(),
|
formData.append("param_position", $("#photoParamPosition").val());
|
||||||
$("#photoType").val(),
|
formData.append("photo", resizedBlob, file.name);
|
||||||
$("#photoParamPosition").val() || null
|
|
||||||
);
|
$.ajax({
|
||||||
} else {
|
url: "upload_photo.php",
|
||||||
showPhotoError(r.message || "Errore durante il caricamento della foto.");
|
type: "POST",
|
||||||
|
data: formData,
|
||||||
|
processData: false,
|
||||||
|
contentType: false,
|
||||||
|
dataType: "json",
|
||||||
|
success: function(r) {
|
||||||
|
$("#photoLoading").hide();
|
||||||
|
$("#photoForm .modal-confirm").prop("disabled", false);
|
||||||
|
|
||||||
|
if (r.success) {
|
||||||
|
showPhotoSuccess();
|
||||||
|
loadPhotoGallery(
|
||||||
|
$("#photoProductionId").val(),
|
||||||
|
$("#photoType").val(),
|
||||||
|
$("#photoParamPosition").val() || null
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
showPhotoError(r.message || "Errore durante il caricamento della foto.");
|
||||||
|
}
|
||||||
|
},
|
||||||
|
error: function() {
|
||||||
|
$("#photoLoading").hide();
|
||||||
|
$("#photoForm .modal-confirm").prop("disabled", false);
|
||||||
|
showPhotoError("Errore di comunicazione con il server.");
|
||||||
}
|
}
|
||||||
},
|
});
|
||||||
error: function() {
|
} catch (err) {
|
||||||
$("#photoLoading").hide();
|
console.error(err);
|
||||||
$("#photoForm .modal-confirm").prop("disabled", false);
|
$("#photoLoading").hide();
|
||||||
showPhotoError("Errore di comunicazione con il server.");
|
$("#photoForm .modal-confirm").prop("disabled", false);
|
||||||
}
|
showPhotoError("Errore durante l'elaborazione dell'immagine.");
|
||||||
});
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
$('.record-card.in-production').each(function() {
|
$('.record-card.in-production').each(function() {
|
||||||
const $card = $(this);
|
const $card = $(this);
|
||||||
const $expanded = $card.find('.record-expanded');
|
const $expanded = $card.find('.record-expanded');
|
||||||
@ -1747,11 +1811,15 @@ if (!empty($_GET['ajax'])) {
|
|||||||
$("#photoInput").click();
|
$("#photoInput").click();
|
||||||
});
|
});
|
||||||
|
|
||||||
// mostra solo il nome file scelto (opzionale)
|
// mostra nome file scelto E fa partire subito l'upload
|
||||||
$(document).on("change", "#photoInput", function() {
|
$(document).on("change", "#photoInput", function() {
|
||||||
const file = this.files[0];
|
const file = this.files[0];
|
||||||
if (!file) return;
|
if (!file) return;
|
||||||
|
|
||||||
$("#selectedPhotoName").text(file.name);
|
$("#selectedPhotoName").text(file.name);
|
||||||
|
|
||||||
|
// 🔥 auto-submit → parte resize + upload
|
||||||
|
$("#photoForm").submit();
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user