rackbeat / laravel-ui-avatars
Official Laravel wrapper around ui-avatars.com and LasseRafn/php-initial-avatar-generator
Installs: 116 134
Dependents: 0
Suggesters: 0
Security: 0
Stars: 42
Watchers: 3
Forks: 12
Open Issues: 3
Requires
- php: >=7.1
- illuminate/support: ^7.0|^8.0|^9.0|^10.0
- lasserafn/php-initial-avatar-generator: ^4.0
Requires (Dev)
- phpunit/phpunit: ^7.0|^9.5.10
- satooshi/php-coveralls: ^1.0
README
Installation
You just require using composer and you're good to go!
composer require rackbeat/laravel-ui-avatars
The Service Provider is automatically registered.
Setup
To setup the config file, you publish it like so:
php artisan vendor:publish --provider="Rackbeat\UIAvatars\UIAvatarsServiceProvider"
You can edit the file in config/ui-avatars.php
.
Usage
1. Add the Rackbeat\UIAvatars\HasAvatar
trait to your Model (e.g. App\Users
)
// ... class User extends Authenticatable { use \Rackbeat\UIAvatars\HasAvatar; // ... }
2. Create a new method on your Model.
This method is practically a proxy to call the HasAvatar
methods. It will return a gravatar from the e-mail, with a fallback to the avatar using ui-avatars.com
API.
public function getAvatar( $size = 64 ) { return $this->getGravatar( $this->email, $size ); }
NOTICE: Gravatar is only available using the API, not locally generated avatars.
3. (Optional) Re-define the name field
Assuming you're not using the default User
Model in Laravel, you can override which field is being used for the name.
public function getAvatarNameKey( ) { return 'full_name'; }
Available methods
getInitials($length=null)
Returns the generated initials, from the name, used in the avatar.
Default $length
can be defined in config/ui-avatars.php
(length
key)
getUrlfriendlyAvatar($size=null)
Return a urlfriendly formatted avatar (URL or base64).
Example usage:
<img src="{{ $user->getUrlfriendlyAvatar() }}" />
Default $size
can be defined in config/ui-avatars.php
(image_size
key)
getAvatarBase64($size=null)
Return a base64 representation of the avatar.
Default $size
can be defined in config/ui-avatars.php
(image_size
key)
getAvatarImage($size=null)
Return a Image object of the avatar.
Default $size
can be defined in config/ui-avatars.php
(image_size
key)
getGravatar($email, $size=null)
Return a link to a Gravatar image using the specified email, and a fallback to using our own generator (assuming provider = api
)
Default $size
can be defined in config/ui-avatars.php
(image_size
key)
Available options
In the config file you can specify different options.
Requirements
- PHP >= 7.1