Loheide-eu-mitarbeiterplatt.../app/Support/Covers.php
Friederich Loheide 05b4b79cb3
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 3m47s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 3m41s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 4m8s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 4m6s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 5m25s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 5m23s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 4m39s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 4m40s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m8s
Titelbild im Profil, durchsuchbare Baum-Auswahl im IT-Ticket (0.20.1)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 05:20:19 +00:00

57 lines
1.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Support;
use App\Models\User;
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 RuntimeException;
/**
* Titelbilder im Profil (der farbige Streifen oben). Gespeichert als ein WebP im
* Querformat im öffentlichen Upload-Verzeichnis – wie die Profilbilder (siehe Avatars).
*/
class Covers
{
public const WIDTH = 1600;
public const HEIGHT = 500;
public function store(User $user, UploadedFile $file): void
{
Images::withLocalCopy($file, function (string $path) use ($user) {
Images::raiseMemoryLimitFor($path);
$image = ImageManager::usingDriver(Driver::class)->decodePath($path);
$file = 'covers/'.$user->getKey().'/'.Str::random(12).'.webp';
// Zuschnitt 16:5 – breit genug für das Profil, die Begrüßung zeigt die Mitte
$image->cover(self::WIDTH, self::HEIGHT);
if (! Storage::disk('uploads')->put($file, $image->encode(new WebpEncoder(quality: 82))->toString())) {
throw new RuntimeException('Titelbild konnte nicht gespeichert werden – ist public/uploads beschreibbar?');
}
$this->delete($user);
$user->forceFill(['cover_path' => $file])->save();
});
}
public function delete(User $user): void
{
if ($user->cover_path === null) {
return;
}
Storage::disk('uploads')->delete($user->cover_path);
$user->forceFill(['cover_path' => null])->save();
}
public function url(User $user): ?string
{
return $user->cover_path ? Storage::disk('uploads')->url($user->cover_path) : null;
}
}