Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 1m56s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 1m56s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 2m2s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 2m9s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 2m40s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 2m35s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 2m29s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 2m38s
CI / Build-Artefakte aktuell (pull_request) Successful in 55s
- Seiten-Plugin: Seitenbaum mit Adressen, Seitennavigation, Brotkrumen und optional Hauptnavigation; Sichtbarkeit vererbt sich auf Unterseiten - Pagebuilder (Block-Registry im Kern, Plugins tragen Blöcke bei): Abschnitt, Spalten, Tabs, Akkordeon, Box, Überschrift, Text (TipTap), Zitat, Schaltflächen, Symbolliste, Tabelle, Trennlinie, Abstand, Bild, Galerie, Video (erst nach Klick), Titelbild, Ansprechpartner, Downloads, Unterseiten, Neuigkeiten, Termine; Sichtbarkeit je Block; Bereinigung beim Speichern - Editor im Portal: Ziehen und Ablegen, Autospeichern als Entwurf, Rückgängig, Gerätevorschau, Versionen, Zeitsteuerung, Bearbeitungssperre, Freigabe anfordern - Rechte „inkl. Unterseiten“ werden im Rechte-Cache aufgelöst - Neuigkeiten: Schreiben im Portal, Freigabe-Workflow mit Benachrichtigungen - Bereich „Redaktion“, Benachrichtigungen im Portal - storage/.migrations-fingerprint nicht mehr ausliefern Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
146 lines
4.4 KiB
PHP
146 lines
4.4 KiB
PHP
<?php
|
||
|
||
namespace App\Support;
|
||
|
||
use Illuminate\Http\UploadedFile;
|
||
use Illuminate\Support\Facades\Storage;
|
||
use Illuminate\Support\Str;
|
||
use Intervention\Image\Drivers\Gd\Driver;
|
||
use Intervention\Image\Encoders\WebpEncoder;
|
||
use Intervention\Image\ImageManager;
|
||
use Throwable;
|
||
|
||
/**
|
||
* Erzeugt aus hochgeladenen Titelbildern feste Größen im 16:9-Format (WebP),
|
||
* damit Listen und Startseite keine Originalbilder laden müssen.
|
||
*/
|
||
class Images
|
||
{
|
||
public const VARIANTS = [
|
||
'thumb' => [480, 270],
|
||
'large' => [1600, 900],
|
||
];
|
||
|
||
private const VARIANTS_LARGEST_FIRST = ['large', 'thumb'];
|
||
|
||
public function createVariants(string $path): void
|
||
{
|
||
$disk = Storage::disk('uploads');
|
||
|
||
if (! $disk->exists($path)) {
|
||
return;
|
||
}
|
||
|
||
try {
|
||
// Nur eine Kopie in voller Auflösung im Speicher: erst auf die größte
|
||
// Variante verkleinern, daraus dann die kleineren ableiten.
|
||
// Wichtig bei Shared Hosting mit 128 MB memory_limit und Handyfotos.
|
||
self::raiseMemoryLimitFor($disk->path($path));
|
||
$image = ImageManager::usingDriver(Driver::class)->decodePath($disk->path($path));
|
||
|
||
foreach (self::VARIANTS_LARGEST_FIRST as $variant) {
|
||
[$width, $height] = self::VARIANTS[$variant];
|
||
$image->coverDown($width, $height);
|
||
$disk->put($this->variantPath($path, $variant), $image->encode(new WebpEncoder(quality: 82))->toString());
|
||
}
|
||
} catch (Throwable $e) {
|
||
report($e);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Bild für Inhalte (Pagebuilder, Beiträge): Seitenverhältnis bleibt, höchstens
|
||
* 2000 Pixel breit, als WebP. GIFs bleiben unverändert (Animationen).
|
||
*
|
||
* @return string Pfad im Speicher „uploads“
|
||
*/
|
||
public function storeContentImage(UploadedFile $file, string $directory): string
|
||
{
|
||
$disk = Storage::disk('uploads');
|
||
$name = $directory.'/'.now()->format('Y/m').'/'.Str::random(24);
|
||
|
||
if ($file->getMimeType() === 'image/gif') {
|
||
$disk->putFileAs(dirname($name), $file, basename($name).'.gif');
|
||
|
||
return $name.'.gif';
|
||
}
|
||
|
||
self::raiseMemoryLimitFor($file->getRealPath());
|
||
$image = ImageManager::usingDriver(Driver::class)->decodePath($file->getRealPath());
|
||
$image->scaleDown(width: 2000);
|
||
$disk->put($name.'.webp', $image->encode(new WebpEncoder(quality: 84))->toString());
|
||
|
||
return $name.'.webp';
|
||
}
|
||
|
||
public function deleteWithVariants(?string $path): void
|
||
{
|
||
if (! $path) {
|
||
return;
|
||
}
|
||
|
||
$disk = Storage::disk('uploads');
|
||
$disk->delete($path);
|
||
|
||
foreach (array_keys(self::VARIANTS) as $variant) {
|
||
$disk->delete($this->variantPath($path, $variant));
|
||
}
|
||
}
|
||
|
||
public function url(?string $path, string $variant = 'large'): ?string
|
||
{
|
||
if (! $path) {
|
||
return null;
|
||
}
|
||
|
||
$disk = Storage::disk('uploads');
|
||
$variantPath = $this->variantPath($path, $variant);
|
||
|
||
return $disk->url($disk->exists($variantPath) ? $variantPath : $path);
|
||
}
|
||
|
||
/**
|
||
* GD hält Bilder unkomprimiert im Speicher (4 Byte je Pixel) – ein Handyfoto
|
||
* mit 12 Megapixeln braucht so allein ~48 MB. Wie WordPress heben wir das
|
||
* Limit für die Bildverarbeitung bei Bedarf auf mindestens 256 MB an.
|
||
*/
|
||
public static function raiseMemoryLimitFor(string $file): void
|
||
{
|
||
$size = @getimagesize($file);
|
||
$needed = $size ? (int) ($size[0] * $size[1] * 4 * 1.8) : 0;
|
||
$current = self::bytes((string) ini_get('memory_limit'));
|
||
|
||
if ($current === -1) {
|
||
return;
|
||
}
|
||
|
||
$target = max(256 * 1024 * 1024, memory_get_usage() + $needed);
|
||
|
||
if ($current < $target) {
|
||
@ini_set('memory_limit', (string) $target);
|
||
}
|
||
}
|
||
|
||
private static function bytes(string $value): int
|
||
{
|
||
$value = trim($value);
|
||
|
||
if ($value === '' || $value === '-1') {
|
||
return -1;
|
||
}
|
||
|
||
$number = (int) $value;
|
||
|
||
return match (strtolower(substr($value, -1))) {
|
||
'g' => $number * 1024 ** 3,
|
||
'm' => $number * 1024 ** 2,
|
||
'k' => $number * 1024,
|
||
default => $number,
|
||
};
|
||
}
|
||
|
||
private function variantPath(string $path, string $variant): string
|
||
{
|
||
return preg_replace('/\.[^.\/]+$/', '', $path)."-{$variant}.webp";
|
||
}
|
||
}
|