Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/voltra/filament-svg-avatar

Change the default avatar url provider with one for inline SVGs
https://github.com/voltra/filament-svg-avatar

filament filament-plugin filamentphp filamentphp-plugin hacktoberfest laravel laravel-package

Last synced: about 8 hours ago
JSON representation

Change the default avatar url provider with one for inline SVGs

Awesome Lists containing this project

README

        

![Filament Svg Avatar: Change the default avatar url provider with one for inline SVGs](https://raw.githubusercontent.com/Voltra/filament-svg-avatar/main/art/banner.jpeg)

# voltra/filament-svg-avatar

[![Latest Version on Packagist](https://img.shields.io/packagist/v/voltra/filament-svg-avatar.svg?style=flat-square)](https://packagist.org/packages/voltra/filament-svg-avatar)
[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/voltra/filament-svg-avatar/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/voltra/filament-svg-avatar/actions?query=workflow%3Arun-tests+branch%3Amain)
[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/voltra/filament-svg-avatar/fix-php-code-styling.yml?branch=main&label=code%20style&style=flat-square)](https://github.com/voltra/filament-svg-avatar/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain)
[![Total Downloads](https://img.shields.io/packagist/dt/voltra/filament-svg-avatar.svg?style=flat-square)](https://packagist.org/packages/voltra/filament-svg-avatar)

Change the default avatar url provider with one for inline SVGs.

No more external HTTP requests just for default avatars.

## Installation

You can install the package via composer:

```bash
composer require voltra/filament-svg-avatar
```

You can publish the config file with:

```bash
php artisan vendor:publish --tag="filament-svg-avatar-config"
```

Optionally, you can publish the views using

```bash
php artisan vendor:publish --tag="filament-svg-avatar-views"
```

This is the contents of the published config file:

```php
return [
/**
* Global config of the SVG size
*
* @type ?int
*
* @default 500
*/
'svgSize' => null,

/**
* Global config for the avatar's background color (as a hex code)
*
* @type ?string
*/
'backgroundColor' => null,

/**
* Global config for the avatar's text color (as a hex code)
*
* @type ?string
*/
'textColor' => null,

/**
* Global config for whether to disallow the plugin from overriding colors
*
* @type bool
*
* @default false
*/
'disallowPluginOverride' => false,

/**
* Global config for the avatar's font-family
*
* @type ?string
*
* @default \Filament\Facades\Filament::getFontFamily()
*/
'fontFamily' => null,

/**
* Global config of the SVG size
*
* @type ?string
*
* @default '.1em'
*/
'textDy' => null,
];
```

## Usage

### As the avatar provider

Note `SvgAvatarsProviders` only renders properly if the font is available on the user's machine when the browser would decode the SVG as a base64 image.

If you want something portable, you'll need to replace Filament's default avatar component (see sections below).

```php
class MyPanelProvider extends \Filament\PanelProvider {
public function panel(\Filament\Panel $panel) {
return $panel
// [...]
->defaultAvatarProvider(\Voltra\FilamentSvgAvatar\Filament\AvatarProviders\SvgAvatarsProviders::class)
// [...]
}
}
```

### As a plugin

It automatically registers `SvgAvatarsProviders` as the avatar provider.

```php
class MyPanelProvider extends \Filament\PanelProvider {
public function panel(\Filament\Panel $panel) {
return $panel
// [...]
->plugins([
// [...]
\Voltra\FilamentSvgAvatar\FilamentSvgAvatarPlugin::make()
->backgroundColor(\Spatie\Color\Hex::fromString('#3b5998'))
->textColor(\Spatie\Color\Hex::fromString('#e9ebee')),
// [...]
])
// [...]
}
}
```

NB: If you register the plugin and want to register a provider that isn't our `SvgAvatarsProviders` (e.g. to use `RawSvgAvatarProvider`), you'll have to register it manually AFTER the plugin.
Other providers from this package can be registered before (or after) the plugin, but external providers need to be registered after the plugin.

```php
class MyPanelProvider extends \Filament\PanelProvider {
public function panel(\Filament\Panel $panel) {
return $panel
// [...]
->plugins([
// [...]
\Voltra\FilamentSvgAvatar\FilamentSvgAvatarPlugin::make()
->backgroundColor(\Spatie\Color\Hex::fromString('#3b5998'))
->textColor(\Spatie\Color\Hex::fromString('#e9ebee')),
// [...]
])
->defaultAvatarProvider(\Voltra\FilamentSvgAvatar\Filament\AvatarProviders\RawSvgAvatarProvider::class)
// [...]
}
}
```

### Replace filament's default avatar component

First change the default avatar provider to use `\Voltra\FilamentSvgAvatar\Filament\AvatarProviders\RawSvgAvatarProvider::class` so that it can properly use the initials instead of a URL:

```php
class MyPanelProvider extends \Filament\PanelProvider {
public function panel(\Filament\Panel $panel) {
return $panel
// [...]
->defaultAvatarProvider(\Voltra\FilamentSvgAvatar\Filament\AvatarProviders\RawSvgAvatarProvider::class)
// [...]
}
}
```

Then you'll have to override filament's avatar blade component.

If you don't want to do it manually, just execute this command:
```bash
php artisan vendor:publish --tag=filament-svg-avatar-core-overrides
```

If you want to do it manually: either publish filament's support package's views, or just create the `resources/views/vendor/filament/components/avatar.blade.php` file with the following content.

```php
@props([
'circular' => true,
'size' => 'md',
])

```

This will use the `` component, configure it based on what `` expects, and output an `` instead of an `` (which means better custom font support!).

### Extend or override

NB: Config values take precedence over overrides

Create a class that implements the `\Voltra\FilamentSvgAvatar\Contracts\SvgAvatarServiceContract` interface.

You can even extend from `\Voltra\FilamentSvgAvatar\Services\FilamentSvgAvatarService`.

Then, in a service provider, bind your implementation to the interface:
```php
class MyServiceProvider extends \Illuminate\Support\ServiceProvider {
// [...]

public function register() {
// [...]
$this->app->scoped(\Voltra\FilamentSvgAvatar\Contracts\SvgAvatarServiceContract::class, MySvgAvatarServiceImpl::class);
// [...]
}

// [...]
}
```

## Testing

```bash
composer test
```

## Changelog

Please see [CHANGELOG](https://github.com/Voltra/filament-svg-avatar/blob/main/CHANGELOG.md) for more information on what has changed recently.

## Contributing

Please see [CONTRIBUTING](https://github.com/Voltra/filament-svg-avatar/blob/main/.github/CONTRIBUTING.md) for details.

## Security Vulnerabilities

Please review [our security policy](https://github.com/Voltra/filament-svg-avatar/security/policy) on how to report security vulnerabilities.

## Credits

- [Voltra](https://github.com/Voltra)
- [All Contributors](https://github.com/Voltra/filament-svg-avatar/contributors)

## License

The MIT License (MIT). Please see [License File](https://github.com/Voltra/filament-svg-avatar/blob/main/LICENSE.md) for more information.