Loheide-eu-mitarbeiterplatt.../app/Support/Avatars.php
Friederich Loheide 6bede3e2ee
All checks were successful
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 3m14s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 3m17s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 3m35s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 3m43s
CI / PHP 8.3 / mysql:8.0 (pull_request) Successful in 5m14s
CI / PHP 8.4 / mysql:8.0 (pull_request) Successful in 5m18s
CI / PHP 8.3 / mysql:8.4 (pull_request) Successful in 5m16s
CI / PHP 8.4 / mysql:8.4 (pull_request) Successful in 5m11s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m40s
Update: ersetzten Plugin-Ordner ignorieren; Uploads trotz open_basedir
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-26 18:45:09 +00:00

90 lines
2.9 KiB
PHP
Raw Permalink 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
{
Images::withLocalCopy($file, fn (string $path) => $this->storeFromPath($user, $path));
}
/**
* 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");
}
}