finity-labs/fin-avatar
Privacy-first avatar provider for Filament v4/v5. Generates initials-based SVG avatars locally via a dedicated route (no Gravatar/ui-avatars requests), auto-strips titles like Dr/Mr/PhD, uses panel primary color, and enables 1-year browser caching.
Installation
composer require finity-labs/fin-avatar
Publish the config (if needed):
php artisan vendor:publish --provider="FinityLabs\FinAvatar\FinAvatarServiceProvider"
Register the Avatar Provider
In your AppServiceProvider or Filament panel configuration:
use FinityLabs\FinAvatar\Facades\FinAvatar;
Filament::registerAvatarProvider(FinAvatar::class);
First Use Case
Replace default Filament avatars in a UserResource:
use FinityLabs\FinAvatar\Avatar;
public static function table(Table $table): Table
{
return $table
->columns([
Avatar::make('avatar')
->size(40)
->color('primary')
->fallback('👤'),
]);
}
config/fin-avatar.php (customization options)src/Avatar.php (core class for customization)routes/web.php (generated /fin-avatar endpoint)Generate Avatars Locally
Use the dedicated route (/fin-avatar) to generate SVGs on-demand:
// In a controller or service
$avatarUrl = FinAvatar::generate('john.doe@example.com', [
'size' => 64,
'color' => '#3b82f6',
'shape' => 'circle',
]);
Filament Integration
Avatar::make() with FinAvatar::make().StatsOverviewWidget or ChartWidget for user avatars.TextInput or Select fields for profile pictures.Caching Strategy
Leverage browser caching via Cache-Control headers (configured in config/fin-avatar.php):
'cache' => [
'enabled' => true,
'ttl' => 31536000, // 1 year
],
Dynamic Customization Pass options dynamically in templates:
<x-fin-avatar
:email="$user->email"
:size="32"
color="success"
shape="rounded"
/>
Filament\Forms\Components\FileUpload for hybrid avatars:
Avatar::make('avatar')
->fallback(fn ($record) => $record->profile_photo_url)
'colors' => [
'primary' => '#10b981',
'secondary' => '#3b82f6',
],
$users->each->generateAvatar(); // Custom method
Route Conflicts
/fin-avatar doesn’t clash with existing routes. Override the route in config/fin-avatar.php:
'route' => 'filament.fin-avatar',
routes/web.php after installation.Caching Headers
ttl in config.'cache' => ['enabled' => env('APP_ENV') !== 'local'],
Email Normalization
FinAvatar::generate(strtolower(trim($email)));
SVG Injection Risks
size or color) if accepting user-provided values:
$size = (int) min(256, abs((int) $request->size));
/fin-avatar route output for malformed SVGs.config/fin-avatar.php:
'debug' => [
'log_missing_fonts' => true,
],
->fallback().Custom Shapes
Extend the FinAvatar class to add shapes (e.g., polygons):
namespace App\Extensions;
use FinityLabs\FinAvatar\Avatar;
class PolygonAvatar extends Avatar
{
protected function getShapeSvg(): string
{
return '<polygon points="..." />';
}
}
Font Integration Override default fonts by publishing assets:
php artisan vendor:publish --tag=fin-avatar-assets
Then update resources/views/vendor/fin-avatar/fonts.blade.php.
Filament Policy Integration Restrict avatar generation to authenticated users:
FinAvatar::middleware(['auth:sanctum']);
Analytics Track avatar generation (e.g., for GDPR compliance logs):
event(new AvatarGenerated($email, $options));
Register in FinAvatarServiceProvider.
loading="lazy" in Blade for offscreen avatars./fin-avatar route behind a CDN for global caching.FinAvatar::render().
```markdown
### Laravel-Specific Quirks
1. **Queue Jobs for Batch Processing**
Dispatch avatar generation as a job to avoid timeouts:
```php
GenerateAvatarJob::dispatch($user->email, $options);
Artisan Commands Create a command to regenerate all avatars:
php artisan fin:avatar:regenerate --users="1,2,3"
Testing Mock the avatar service in tests:
$this->partialMock(FinAvatar::class, function ($mock) {
$mock->shouldReceive('generate')
->andReturn('/path/to/mock.svg');
});
Filament 5.x Migration
Update Avatar usage from:
Filament\Tables\Columns\AvatarColumn
to:
FinityLabs\FinAvatar\Columns\AvatarColumn
in Filament 5.x projects.
How can I help you explore Laravel packages today?