Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 7m25s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 6m46s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 7m52s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 8m5s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 10m33s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 10m53s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 9m1s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 8m49s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 54s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m4s
- /uploads nur über das Portal: UploadController prüft Sitzung bzw. App-Token; frei nur die Dateien der Anmeldeseite (Logos, Symbole, Anmeldehintergrund, Stylesheet, Schrift); public/uploads/.htaccess leitet alles an index.php (Migration schreibt sie, da der Ordner vor dem Updater geschützt ist); Systemzustand prüft die Regel; nginx-Hinweis in docs/SECURITY.md - Passwort-Anmeldung abschaltbar (PasswordLogin): Login-Formular, Passwort vergessen, App-Token und /api/v1/config; nur mit verknüpftem Konto abschaltbar, automatische Rückkehr ohne nutzbaren Dienst, Notausgang INHAUS_PASSWORD_LOGIN - Titelbilder: profile_covers (bis 12), zufällig je Seitenaufruf - ImageCompressor: Uploads im Backend (SafeUpload) und WordPress-Import auf ≤ 2400 px komprimiert, Format bleibt Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
87 lines
2.9 KiB
PHP
87 lines
2.9 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', 'profile_covers'];
|
||
|
||
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]);
|
||
} elseif (is_array($theme[$field] ?? null)) {
|
||
$theme[$field] = array_map(fn ($path) => is_string($path) ? $this->optimize($path) : $path, $theme[$field]);
|
||
}
|
||
}
|
||
|
||
// Das erste Titelbild ist zugleich das einzelne (ältere Stellen lesen nur dieses)
|
||
if (($theme['profile_covers'] ?? []) !== []) {
|
||
$theme['profile_cover'] = $theme['profile_covers'][0];
|
||
}
|
||
|
||
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;
|
||
}
|
||
}
|
||
}
|