bitverse/identicon
Generate deterministic SVG identicons from any string in PHP. Pluggable preprocessors (e.g., MD5) and generators; includes Rings and GitHub-style 5x5 Pixels generators. Easy to use: getIcon() returns SVG you can save or render.
Installation
composer require bitverse/identicon
Add to composer.json if using a monorepo or custom package setup.
First Use Case: Generate a Basic Identicon
use Bitverse\Identicon\Identicon;
use Bitverse\Identicon\Generator\RingsGenerator;
use Bitverse\Identicon\Preprocessor\MD5Preprocessor;
$identicon = new Identicon(new MD5Preprocessor(), new RingsGenerator());
$svg = $identicon->getIcon('user@example.com');
file_put_contents(public_path('avatars/user.svg'), $svg);
RingsGenerator is the default and most visually distinct option. For quick testing, use MD5Preprocessor (default) to ensure consistent hashing.Preprocessing
MD5Preprocessor for deterministic hashing (default).SHA1Preprocessor) if needed:
$preprocessor = new \Bitverse\Identicon\Preprocessor\SHA1Preprocessor();
Generator Selection
\Bitverse\Identicon\Generator\AbstractGenerator for unique styles.Dynamic Generation in Controllers
public function showAvatar(Request $request, $email)
{
$identicon = app(Identicon::class);
return response($identicon->getIcon($email))
->header('Content-Type', 'image/svg+xml');
}
Caching Identicons
$cacheKey = 'identicon:' . md5($email);
$svg = cache()->remember($cacheKey, now()->addDays(30), function() use ($email) {
return $identicon->getIcon($email);
});
Integration with Blade
// In a service provider
Blade::directive('identicon', function ($email) {
return "<?php echo (new \\Bitverse\\Identicon\\Identicon())->getIcon($email); ?>";
});
Usage:
<img src="{{ identicon('user@example.com') }}" alt="Identicon">
SVG Output Size
DOMDocument to minify:
$dom = new DOMDocument();
$dom->loadXML($svg);
$dom->formatOutput = true;
$minified = $dom->saveXML();
Color Consistency
Color::parseHex() for custom palettes:
$generator->setBackgroundColor(Color::parseHex('#f0f0f0'));
$generator->setForegroundColors([Color::parseHex('#3498db'), Color::parseHex('#e74c3c')]);
Color::fromHash() for dynamic but consistent colors.Performance
Deprecated Methods
Identicon::generate() (deprecated). Use getIcon() instead.Last Release (2015)
phpseclib for hashing) if critical.Invalid SVG Output
null or non-string inputs).simplexml_load_string($svg).Color Clashes
Color::parseHex() with high-contrast pairs (e.g., #2c3e50 + #ecf0f1).Caching Issues
cache()->forget('identicon:*');
Custom Generators
Extend AbstractGenerator to create new patterns:
class HexagonGenerator extends AbstractGenerator {
protected function generatePattern($hash) {
// Custom logic
}
}
Preprocessor Overrides
Implement \Bitverse\Identicon\Preprocessor\PreprocessorInterface for custom hashing (e.g., user ID + salt):
class CustomPreprocessor implements PreprocessorInterface {
public function preprocess($input) {
return hash('sha256', $input . config('app.salt'));
}
}
Symfony Integration
Use BitverseIdenticonBundle for Twig filters and routing helpers (if upgrading to Symfony 5+).
How can I help you explore Laravel packages today?