tomshaw/laravel-gravatar
Zero-config Laravel package that adds a Blade @gravatar directive with named parameters. Generate Gravatar URLs from an email with options for size, default image style, and rating—perfect for quickly rendering user avatars in your views.
Installation:
composer require tomshaw/laravel-gravatar
No additional configuration is required—this package is zero-config.
First Use Case: Insert a Gravatar into a Blade template for a user profile:
<img src="@gravatar(email: $user->email, size: 80, default: 'identicon')" alt="User Avatar">
This renders an 80px Gravatar with an identicon fallback if no Gravatar exists.
Where to Look First:
@gravatar directive directly in views.rating, secure, forceDefault).GravatarDirective.php for advanced customization (if needed).Dynamic User Avatars: Loop through users and render avatars with consistent sizing:
@foreach($users as $user)
<div class="user-card">
<img src="@gravatar(email: $user->email, size: 50)" alt="{{ $user->name }}">
<p>{{ $user->name }}</p>
</div>
@endforeach
Conditional Fallbacks:
Use default and rating to enforce brand guidelines:
<img src="@gravatar(email: $user->email, default: 'retro', rating: 'pg')" alt="Profile">
rating: 'pg' ensures no explicit content.default: 'retro' aligns with your design system.Secure URLs:
Enable HTTPS for Gravatar URLs (default is secure: true):
<img src="@gravatar(email: $user->email, secure: true)" alt="Secure Avatar">
Component Integration: Create a reusable Blade component for avatars:
@component('components.avatar', ['email' => $user->email, 'size' => 60])
@slot('fallback')
{{ strtoupper(substr($user->name, 0, 1)) }}
@endslot
@endcomponent
Component File (resources/views/components/avatar.blade.php):
<img
src="@gravatar(email: $email, size: $size, default: 'identicon')"
alt="{{ $slot ?? 'User Avatar' }}"
class="avatar"
>
API Responses: Generate Gravatar URLs in JSON responses for frontend frameworks:
return response()->json([
'user' => [
'avatar' => route('gravatar', [
'email' => $user->email,
'size' => 100,
'default' => 'wavatar'
]),
],
]);
Route Definition (routes/web.php):
Route::get('/gravatar', function (Request $request) {
return Gravatar::getUrl($request->email, $request->size, $request->default);
})->name('gravatar');
Team Onboarding:
@gravatar directive in your team’s style guide.<!-- Default avatar for comments -->
<img src="@gravatar(email: $comment->user->email, size: 40)" alt="{{ $comment->user->name }}">
A/B Testing:
Test different default styles (e.g., retro vs. robohash) by toggling the default parameter in experiments:
@if (config('app.feature_flag.robohash_avatars'))
<img src="@gravatar(email: $user->email, default: 'robohash')">
@else
<img src="@gravatar(email: $user->email, default: 'retro')">
@endif
Caching: Cache Gravatar URLs in a service layer to reduce API calls (though Gravatar’s CDN is already optimized):
class AvatarService {
public function getCachedUrl(string $email, int $size = 60): string {
$cacheKey = "gravatar_{$email}_{$size}";
return cache()->remember($cacheKey, now()->addHours(1), function () use ($email, $size) {
return Gravatar::getUrl($email, $size);
});
}
}
Laravel Mix/Webpack: Use the directive in inline styles or JavaScript:
<style>
.user-avatar {
background-image: url("@gravatar(email: '{{ $user->email }}', size: 30)");
}
</style>
Livewire/Alpine.js: Dynamically update avatars when user data changes:
<div x-data="{ email: '{{ $user->email }}' }">
<img :src="'@gravatar(email: ' + email + ', size: 50)'" alt="Dynamic Avatar">
</div>
Testing: Mock Gravatar URLs in unit tests:
use Tomshaw\Gravatar\Facades\Gravatar;
public function test_gravatar_directive() {
Gravatar::shouldReceive('getUrl')
->once()
->with('[email protected]', 60, 'mp')
->andReturn('https://example.com/avatar.jpg');
$this->blade->render('@gravatar(email: "[email protected]", size: 60)')
->assertSee('https://example.com/avatar.jpg');
}
Email Validation:
user@) will return a 404.@gravatar:
$email = filter_var($user->email, FILTER_SANITIZE_EMAIL);
Size Limits:
$size. Passing 0 or 3000 will throw an exception.$size = min(max($request->size, 1), 2048);
Caching Headers:
default or rating parameters, users may see stale images until the cache expires.forceDefault: 'y' to bypass cache for testing:
<img src="@gravatar(email: $user->email, default: 'robohash', forceDefault: 'y')">
HTTPS Enforcement:
secure parameter defaults to true, but some environments (e.g., local development) may ignore it.secure: false for HTTP contexts:
<img src="@gravatar(email: $user->email, secure: false)">
Blade Compilation:
@gravatar directive is compiled into a URL string. If you dynamically generate the directive (e.g., via JavaScript), it won’t work.// Controller
$avatarUrl = Gravatar::getUrl($user->email, 60);
return view('profile', compact('avatarUrl'));
<!-- View -->
<img src="{{ $avatarUrl }}" alt="User Avatar">
Invalid URLs:
<img> tag’s src attribute.F12).<img> tag and copy the src URL.Parameter Overrides:
rating: 'x') may fail if Gravatar’s API rejects them.Package Not Found:
@gravatar is not recognized, ensure:
composer require tomshaw/laravel-gravatar).How can I help you explore Laravel packages today?