usarise/identicon

A PHP library for generating identicons.

0.8.2 2024-09-07 14:02 UTC

This package is auto-updated.

Last update: 2024-11-22 16:31:25 UTC


README

PHP Version Latest Version License Total Downloads GitHub CI

Based and inspired on

test

Installation

composer require usarise/identicon

Usage browser

<?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;

Usage write file

<?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}");

Usage data url

<?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>

Usage advanched

Construct

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)
);

Image Canvas

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;

Gd extension (GD Library)

new GdCanvas()

Imagick extension (ImageMagick)

new ImagickCanvas()

SVG

new SvgCanvas()

Size

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

Resolutions

Pixel resolution of the pattern identicon

use Usarise\Identicon\Resolution;

6x6 (Tiny)

Tiny

Resolution::Tiny

8x8 (Small)

Small

Resolution::Small

10x10 (Medium)

Medium

Resolution::Medium

12x12 (Large)

Large

Resolution::Large

14x14 (Huge)

Huge

Resolution::Huge

Generate

String

Username, id, email, ip, etc

$response = $identicon->generate(string: 'test')

Background

CSS 6-digit or 3-digit hex color

$response = $identicon->generate(string: 'test', background: '#f2f1f2')

Foreground

CSS 6-digit or 3-digit hex color

$response = $identicon->generate(string: 'test', foreground: '#84c7b5')

Background and Foreground

CSS 6-digit or 3-digit hex color

$response = $identicon->generate(string: 'test', background: '#f2f1f2', foreground: '#84c7b5')

Response

Format

png, svg, other custom

$response->format // svg

Mime type

image/png, image/svg+xml, other custom

$response->mimeType // image/svg+xml

Output string

Compressed image

$response->output

Alternative $response->output: response object to string

Compressed image

(string) $response

Uncompressed image

object, string, null

$response->image // object

Save file

Allowed file extension only $response->format

$response->save(path: __DIR__ . '/test.svg')