yzalis/identicon
Generate GitHub-style identicon avatars from a string or hash in PHP/Laravel. Simple API to create deterministic, unique icons on the fly, render as PNG/SVG, and integrate into apps for user placeholders without storing image files.
Installation
composer require yzalis/identicon
No additional configuration is required—just autoload the package.
First Use Case: Basic Identicon Generation
use Yzalis\Identicon\Facades\Identicon;
// Generate a simple identicon from a string (e.g., email or username)
$identicon = Identicon::generate('user@example.com');
// Output to a file (e.g., for avatar storage)
$identicon->save(storage_path('app/public/avatars/user.png'));
Where to Look First
Yzalis\Identicon\Facades\Identicon (simplifies usage).Yzalis\Identicon\IdenticonBuilder (for advanced customization).Dynamic Avatar Generation
// Generate and store avatars for users on registration/login
$user->avatar_path = storage_path('app/public/avatars/' . $user->email_hash . '.png');
Identicon::generate($user->email)->save($user->avatar_path);
Customizing Appearance
// Use the builder for fine-grained control
$identicon = Identicon::builder()
->size(200) // Width/height in pixels
->margin(10) // Padding around the identicon
->colors(['#FF5733', '#33FF57', '#3357FF']) // Custom palette
->generate('custom@user.com');
Integration with Laravel Views
// Blade directive for easy templating
@identicon('user@example.com', ['width' => 50, 'height' => 50])
Register the directive in a service provider:
Blade::directive('identicon', function ($expression) {
return "<?php echo Yzalis\Identicon\Facades\Identicon::generate(" . $expression[0] . ")->toBase64(); ?>";
});
Caching for Performance
// Cache generated identicons to avoid reprocessing
$cacheKey = 'identicon_' . md5($user->email);
$path = cache()->remember($cacheKey, now()->addYears(1), function () use ($user) {
$identicon = Identicon::generate($user->email);
return $identicon->save(storage_path('app/public/avatars/' . $user->email_hash . '.png'));
});
API Responses
// Return identicons as base64 in JSON APIs
return response()->json([
'avatar' => Identicon::generate($user->email)->toBase64()
]);
Input Hashing for Consistency
md5($user->email)) to ensure the same user gets the same identicon.$hash = md5(strtolower(trim($user->email)));
Identicon::generate($hash)->save(...);
File Permissions
storage/app/public directory is writable:
chmod -R 755 storage/app/public
php artisan storage:link
Color Clashes
Identicon::builder()->colors(['#000000', '#FFFFFF', '#FF0000'])->generate($input);
Size Limitations
128x128 pixels. Large sizes (>500px) may degrade performance or exceed memory limits. Test with ->size() before production use.Base64 vs. File Output
toBase64() returns a data URI (e.g., data:image/png;base64,...). Avoid embedding large base64 strings in HTML—use files or CDN-hosted assets instead.Verify Input
\Log::debug('Generating identicon for:', ['input' => $user->email]);
Check Output Paths
storage_path() or public_path() to avoid hardcoding paths. Example:
$path = public_path("avatars/{$user->id}.png");
Clear Cache
php artisan cache:clear
php artisan view:clear
Custom Color Palettes
Palette class or trait to manage dynamic color schemes (e.g., brand colors):
class BrandPalette {
public static function get(): array {
return ['#2B5CE6', '#FF7A59', '#FF5733'];
}
}
Identicon::builder()->colors(BrandPalette::get())->generate($input);
Shape Customization
Yzalis\Identicon\IdenticonGenerator to support custom shapes (e.g., circles, polygons).Laravel Service Provider
$this->app->bind(IdenticonBuilder::class, function () {
return new IdenticonBuilder(config('identicon.defaults'));
});
Queue Jobs for Async Generation
dispatch(new GenerateIdenticonJob($user->email, $user->avatar_path));
class GenerateIdenticonJob implements ShouldQueue {
public function handle() {
Identicon::generate($this->email)->save($this->path);
}
}
How can I help you explore Laravel packages today?