This guide helps you migrate from Identicon v2 to v3.
- v2: PHP 5.5+
- v3: PHP 8.1+ (required for readonly classes, enums, etc.)
// v2
use Identicon\Identicon;
// v3
use Yzalis\Identicon\Identicon;- v2: MD5 (hardcoded)
- v3: SHA-256 (default), MD5 available via
useMd5()
This means identicons will look different for the same input!
// v2
$identicon = new Identicon();
$identicon = new Identicon(new SvgGenerator());
// v3
$identicon = new Identicon();
$identicon = (new IdenticonBuilder())->useSvg()->build();// v2 - parameters on each call
$identicon->displayImage('email@example.com', 128, '#ff0000', '#ffffff');
$data = $identicon->getImageData('email@example.com', 128, '#ff0000', '#ffffff');
$uri = $identicon->getImageDataUri('email@example.com', 128);
// v3 - configure once, generate multiple
$identicon = (new IdenticonBuilder())
->size(128)
->color('#ff0000')
->backgroundColor('#ffffff')
->build();
$identicon->displayImage('email@example.com');
$data = $identicon->getImageData('email@example.com');
$uri = $identicon->getImageDataUri('email@example.com');
// v3 - quick one-liner
$uri = Identicon::generate('email@example.com', 128);setGenerator()- UseIdenticonBuilder->renderer()insteadgetImageResource()- UsegetImageData()instead
// v2
$color = $identicon->getColor(); // Returns array ['r' => X, 'g' => Y, 'b' => Z]
// v3
$color = $identicon->getColor('email@example.com'); // Returns Color object
echo $color->red;
echo $color->toHex();If you need the same identicons as v2 (important for existing user avatars):
use Yzalis\Identicon\IdenticonBuilder;
$identicon = (new IdenticonBuilder())
->useV2Compatibility() // Activates MD5 + legacy color extraction + 5×5 grid
->build();
$uri = $identicon->getImageDataUri('email@example.com');// Custom grid size (3-25)
$identicon = (new IdenticonBuilder())
->gridSize(7) // 7×7 grid instead of default 5×5
->build();
// Auto-calculate based on image size
$identicon = (new IdenticonBuilder())
->size(1024)
->autoGridSize() // Will use ~17×17 grid
->build();// Fixed margin in pixels
$identicon = (new IdenticonBuilder())
->margin(16)
->build();
// Margin as percentage
$identicon = (new IdenticonBuilder())
->size(200)
->marginPercent(10) // 20px margin
->build();
// Different margins per side
$identicon = (new IdenticonBuilder())
->margin(['top' => 10, 'right' => 20, 'bottom' => 10, 'left' => 20])
->build();// v3 uses SHA-256 by default
$identicon = (new IdenticonBuilder())->build();
// Explicitly choose algorithm
$identicon = (new IdenticonBuilder())
->useSha256() // or ->useMd5()
->build();v3 uses HSL color space for better color distribution:
- Guaranteed vibrant colors (45-75% saturation)
- Guaranteed visible colors (40-60% lightness)
- Better color variety across inputs
$identicon->saveToFile('email@example.com', '/path/to/avatar.png');- Update
composer.jsonto require PHP 8.1+ - Run
composer update yzalis/identicon - Update namespace imports:
Identicon\→Yzalis\Identicon\ - Replace
new Identicon($generator)withIdenticonBuilder - Move parameters from method calls to builder configuration
- Update
getColor()calls to pass the input string - If visual compatibility is needed, add
->useV2Compatibility() - Run tests to verify behavior