From ae7bab1c957a1352b003e7d52d8ab9ba734a2783 Mon Sep 17 00:00:00 2001 From: torsten Date: Sun, 8 Nov 2026 16:51:23 +0100 Subject: [PATCH] Fix: Tour-Titelbild wird gespeichert und angezeigt --- motorradig/src/Controllers/TourController.php | 18 ++++++++++++- motorradig/src/Core/Image.php | 27 ++++++++++++++++--- motorradig/views/tour/edit.php | 10 ++++++- motorradig/views/tour/edit_old.php | 10 ++++++- motorradig/views/tour/show.php | 10 ++++++- motorradig/views/tour/show_old.php | 10 ++++++- 6 files changed, 77 insertions(+), 8 deletions(-) diff --git a/motorradig/src/Controllers/TourController.php b/motorradig/src/Controllers/TourController.php index a44653e..e10748a 100644 --- a/motorradig/src/Controllers/TourController.php +++ b/motorradig/src/Controllers/TourController.php @@ -83,6 +83,21 @@ class TourController 'updated_at' => now(), ]); + if (!empty($_FILES['bild']['name'])) { + try { + Image::storePublic($_FILES['bild'], 'img/tour', (string) $tour->id); + } catch (\Throwable $e) { + if (!$e instanceof \RuntimeException) { + error_log('Tourbild-Upload: ' . $e); + } + + flash_set('error', $e instanceof \RuntimeException + ? $e->getMessage() + : 'Das Bild konnte nicht verarbeitet werden.'); + redirect('/tour/' . $tour->id . '/edit'); + } + } + redirect('/tour'); } @@ -190,7 +205,8 @@ class TourController if (!empty($_FILES['bild']['name'])) { try { - Image::store($_FILES['bild'], 'tour_' . $tourId, (string) input('name')); + // Tour-Titelbilder liegen wie bisher unter public/img/tour/.jpg + Image::storePublic($_FILES['bild'], 'img/tour', (string) $tourId); } catch (\Throwable $e) { if (!$e instanceof \RuntimeException) { error_log('Tourbild-Upload: ' . $e); diff --git a/motorradig/src/Core/Image.php b/motorradig/src/Core/Image.php index b5a76a3..568803f 100644 --- a/motorradig/src/Core/Image.php +++ b/motorradig/src/Core/Image.php @@ -27,6 +27,29 @@ class Image * lesbar oder das Zielverzeichnis nicht beschreibbar ist */ public static function store(array $upload, string $category, string $name): bool + { + $dir = config('paths.img_storage') . '/' . self::slug($category); + + return self::storeTo($upload, $dir, self::slug(pathinfo($name, PATHINFO_FILENAME))); + } + + /** + * Speichert ein öffentlich ausgeliefertes Bild unter public//. + * Wird für die Tour-Titelbilder (public/img/tour/.jpg) verwendet. + */ + public static function storePublic(array $upload, string $directory, string $name): bool + { + $dir = public_path(trim($directory, '/')); + + return self::storeTo($upload, $dir, self::slug(pathinfo($name, PATHINFO_FILENAME))); + } + + /** + * Gemeinsame Prüf- und Speicherlogik für alle Zielverzeichnisse. + * + * @throws \RuntimeException + */ + private static function storeTo(array $upload, string $dir, string $baseName): bool { // Ohne JPEG-Unterstützung in GD schlägt imagejpeg() sonst mit einem // Fatal Error fehl, der (ohne display_errors) unsichtbar bleibt. @@ -67,8 +90,6 @@ class Image throw new \RuntimeException('Das Bild hat keine gültige Größe.'); } - $dir = config('paths.img_storage') . '/' . self::slug($category); - if (!is_dir($dir)) { // 0775 genügt, wenn PHP-FPM als Eigentümer/Gruppe läuft; 0777 hilft // bei Bind-Mounts mit abweichender UID. @@ -79,7 +100,7 @@ class Image throw new \RuntimeException('Der Bildspeicher auf dem Server ist nicht beschreibbar. Bitte den Administrator informieren.'); } - $base = $dir . '/' . self::slug(pathinfo($name, PATHINFO_FILENAME)); + $base = $dir . '/' . $baseName; $data = @file_get_contents($tmp); $src = $data === false ? false : @imagecreatefromstring($data); diff --git a/motorradig/views/tour/edit.php b/motorradig/views/tour/edit.php index 09aee5c..b006186 100644 --- a/motorradig/views/tour/edit.php +++ b/motorradig/views/tour/edit.php @@ -67,7 +67,15 @@
- + id . '.jpg'); ?> + + + + + + + diff --git a/motorradig/views/tour/edit_old.php b/motorradig/views/tour/edit_old.php index 191118d..93fdeb2 100644 --- a/motorradig/views/tour/edit_old.php +++ b/motorradig/views/tour/edit_old.php @@ -24,7 +24,15 @@
- + id . '.jpg'); ?> + + + + + + +
diff --git a/motorradig/views/tour/show.php b/motorradig/views/tour/show.php index 4d41750..d30b87d 100644 --- a/motorradig/views/tour/show.php +++ b/motorradig/views/tour/show.php @@ -65,7 +65,15 @@ - + id . '.jpg'); ?> + + + + + + +
diff --git a/motorradig/views/tour/show_old.php b/motorradig/views/tour/show_old.php index 012b850..c0e22df 100644 --- a/motorradig/views/tour/show_old.php +++ b/motorradig/views/tour/show_old.php @@ -40,7 +40,15 @@ - + id . '.jpg'); ?> + + + + + + +