Galerie-Vorschaubilder schärfer erzeugen
This commit is contained in:
parent
a6735d7d4a
commit
53ab930ddf
4 changed files with 104 additions and 14 deletions
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -9,7 +9,8 @@ namespace App\Core;
|
|||
*
|
||||
* Ein hochgeladenes Bild wird in zwei Größen gespeichert:
|
||||
* <name>_gross.jpg – große Ansicht (max. 1200 px breit bzw. 900 px hoch)
|
||||
* <name>.jpg – Vorschaubild (max. 256 px lange Kante)
|
||||
* <name>.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/<category>/.
|
||||
*
|
||||
* @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
|
||||
{
|
||||
|
|
|
|||
|
|
@ -75,7 +75,7 @@
|
|||
data-title="<?= e($caption) ?>">
|
||||
<img src="/img_storage/tour-<?= e($tour_id) ?>/<?= e($img[0]) ?>.jpg"
|
||||
alt="<?= e($caption) ?>"
|
||||
loading="lazy" decoding="async" width="256" height="192">
|
||||
loading="lazy" decoding="async" width="600" height="450">
|
||||
<span class="gallery-overlay" aria-hidden="true">
|
||||
<span class="gallery-zoom">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24"
|
||||
|
|
|
|||
|
|
@ -51,7 +51,7 @@
|
|||
data-title="<?= e($caption) ?> (gelöscht)">
|
||||
<img src="/img_storage/tour-<?= e($tour_id) ?>-DELETED/<?= e($img[0]) ?>.jpg"
|
||||
alt="<?= e($caption) ?>, gelöscht"
|
||||
loading="lazy" decoding="async" width="256" height="192">
|
||||
loading="lazy" decoding="async" width="600" height="450">
|
||||
<span class="gallery-badge">Gelöscht</span>
|
||||
<span class="gallery-overlay" aria-hidden="true">
|
||||
<span class="gallery-zoom">
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue