diff --git a/public/userarea/photoschool/1_1769673298_0887961537437_0_0_536_0_75.jpg b/public/userarea/photoschool/1_1769673298_0887961537437_0_0_536_0_75.jpg new file mode 100644 index 0000000..5cf062c Binary files /dev/null and b/public/userarea/photoschool/1_1769673298_0887961537437_0_0_536_0_75.jpg differ diff --git a/public/userarea/photoschool/1_1769673301_dsc_2857.jpg b/public/userarea/photoschool/1_1769673301_dsc_2857.jpg new file mode 100644 index 0000000..c823d26 Binary files /dev/null and b/public/userarea/photoschool/1_1769673301_dsc_2857.jpg differ diff --git a/public/userarea/photoschool/1_1769673304_giocattolo-975apd-1.jpg b/public/userarea/photoschool/1_1769673304_giocattolo-975apd-1.jpg new file mode 100644 index 0000000..b62d4dc Binary files /dev/null and b/public/userarea/photoschool/1_1769673304_giocattolo-975apd-1.jpg differ diff --git a/public/userarea/school_profile.php b/public/userarea/school_profile.php index 2ae99a0..a3d06f3 100644 --- a/public/userarea/school_profile.php +++ b/public/userarea/school_profile.php @@ -55,6 +55,11 @@ if ($is_new) { } else { // se esiste, sincronizza school_id in sessione $_SESSION['school_id'] = (int)$school['id']; + + // Carica foto esistenti + $stmtPhotos = $pdo->prepare("SELECT id, filename FROM school_photos WHERE school_id = ? ORDER BY sort_order ASC, id ASC"); + $stmtPhotos->execute([$school['id']]); + $existingPhotos = $stmtPhotos->fetchAll(PDO::FETCH_ASSOC); } @@ -69,10 +74,69 @@ function generateSlug($string) return $slug; } +// Ridimensiona immagine con GD (max 1920 px lato lungo, qualità 82%) +function resizeAndSaveImage($tmp_name, $target_path, $maxDimension = 1920, $quality = 82) +{ + list($width, $height, $type) = getimagesize($tmp_name); + + if ($width <= $maxDimension && $height <= $maxDimension) { + return move_uploaded_file($tmp_name, $target_path); + } + + $ratio = min($maxDimension / $width, $maxDimension / $height); + $newW = (int)($width * $ratio); + $newH = (int)($height * $ratio); + + $src = null; + switch ($type) { + case IMAGETYPE_JPEG: + $src = imagecreatefromjpeg($tmp_name); + break; + case IMAGETYPE_PNG: + $src = imagecreatefrompng($tmp_name); + break; + case IMAGETYPE_GIF: + $src = imagecreatefromgif($tmp_name); + break; + default: + return false; + } + if (!$src) return false; + + $dst = imagecreatetruecolor($newW, $newH); + + // Trasparenza per PNG + if ($type == IMAGETYPE_PNG) { + imagealphablending($dst, false); + imagesavealpha($dst, true); + $transparent = imagecolorallocatealpha($dst, 255, 255, 255, 127); + imagefilledrectangle($dst, 0, 0, $newW, $newH, $transparent); + } + + imagecopyresampled($dst, $src, 0, 0, 0, 0, $newW, $newH, $width, $height); + + $success = false; + switch ($type) { + case IMAGETYPE_JPEG: + $success = imagejpeg($dst, $target_path, $quality); + break; + case IMAGETYPE_PNG: + $success = imagepng($dst, $target_path, (int)(9 - ($quality / 10))); + break; + case IMAGETYPE_GIF: + $success = imagegif($dst, $target_path); + break; + } + + imagedestroy($src); + imagedestroy($dst); + return $success; +} + // POST - Salvataggio $success_message = $error = null; -if ($_SERVER['REQUEST_METHOD'] === 'POST') { +if ($_SERVER['REQUEST_METHOD'] === 'POST' && !isset($_POST['action'])) { $name = trim($_POST['name'] ?? ''); $slug = generateSlug(trim($_POST['slug'] ?? $name)); $website = trim($_POST['website'] ?? ''); @@ -88,7 +152,8 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { $longitude = !empty($_POST['longitude']) ? floatval($_POST['longitude']) : null; $owner_name = trim($_POST['owner_name'] ?? ''); $vat_number = trim($_POST['vat_number'] ?? ''); - $status = in_array($_POST['status'] ?? 'active', ['active', 'inactive', 'suspended']) ? $_POST['status'] : 'active'; + $rawStatus = $_POST['status'] ?? 'active'; + $status = in_array($rawStatus, ['active', 'inactive', 'suspended'], true) ? $rawStatus : 'active'; // Validazioni if (empty($name)) $error = "Il nome della scuola è obbligatorio."; @@ -199,6 +264,92 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { } } } + +// AJAX per gestione foto +if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) { + header('Content-Type: application/json; charset=utf-8'); + + if ($is_new || empty($school['id'])) { + echo json_encode(['success' => false, 'error' => 'Prima salva il profilo scuola']); + exit; + } + + $school_id = (int)$school['id']; + + if ($_POST['action'] === 'upload_photos') { + $currentCount = $pdo->query("SELECT COUNT(*) FROM school_photos WHERE school_id = $school_id")->fetchColumn(); + $canAdd = 5 - $currentCount; + + if ($canAdd <= 0) { + echo json_encode(['success' => false, 'error' => 'Limite di 5 foto raggiunto']); + exit; + } + + $uploaded = []; + $errors = []; + + foreach ($_FILES['photos']['tmp_name'] ?? [] as $i => $tmp) { + if ($canAdd <= count($uploaded)) break; + if (empty($tmp) || $_FILES['photos']['error'][$i] !== 0) continue; + + $origName = $_FILES['photos']['name'][$i]; + $ext = strtolower(pathinfo($origName, PATHINFO_EXTENSION)); + if (!in_array($ext, ['jpg', 'jpeg', 'png', 'gif'])) { + $errors[] = $origName . ' - formato non supportato'; + continue; + } + + $safeName = preg_replace('/[^a-z0-9._-]/i', '', pathinfo($origName, PATHINFO_FILENAME)); + $newFilename = "photoschool/{$school_id}_" . time() . "_{$safeName}.{$ext}"; + + if (resizeAndSaveImage($tmp, $newFilename)) { + $stmt = $pdo->prepare("INSERT INTO school_photos + (school_id, filename, original_name, mime_type, file_size, sort_order) + VALUES (?, ?, ?, ?, ?, ?)"); + $stmt->execute([ + $school_id, + $newFilename, + $origName, + $_FILES['photos']['type'][$i] ?: 'image/jpeg', + (int)$_FILES['photos']['size'][$i], + $currentCount + count($uploaded) + ]); + + $uploaded[] = [ + 'id' => $pdo->lastInsertId(), + 'filename' => $newFilename + ]; + } else { + $errors[] = $origName . ' - errore elaborazione'; + } + } + + echo json_encode([ + 'success' => !empty($uploaded), + 'uploaded' => $uploaded, + 'errors' => $errors, + 'remaining' => 5 - ($currentCount + count($uploaded)) + ]); + exit; + } + + if ($_POST['action'] === 'delete_photo' && !empty($_POST['photo_id'])) { + $photoId = (int)$_POST['photo_id']; + $stmt = $pdo->prepare("SELECT filename FROM school_photos WHERE id = ? AND school_id = ?"); + $stmt->execute([$photoId, $school_id]); + $row = $stmt->fetch(PDO::FETCH_ASSOC); + + if ($row) { + if (file_exists($row['filename'])) @unlink($row['filename']); + $pdo->prepare("DELETE FROM school_photos WHERE id = ?")->execute([$photoId]); + } + echo json_encode(['success' => true]); + exit; + } + + echo json_encode(['success' => false, 'error' => 'Azione non valida']); + exit; +} ?> @@ -277,6 +428,18 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { letter-spacing: 1px; margin: 0 auto 1rem; } + + /* Thumbnails foto scuola */ + .school-photo-thumb { + cursor: zoom-in; + transition: transform .15s ease, box-shadow .15s ease; + box-shadow: 0 2px 10px rgba(0, 0, 0, .08); + } + + .school-photo-thumb:hover { + transform: scale(1.03); + box-shadow: 0 10px 25px rgba(0, 0, 0, .16); + } @@ -463,6 +626,49 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { + + +
+
+
+
Foto della scuola (max 5)
+ +
+

Trascina le immagini qui oppure

+ + +
Foto rimanenti:
+
+ +
+ +
+ Foto scuola + + +
+ +
+ + + +
+
+ @@ -645,6 +851,167 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { reader.readAsDataURL(file); }); + \ No newline at end of file