Based and inspired on
- github_avatars_generator (matrix)
- identicon (color)
composer require usarise/identicon
<?php
declare(strict_types=1);
require_once __DIR__ . '/vendor/autoload.php';
use Usarise\Identicon\Identicon;
use Usarise\Identicon\Image\Svg\Canvas as SvgCanvas;
$identicon = new Identicon(
new SvgCanvas(),
420,
);
$response = $identicon->generate('test');
header("Content-type: {$response->mimeType}");
echo (string) $response;
<?php
declare(strict_types=1);
require_once __DIR__ . '/vendor/autoload.php';
use Usarise\Identicon\Identicon;
use Usarise\Identicon\Image\Svg\Canvas as SvgCanvas;
$identicon = new Identicon(
new SvgCanvas(),
420,
);
$response = $identicon->generate('test');
$response->save("test.{$response->format}");
<?php
declare(strict_types=1);
require_once __DIR__ . '/vendor/autoload.php';
use Usarise\Identicon\Identicon;
use Usarise\Identicon\Image\Svg\Canvas as SvgCanvas;
$identicon = new Identicon(
new SvgCanvas(),
420,
);
$response = $identicon->generate('test');
$data = sprintf(
'data:%s;base64,%s',
$response->mimeType,
base64_encode(
(string) $response,
),
);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Identicon Data URL example</title>
</head>
<body>
<img src="<?php echo $data; ?>" />
</body>
</html>
use Usarise\Identicon\Image\Svg\Canvas as SvgCanvas;
use Usarise\Identicon\{Identicon, Resolution};
$identicon = new Identicon(
image: new SvgCanvas(), // implementation Usarise\Identicon\Image\CanvasInterface
size: 420, // 420x420 pixels
resolution: Resolution::Medium, // Resolution 10x10 (Default)
);
Implementations Usarise\Identicon\Image\CanvasInterface
from the box
use Usarise\Identicon\Image\Gd\Canvas as GdCanvas;
use Usarise\Identicon\Image\Imagick\Canvas as ImagickCanvas;
use Usarise\Identicon\Image\Svg\Canvas as SvgCanvas;
new GdCanvas()
new ImagickCanvas()
new SvgCanvas()
Output image height and width
Must be a positive multiple of the resolution
Example: 120 for resolution Resolution::Medium
and
126 for resolution Resolution::Large
Pixel resolution of the pattern identicon
use Usarise\Identicon\Resolution;
Resolution::Tiny
Resolution::Small
Resolution::Medium
Resolution::Large
Resolution::Huge
Username, id, email, ip, etc
$response = $identicon->generate(string: 'test')
CSS 6-digit or 3-digit hex color
$response = $identicon->generate(string: 'test', background: '#f2f1f2')
CSS 6-digit or 3-digit hex color
$response = $identicon->generate(string: 'test', foreground: '#84c7b5')
CSS 6-digit or 3-digit hex color
$response = $identicon->generate(string: 'test', background: '#f2f1f2', foreground: '#84c7b5')
png
, svg
, other custom
$response->format // svg
image/png
, image/svg+xml
, other custom
$response->mimeType // image/svg+xml
Compressed image
$response->output
Compressed image
(string) $response
object
, string
, null
$response->image // object
Allowed file extension only $response->format
$response->save(path: __DIR__ . '/test.svg')