creativeorange/gravatar
Laravel package for generating Gravatar URLs and image tags from email addresses. Supports size, default image, rating, secure URLs, and caching options. Simple helpers and configuration make it easy to drop Gravatar avatars into your app.
Installation:
composer require creativeorange/gravatar
Publish the config (optional but recommended for customization):
php artisan vendor:publish --provider="Creativeorange\Gravatar\GravatarServiceProvider" --tag="config"
First Use Case: Generate a Gravatar URL for an email in a controller:
use Creativeorange\Gravatar\Facades\Gravatar;
$url = Gravatar::get('user@example.com');
Or directly in Blade:
{{ Gravatar::get('user@example.com') }}
Quick Configuration:
Override defaults in config/gravatar.php:
'size' => 200,
'default' => 'identicon',
'rating' => 'pg',
'secure' => true,
public function showProfile(User $user) {
$avatarUrl = Gravatar::get($user->email, [
'size' => 300,
'default' => 'mp',
]);
return view('profile', compact('avatarUrl'));
}
<img src="{{ Gravatar::get($user->email, ['size' => 300]) }}" alt="Profile">
public function showComments() {
$comments = Comment::with('user')->get();
foreach ($comments as $comment) {
$comment->avatar = Gravatar::get($comment->user->email, [
'size' => 80,
'default' => 'wavatar',
]);
}
return view('comments', compact('comments'));
}
Gravatar::setConfig([
'default' => Tenant::current()->fallback ?? 'retro',
]);
if (Gravatar::exists('user@example.com')) {
// Use Gravatar
} else {
// Fallback logic
}
{{ Gravatar::image('user@example.com', ['size' => 150]) }}
Renders:
<img src="..." alt="Gravatar" width="150" height="150">
$this->app->bind('gravatar.http.client', function () {
return new MockHttpClient();
});
use Creativeorange\Gravatar\Events\GravatarCached;
GravatarCached::dispatch($user->email, $url);
Gravatar::queueGet($email, $options);
Gravatar::setConfig(['size' => request()->wantsJson() ? 50 : 200]);
Gravatar::clearCache($oldEmail);
Gravatar::clearCache($newEmail);
Gravatar::queueGet($email); // Process asynchronously
$url = Gravatar::get($email); // Instead of {{ Gravatar::get($email) }}
setConfig() sparingly or merge configs:
Gravatar::setConfig(array_merge(config('gravatar'), ['size' => 200]));
'debug' => env('GRAVATAR_DEBUG', false),
$this->app->bind('gravatar.http.client', function () {
return new MockHttpClient();
});
$hash = Gravatar::hash('user@example.com');
// Should match Gravatar's output
$response = Gravatar::getHttpClient()->get($url);
$response->getHeaders();
Gravatar::setHashAlgorithm(function ($email) {
return hash('sha1', strtolower(trim($email)));
});
$this->app->bind('gravatar.http.client', function () {
return new SymfonyHttpClient();
});
GravatarCached::listen(function ($email, $url) {
// Log or cache the URL
});
Blade::directive('gravatar', function ($email) {
return "<?php echo Creativeorange\Gravatar\Facades\Gravatar::get($email); ?>";
});
Gravatar::setFallback(function ($email) {
return "https://example.com/fallback/{$email}.png";
});
How can I help you explore Laravel packages today?