more fixes
This commit is contained in:
@@ -18,6 +18,18 @@ class AvatarService
|
||||
|
||||
private const ALLOWED_MIME_TYPES = ['image/jpeg', 'image/png', 'image/webp'];
|
||||
|
||||
private const ALLOWED_POSITIONS = [
|
||||
'top-left',
|
||||
'top',
|
||||
'top-right',
|
||||
'left',
|
||||
'center',
|
||||
'right',
|
||||
'bottom-left',
|
||||
'bottom',
|
||||
'bottom-right',
|
||||
];
|
||||
|
||||
protected $sizes = [
|
||||
'xs' => 32,
|
||||
'sm' => 64,
|
||||
@@ -50,13 +62,13 @@ class AvatarService
|
||||
}
|
||||
}
|
||||
|
||||
public function storeFromUploadedFile(int $userId, UploadedFile $file): string
|
||||
public function storeFromUploadedFile(int $userId, UploadedFile $file, string $position = 'center'): string
|
||||
{
|
||||
$this->assertImageManagerAvailable();
|
||||
$this->assertStorageIsAllowed();
|
||||
$binary = $this->assertSecureImageUpload($file);
|
||||
|
||||
return $this->storeFromBinary($userId, $binary);
|
||||
return $this->storeFromBinary($userId, $binary, $position);
|
||||
}
|
||||
|
||||
public function storeFromLegacyFile(int $userId, string $path): ?string
|
||||
@@ -76,10 +88,26 @@ class AvatarService
|
||||
return $this->storeFromBinary($userId, $binary);
|
||||
}
|
||||
|
||||
private function storeFromBinary(int $userId, string $binary): string
|
||||
public function removeAvatar(int $userId): void
|
||||
{
|
||||
$diskName = (string) config('avatars.disk', 's3');
|
||||
Storage::disk($diskName)->deleteDirectory("avatars/{$userId}");
|
||||
|
||||
UserProfile::query()->updateOrCreate(
|
||||
['user_id' => $userId],
|
||||
[
|
||||
'avatar_hash' => null,
|
||||
'avatar_mime' => null,
|
||||
'avatar_updated_at' => Carbon::now(),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
private function storeFromBinary(int $userId, string $binary, string $position = 'center'): string
|
||||
{
|
||||
$image = $this->readImageFromBinary($binary);
|
||||
$image = $this->normalizeImage($image);
|
||||
$cropPosition = $this->normalizePosition($position);
|
||||
|
||||
$diskName = (string) config('avatars.disk', 's3');
|
||||
$disk = Storage::disk($diskName);
|
||||
@@ -87,7 +115,7 @@ class AvatarService
|
||||
|
||||
$hashSeed = '';
|
||||
foreach ($this->sizes as $size) {
|
||||
$variant = $image->cover($size, $size);
|
||||
$variant = $image->cover($size, $size, $cropPosition);
|
||||
$encoded = (string) $variant->encode(new WebpEncoder($this->quality));
|
||||
$disk->put("{$basePath}/{$size}.webp", $encoded, [
|
||||
'visibility' => 'public',
|
||||
@@ -110,6 +138,17 @@ class AvatarService
|
||||
return $hash;
|
||||
}
|
||||
|
||||
private function normalizePosition(string $position): string
|
||||
{
|
||||
$normalized = strtolower(trim($position));
|
||||
|
||||
if (in_array($normalized, self::ALLOWED_POSITIONS, true)) {
|
||||
return $normalized;
|
||||
}
|
||||
|
||||
return 'center';
|
||||
}
|
||||
|
||||
private function normalizeImage($image)
|
||||
{
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user