From 53ab930ddf421d910268a9aab2eed870c7053996 Mon Sep 17 00:00:00 2001 From: torsten Date: Sun, 8 Nov 2026 20:05:12 +0100 Subject: [PATCH] =?UTF-8?q?Galerie-Vorschaubilder=20sch=C3=A4rfer=20erzeug?= =?UTF-8?q?en?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/Controllers/GalleryController.php | 2 +- motorradig/src/Core/Image.php | 112 ++++++++++++++++-- motorradig/views/tour/gallery/show.php | 2 +- .../views/tour/gallery/show_deleted.php | 2 +- 4 files changed, 104 insertions(+), 14 deletions(-) diff --git a/motorradig/src/Controllers/GalleryController.php b/motorradig/src/Controllers/GalleryController.php index b26808c..31df501 100644 --- a/motorradig/src/Controllers/GalleryController.php +++ b/motorradig/src/Controllers/GalleryController.php @@ -94,7 +94,7 @@ class GalleryController } try { - Image::store($upload, 'tour_' . $tour_id, $upload['name']); + Image::store($upload, 'tour_' . $tour_id, $upload['name'], true); $stored++; } catch (\Throwable $e) { if (!$e instanceof \RuntimeException) { diff --git a/motorradig/src/Core/Image.php b/motorradig/src/Core/Image.php index 568803f..0f649f5 100644 --- a/motorradig/src/Core/Image.php +++ b/motorradig/src/Core/Image.php @@ -9,7 +9,8 @@ namespace App\Core; * * Ein hochgeladenes Bild wird in zwei Größen gespeichert: * _gross.jpg – große Ansicht (max. 1200 px breit bzw. 900 px hoch) - * .jpg – Vorschaubild (max. 256 px lange Kante) + * .jpg – Vorschaubild (max. 600 px lange Kante; Galeriebilder + * als 4:3-Ausschnitt mit 600×450 px) * * Hinweis: kein imagedestroy() aufrufen. Seit PHP 8.0 werden GdImage-Objekte * automatisch freigegeben; imagedestroy() ist seit PHP 8.5 deprecated und @@ -23,14 +24,17 @@ class Image /** * Speichert ein hochgeladenes Bild unter storage/img_storage//. * + * @param bool $cropThumbnail Vorschaubild als 4:3-Ausschnitt erzeugen + * (für die Kacheln der Bildergalerie) + * * @throws \RuntimeException wenn der Upload unvollständig, das Format nicht * lesbar oder das Zielverzeichnis nicht beschreibbar ist */ - public static function store(array $upload, string $category, string $name): bool + public static function store(array $upload, string $category, string $name, bool $cropThumbnail = false): bool { $dir = config('paths.img_storage') . '/' . self::slug($category); - return self::storeTo($upload, $dir, self::slug(pathinfo($name, PATHINFO_FILENAME))); + return self::storeTo($upload, $dir, self::slug(pathinfo($name, PATHINFO_FILENAME)), $cropThumbnail); } /** @@ -41,7 +45,35 @@ class Image { $dir = public_path(trim($directory, '/')); - return self::storeTo($upload, $dir, self::slug(pathinfo($name, PATHINFO_FILENAME))); + return self::storeTo($upload, $dir, self::slug(pathinfo($name, PATHINFO_FILENAME)), false); + } + + /** + * Erzeugt ein Vorschaubild aus einer bereits gespeicherten Bilddatei neu. + * Nützlich, um bestehende Vorschaubilder nach einer Größenänderung zu + * aktualisieren (Quelle ist üblicherweise die _gross.jpg-Datei). + */ + public static function regenerateThumbnail(string $source, string $target, bool $cropThumbnail = false): bool + { + $data = @file_get_contents($source); + $src = $data === false ? false : @imagecreatefromstring($data); + + if (!$src instanceof \GdImage) { + return false; + } + + $w = imagesx($src); + $h = imagesy($src); + + if ($cropThumbnail) { + self::resizeCropped($src, $target, 600, 450); + } elseif ($w > $h) { + self::resize($src, $target, 600, (int) round($h * 600 / $w), 88); + } else { + self::resize($src, $target, (int) round($w * 600 / $h), 600, 88); + } + + return true; } /** @@ -49,7 +81,7 @@ class Image * * @throws \RuntimeException */ - private static function storeTo(array $upload, string $dir, string $baseName): bool + private static function storeTo(array $upload, string $dir, string $baseName, bool $cropThumbnail): bool { // Ohne JPEG-Unterstützung in GD schlägt imagejpeg() sonst mit einem // Fatal Error fehl, der (ohne display_errors) unsichtbar bleibt. @@ -118,17 +150,21 @@ class Image self::resize($src, $base . '_gross.jpg', (int) round($w * 900 / $h), 900); } - // Vorschaubild - if ($w > $h) { - self::resize($src, $base . '.jpg', 256, (int) round($h * 256 / $w)); + // Vorschaubild: 600 px, damit die Kacheln auch auf hochauflösenden + // Displays (Retina) scharf bleiben. Galeriebilder bekommen einen + // 4:3-Ausschnitt, weil die Kacheln im Raster genau dieses Format haben. + if ($cropThumbnail) { + self::resizeCropped($src, $base . '.jpg', 600, 450); + } elseif ($w > $h) { + self::resize($src, $base . '.jpg', 600, (int) round($h * 600 / $w), 88); } else { - self::resize($src, $base . '.jpg', (int) round($w * 256 / $h), 256); + self::resize($src, $base . '.jpg', (int) round($w * 600 / $h), 600, 88); } return true; } - private static function resize(\GdImage $src, string $target, int $newW, int $newH): void + private static function resize(\GdImage $src, string $target, int $newW, int $newH, int $quality = 85): void { $newW = max(1, $newW); $newH = max(1, $newH); @@ -151,13 +187,67 @@ class Image imagecopyresampled($dst, $src, 0, 0, 0, 0, $newW, $newH, imagesx($src), imagesy($src)); - if (!@imagejpeg($dst, $target, 85)) { + if (!@imagejpeg($dst, $target, $quality)) { error_log('Image::resize: imagejpeg() fehlgeschlagen für ' . $target); throw new \RuntimeException('Das Bild konnte nicht gespeichert werden (Schreibrechte prüfen).'); } } + /** + * Erzeugt einen mittig zugeschnittenen Ausschnitt im Zielformat. + * Kleine Bilder werden nicht hochskaliert, damit sie nicht unscharf werden. + */ + private static function resizeCropped(\GdImage $src, string $target, int $newW, int $newH): void + { + $srcW = imagesx($src); + $srcH = imagesy($src); + $targetRatio = $newW / $newH; + $srcRatio = $srcW / $srcH; + + if ($srcRatio > $targetRatio) { + $cropH = $srcH; + $cropW = (int) round($srcH * $targetRatio); + } else { + $cropW = $srcW; + $cropH = (int) round($srcW / $targetRatio); + } + + $cropW = max(1, $cropW); + $cropH = max(1, $cropH); + $srcX = (int) floor(($srcW - $cropW) / 2); + $srcY = (int) floor(($srcH - $cropH) / 2); + + // Kein Hochskalieren: bei sehr kleinen Quellbildern bleibt der + // Ausschnitt in Originalgröße, das Seitenverhältnis bleibt erhalten. + if ($cropW < $newW) { + $newW = $cropW; + $newH = max(1, (int) round($newW * $cropH / $cropW)); + } + + $dst = imagecreatetruecolor($newW, $newH); + + if ($dst === false) { + error_log('Image::resizeCropped: imagecreatetruecolor() fehlgeschlagen für ' . $target); + + throw new \RuntimeException('Das Bild konnte nicht verarbeitet werden.'); + } + + $white = imagecolorallocate($dst, 255, 255, 255); + + if ($white !== false) { + imagefilledrectangle($dst, 0, 0, $newW, $newH, $white); + } + + imagecopyresampled($dst, $src, 0, 0, $srcX, $srcY, $newW, $newH, $cropW, $cropH); + + if (!@imagejpeg($dst, $target, 88)) { + error_log('Image::resizeCropped: imagejpeg() fehlgeschlagen für ' . $target); + + throw new \RuntimeException('Das Bild konnte nicht gespeichert werden (Schreibrechte prüfen).'); + } + } + /** Erzeugt einen sicheren, URL-tauglichen Slug. */ public static function slug(string $value): string { diff --git a/motorradig/views/tour/gallery/show.php b/motorradig/views/tour/gallery/show.php index ff664c6..fa40bd5 100644 --- a/motorradig/views/tour/gallery/show.php +++ b/motorradig/views/tour/gallery/show.php @@ -75,7 +75,7 @@ data-title=""> <?= e($caption) ?> + loading="lazy" decoding="async" width="600" height="450">