Installation:
composer require bitverse/identicon-bundle
Enable the bundle in config/bundles.php (Symfony 4+) or app/AppKernel.php (Symfony 2/3):
Bitverse\IdenticonBundle\BitverseIdenticonBundle::class => ['all' => true],
First Use Case: Generate an identicon for a string (e.g., username, email hash) in a controller or service:
use Symfony\Component\HttpFoundation\Response;
class UserController extends AbstractController
{
public function show(User $user): Response
{
$identicon = $this->get('identicon')->getIcon($user->email);
return new Response($identicon);
}
}
Where to Look First:
config/packages/bitverse_identicon.yaml (Symfony 4+) or app/config/config.yml (Symfony 2/3).identicon service is auto-registered and ready to use.PixelsGenerator (outputs SVG by default; check Generator classes for alternatives).Generating Identicons:
$identicon = $this->get('identicon')->getIcon('user@example.com');
MD5Preprocessor by configuring a custom class in bitverse_identicon.preprocessor.class.
Example: Use Bitverse\Identicon\Preprocessor\SHA1Preprocessor for SHA-1 hashing.Integration with Twig: Pass the identicon service to Twig templates:
{% set identicon = app.service('identicon').getIcon(user.email) %}
<img src="data:image/svg+xml;base64,{{ identicon|base64_encode }}">
Or create a Twig extension for reusable logic.
Dynamic Identicon URLs:
Generate URLs for identicons in routes (e.g., /identicon/{hash}):
// routes.yaml
identicon:
path: /identicon/{hash}
controller: Bitverse\IdenticonBundle\Controller\IdenticonController::generate
Use this in templates:
<img src="{{ path('identicon', {'hash': user.email}) }}">
Batch Generation:
Pre-generate identicons for users and store them in the database (e.g., as identicon_svg column):
$user->identicon = $this->get('identicon')->getIcon($user->email);
$user->save();
Custom Generators:
Extend Bitverse\Identicon\Generator\AbstractGenerator to create new styles (e.g., circular identicons). Configure in bitverse_identicon.generator.class.
Caching: Cache generated identicons to avoid reprocessing:
$cache = $this->get('cache.app');
$cacheKey = 'identicon_' . md5($input);
if (!$cache->has($cacheKey)) {
$identicon = $this->get('identicon')->getIcon($input);
$cache->set($cacheKey, $identicon, 3600); // Cache for 1 hour
} else {
$identicon = $cache->get($cacheKey);
}
Dependency Injection:
Inject the identicon service into services/controllers:
# config/services.yaml
App\Service\UserService:
arguments:
$identicon: '@identicon'
Deprecated Symfony 2/3:
flex recipes or manually configure bundles in config/bundles.php.AppKernel with Kernel and update service registration.symfony/dependency-injection to ^4.0).Output Format:
PixelsGenerator outputs SVG. If you need PNG/JPEG:
ImagineBundle to convert SVG to PNG.AbstractGenerator to implement raster output.Configuration Overrides:
background_color requires hexadecimal values (e.g., #FFFFFF). Invalid values may throw errors.Service Not Found:
@identicon fails to resolve, ensure:
bundles.php/AppKernel.Performance:
Check Generator Output:
getIcon() to debug issues:
file_put_contents('debug.svg', $this->get('identicon')->getIcon('test'));
Preprocessor Debugging:
Bitverse\Identicon\Preprocessor\MD5Preprocessor to log input/output:
public function preprocess($input) {
error_log("Input: $input");
return parent::preprocess($input);
}
Configuration Errors:
InvalidConfigurationException for malformed YAML. Use:
php bin/console debug:config bitverse_identicon
to validate config.Custom Preprocessors:
Bitverse\Identicon\Preprocessor\PreprocessorInterface to add logic (e.g., truncate long strings):
class TruncatePreprocessor implements PreprocessorInterface {
public function preprocess($input) {
return substr($input, 0, 16); // Truncate to 16 chars
}
}
bitverse_identicon.preprocessor.class.Generator Extensions:
AbstractGenerator. Example:
class CircleGenerator extends AbstractGenerator {
protected function generatePixels($hash) {
// Custom logic for circular identicons
}
}
bitverse_identicon.generator.class.Event Listeners:
# config/services.yaml
App\EventListener\IdenticonListener:
tags:
- { name: kernel.event_listener, event: user.created, method: onUserCreated }
public function onUserCreated(UserCreatedEvent $event) {
$user = $event->getUser();
$user->identicon = $this->identicon->getIcon($user->email);
$user->save();
}
Twig Filters:
// src/Twig/AppExtension.php
public function getFilters() {
return [
new TwigFilter('identicon', [$this->identicon, 'getIcon']),
];
}
Usage:
<img src="data:image/svg+xml;base64,{{ 'user@example.com'|identicon|base64_encode }}">
How can I help you explore Laravel packages today?