Loheide-eu-mitarbeiterplatt.../app/Support/Avatars.php
Friederich Loheide 293d7f86a4
Some checks failed
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 3m4s
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 3m8s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 3m32s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 3m35s
CI / PHP 8.3 / mysql:8.0 (pull_request) Successful in 5m7s
CI / PHP 8.4 / mysql:8.0 (pull_request) Successful in 5m1s
CI / PHP 8.3 / mysql:8.4 (pull_request) Successful in 5m30s
CI / PHP 8.4 / mysql:8.4 (pull_request) Successful in 5m31s
CI / Build-Artefakte aktuell (pull_request) Failing after 1m26s
Plugins bei fehlender Klasse nicht dauerhaft deaktivieren, Speichern-Abstand im Admin, Profilbild-Fehlermeldung
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-26 18:28:03 +00:00

90 lines
2.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;
/**
* Speichert Profilbilder als quadratische WebP-Dateien in festen Größen.
*
* Die Bilder liegen im öffentlichen Upload-Verzeichnis und werden von Apache
* direkt ausgeliefert – ohne PHP und ohne Symlink (auf Shared Hosting oft
* nicht möglich). Der Zufallsanteil im Dateinamen macht die URL bei jedem
* neuen Bild eindeutig, sodass sie lange gecacht werden darf.
*/
class Avatars
{
/** Pixelgrößen, die erzeugt werden (Header, Listen, Profilseite). */
public const SIZES = [96, 256];
public function store(User $user, UploadedFile $file): void
{
$this->storeFromPath($user, $file->getRealPath());
}
/**
* Profilbild aus Binärdaten (z. B. Foto aus Microsoft Entra).
*/
public function storeFromBinary(User $user, string $binary): void
{
$temporary = tempnam(sys_get_temp_dir(), 'avatar');
file_put_contents($temporary, $binary);
try {
$this->storeFromPath($user, $temporary);
} finally {
@unlink($temporary);
}
}
private function storeFromPath(User $user, string $path): void
{
Images::raiseMemoryLimitFor($path);
$image = ImageManager::usingDriver(Driver::class)->decodePath($path);
$base = 'avatars/'.$user->getKey().'/'.Str::random(12);
// Größte Stufe zuerst und danach schrittweise verkleinern – so liegt nur eine
// Kopie in voller Auflösung im Speicher (wichtig bei 128 MB memory_limit).
foreach (array_reverse(self::SIZES) as $size) {
$image->cover($size, $size);
if (! Storage::disk('uploads')->put("{$base}-{$size}.webp", $image->encode(new WebpEncoder(quality: 85))->toString())) {
throw new RuntimeException('Profilbild konnte nicht gespeichert werden – ist public/uploads beschreibbar?');
}
}
$this->delete($user);
$user->forceFill(['avatar_path' => $base])->save();
}
public function delete(User $user): void
{
if ($user->avatar_path === null) {
return;
}
foreach (self::SIZES as $size) {
Storage::disk('uploads')->delete("{$user->avatar_path}-{$size}.webp");
}
$user->forceFill(['avatar_path' => null])->save();
}
public function url(User $user, int $size = 256): ?string
{
if ($user->avatar_path === null) {
return null;
}
$size = in_array($size, self::SIZES, true) ? $size : max(self::SIZES);
return Storage::disk('uploads')->url("{$user->avatar_path}-{$size}.webp");
}
}