laravolt/avatar
Generate unique placeholder avatars from names or emails using initials, with customizable colors/fonts/sizes. Works in Laravel/Lumen or any PHP app. Output as base64 data URI, save PNG/JPG files, or fall back to Gravatar for email-based avatars.
Installation:
composer require laravolt/avatar
(Laravel 5.5+ auto-discovers the package; for older versions, register Laravolt\Avatar\ServiceProvider in config/app.php and add the facade alias.)
First Usage: Generate a base64-encoded avatar from a name in a Blade view:
<img src="{{ Avatar::create('John Doe')->toBase64() }}" alt="Avatar" />
(Outputs a circular image with initials "JD" on a random background.)
Key Files:
config/laravolt/avatar.php (published via php artisan vendor:publish)app/Avatar.php (facade for fluent method chaining).Scenario: Display consistent avatars for users in a dashboard. Implementation:
// In a User model or controller:
$avatarUrl = Avatar::create($user->name)
->setDimension(80)
->setShape('square')
->toBase64();
// In Blade:
<img src="{{ $avatarUrl }}" class="user-avatar" />
Why?
Pattern: Chain methods for runtime customization.
// Dynamic sizing based on context
$avatar = Avatar::create($user->name)
->setDimension($user->isPremium() ? 120 : 80)
->setTheme($user->preferredTheme ?? 'colorful')
->toBase64();
Use Case: Adapt avatars to UI themes or user preferences.
Pattern: Hybrid approach for existing Gravatar users.
$email = $user->email;
$avatar = Avatar::create($email)
->toGravatar(['d' => Avatar::create($user->name)->toBase64()]); // Fallback to custom avatar
Use Case: Migrate users from Gravatar to self-hosted avatars gradually.
Pattern: Use SVG for resolution-independent avatars.
// In Blade:
<div class="avatar-container">
{!! Avatar::create($user->name)
->setResponsive()
->setFontFamily('Roboto')
->toSvg() !!}
</div>
CSS:
.avatar-container svg {
width: 100%;
height: auto;
}
Use Case: Retina displays or responsive designs.
Pattern: Cache generated avatars to reduce load.
// Cache for 1 hour
$cachedAvatar = Cache::remember("avatar_{$user->id}", now()->addHour(), function() use ($user) {
return Avatar::create($user->name)->toBase64();
});
Use Case: High-traffic applications (e.g., social networks).
Eloquent Models: Add an accessor to User model:
public function getAvatarAttribute()
{
return Avatar::create($this->name)->toBase64();
}
Usage: <img src="{{ $user->avatar }}" />.
API Responses: Return avatars in JSON:
return response()->json([
'user' => $user,
'avatar' => Avatar::create($user->name)->toBase64(),
]);
require 'vendor/autoload.php';
$avatar = new \Laravolt\Avatar\Avatar(['driver' => 'gd']);
echo $avatar->create('Jane Doe')->toSvg();
Avatar::create($user->name)
->setTheme([
'backgrounds' => [$user->favoriteColor],
'foregrounds' => ['#000000'],
])
->toBase64();
$path = Avatar::create($user->name)
->save(storage_path("app/avatars/{$user->id}.png"));
Font Handling:
José) may render incorrectly.'ascii' => true in config or use fonts supporting Unicode (e.g., Noto Sans).Image Driver Conflicts:
Imagick may not be available on shared hosting.gd driver in config:
'driver' => 'gd',
SVG Caching:
Cache::put() with a unique key (e.g., hashed name + config).Gravatar Hashing:
Performance:
Inspect Generated Images:
$image = Avatar::create('Test')->getImageObject();
$image->save(storage_path('debug-avatar.png')); // Save for inspection
Check Config Overrides:
setTheme()) take precedence over config.Avatar::create('Test')->getAttribute('theme');
Font Paths:
config/laravolt/avatar.php are absolute and accessible by the web server.Gravatar Parameters:
echo Avatar::create('test@example.com')->toGravatar();
Custom Generators:
\Laravolt\Avatar\Generator\DefaultGenerator to create unique styles (e.g., pixel art).'generator' => App\Generators\PixelAvatar::class,
Theme Extensions:
config/laravolt/avatar.php:
'themes' => [
'monochrome' => [
'backgrounds' => ['#000000'],
'foregrounds' => ['#FFFFFF'],
],
],
Intervention Image Hooks:
$image = Avatar::create('Test')->getImageObject();
$image->filters()->sepia(); // Apply filters
$image->toBase64();
Event Listeners:
Avatar::creating(function ($name) {
Log::debug("Generating avatar for: $name");
});
Caching Layer:
Avatar::create('Test')->setCacheDriver('redis')->toBase64();
Consistent Hashing:
$cacheKey = md5($user->name . $user->email);
Dark Mode Support:
dark theme:
'themes' => [
'dark' => [
'backgrounds' => ['#1a1a1a'],
'foregrounds' => ['#ffffff'],
],
],
document.body.classList.toggle('dark-mode');
Avatar.create('User').setTheme('dark').toBase64();
Accessibility:
<img
src="{{ Avatar::create($user->name)->toBase64() }}"
alt="{{ $user->name }} avatar"
loading="lazy"
width="80"
height="80"
/>
Batch Generation:
use Laravolt\
How can I help you explore Laravel packages today?