Plug n play avatar, turn name, email, and any other string into beautiful avatar (or gravatar), effortless.
Plug n play avatar, turn name, email, and any other string into beautiful avatar (or gravatar), effortless.
Display unique avatar for any user based on their (initials) name.
| Version | PHP | Laravel | Intervention Image |
|---|---|---|---|
| 7.x | >= 8.3 | 10.x – 13.x | ^4.0 |
| 6.x | >= 8.1 | 10.x – 12.x | ^3.0 |
| 5.x | >= 8.0 | 9.x – 11.x | ^2.0 |
This package was originally built for Laravel, but can also be used in any PHP project.
Read more about integration with PHP project here.
composer require laravolt/avatarThe service provider and Avatar facade are registered automatically via package auto-discovery.
php artisan vendor:publish --provider="Laravolt\Avatar\ServiceProvider"This will create config file located in config/laravolt/avatar.php.
$app->register(Laravolt\Avatar\LumenServiceProvider);//this will output data-uri (base64 image data)
//something like data:image/png;base64,iVBORw0KGg....
Avatar::create('Joko Widodo')->toBase64();
//use in view
//this will display initials JW as an image
toBase64() }}" />Avatar::create('Susilo Bambang Yudhoyono')->save('sample.png');
Avatar::create('Susilo Bambang Yudhoyono')->save('sample.jpg', 100); // quality = 100Avatar::create('[email protected]')->toGravatar();
// Output: http://gravatar.com/avatar/0c5cbf5a8762d91d930795a6107b2ce5814a6ab26e60c7ec6b75bc81c7dfe3ee
Avatar::create('[email protected]')->toGravatar(['d' => 'identicon', 'r' => 'pg', 's' => 100]);
// Output: http://gravatar.com/avatar/0c5cbf5a8762d91d930795a6107b2ce5814a6ab26e60c7ec6b75bc81c7dfe3ee?d=identicon&r=pg&s=100Gravatar parameter reference: https://docs.gravatar.com/api/avatars/images/
Avatar::create('Susilo Bambang Yudhoyono')->toSvg();You may specify custom font-family for your SVG text.
<head>
<link href="https://fonts.googleapis.com/css?family=Laravolt" rel="stylesheet">
<style>
@font-face {
font-family: Laravolt;
src: url({{ asset('fonts/laravolt.woff')) }});
}
</style>
</head>Avatar::create('Susilo Bambang Yudhoyono')->setFontFamily('Laravolt')->toSvg();You may make the SVG responsive. This excludes the height and width attributes.
Avatar::create('Susilo Bambang Yudhoyono')->setResponsive()->toSvg();Avatar::create('Abdul Somad')->getImageObject();The method will return an instance of Intervention image object, so you can use it for further purposes.
By default, this package will try to output any initials letter as it is. If the name supplied contains any non-ASCII character (e.g. ā, Ě, ǽ) then the result will depend on which font used (see config). If the font supports the characters supplied, they will be displayed successfully, otherwise they will not.
Alternatively, we can convert all non-ascii to their closest ASCII counterparts. If no closest counterparts are found, those characters are removed. Thanks to Stringy for providing such useful functions. What we need is just change one line in config/avatar.php:
'ascii' => true,…We can override configuration at runtime by using the following functions:
…// include composer autoload
require 'vendor/autoload.php';
// import the Avatar class
use Laravolt\Avatar\Avatar;
// create your first avatar
$avatar = new Avatar($config);
$avatar->create('John Doe')->toBase64();
$avatar->create('John Doe')->save('path/to/file.png', $quality = 90);$config is just an ordinary array with same format as explained above (See Configuration).
No open issues yet, or sync has not completed.