Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 7m38s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 8m0s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 11m51s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 12m15s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 13m27s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 13m42s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 16m0s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 15m20s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 56s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m14s
- Erscheinungsbild: große Fotos (Anmeldung, Titelbild) beim Speichern als WebP ≤ 2400 px; Migration verkleinert vorhandene einmal - Vite: Grundgerüst und UI-Bausteine als gemeinsame Dateien (Startseite 34 statt 58 Skripte) - Plugin-Oberflächen: Kopie nach public/ erneuert sich bei neuem Build (Fingerabdruck von dist/inhaus.json), auch ohne neue Version Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
80 lines
2.5 KiB
PHP
80 lines
2.5 KiB
PHP
<?php
|
||
|
||
namespace App\Support\Theme;
|
||
|
||
use App\Support\Images;
|
||
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;
|
||
|
||
/**
|
||
* Fotos im Erscheinungsbild (Hintergrund der Anmeldung, Standard-Titelbild) werden oft in voller
|
||
* Kameragröße hochgeladen – mehrere MB, die auf jeder Seite mitgeladen würden. Große Dateien
|
||
* werden deshalb als WebP mit höchstens 2400 px abgelegt. Logos bleiben unverändert.
|
||
*/
|
||
class ThemePhotos
|
||
{
|
||
/** Bildfelder, die Fotos sind (Logos, Symbole und Platzhalter bleiben, wie sie sind) */
|
||
public const FIELDS = ['login_image', 'profile_cover'];
|
||
|
||
public const MAX_EDGE = 2400;
|
||
|
||
/** Ab dieser Größe wird verkleinert (kleinere Dateien sind bereits sparsam) */
|
||
public const MAX_BYTES = 450 * 1024;
|
||
|
||
/**
|
||
* @param array<string, mixed> $theme
|
||
* @return array<string, mixed>
|
||
*/
|
||
public function optimizeAll(array $theme): array
|
||
{
|
||
foreach (self::FIELDS as $field) {
|
||
if (is_string($theme[$field] ?? null) && $theme[$field] !== '') {
|
||
$theme[$field] = $this->optimize($theme[$field]);
|
||
}
|
||
}
|
||
|
||
return $theme;
|
||
}
|
||
|
||
/** Liefert den (ggf. neuen) Pfad – bei Fehlern den alten */
|
||
public function optimize(string $path): string
|
||
{
|
||
$disk = Storage::disk('uploads');
|
||
$extension = strtolower(pathinfo($path, PATHINFO_EXTENSION));
|
||
|
||
if (! in_array($extension, ['jpg', 'jpeg', 'png', 'webp'], true) || ! $disk->exists($path)) {
|
||
return $path;
|
||
}
|
||
|
||
try {
|
||
$local = $disk->path($path);
|
||
$size = $disk->size($path);
|
||
[$width, $height] = @getimagesize($local) ?: [0, 0];
|
||
|
||
if ($size <= self::MAX_BYTES && max($width, $height) <= self::MAX_EDGE) {
|
||
return $path;
|
||
}
|
||
|
||
Images::raiseMemoryLimitFor($local);
|
||
$image = ImageManager::usingDriver(Driver::class)->decodePath($local)->scaleDown(self::MAX_EDGE, self::MAX_EDGE);
|
||
$encoded = $image->encode(new WebpEncoder(quality: 80))->toString();
|
||
|
||
if (strlen($encoded) >= $size) {
|
||
return $path; // nichts gewonnen
|
||
}
|
||
|
||
$target = 'theme/'.Str::ulid().'.webp';
|
||
$disk->put($target, $encoded);
|
||
|
||
return $target;
|
||
} catch (Throwable $e) {
|
||
report($e);
|
||
|
||
return $path;
|
||
}
|
||
}
|
||
}
|