MonsterID

Packagist GitLab GitHub Bitbucket Gitea

MonsterID is a method to generate a unique monster image based upon a certain identifier (IP address, email address, whatever). It can be used to automatically provide personal avatar images in blog comments or other community services.

Monster Example

MonsterID was inspired by a post by Don Park and the Combinatoric Critters.

Installation

Install it with Composer

composer require sandfoxme/monsterid

Usage

Function-style

Get PNG as a string:

<?php

use function SandFox\MonsterID\build_monster;

// output to browser
header('Content-type: image/png');
echo build_monster('email@example.com', 150);

Put PNG to a stream:

<?php

use function SandFox\MonsterID\stream_monster;

// save to file
$stream = fopen('avatar.png', 'w');
stream_monster($stream, 'email@example.com', 150);
fclose($stream);

Export GD object:

<?php

use function SandFox\MonsterID\build_monster_gd;

// convert it to a different format for example
$gd = build_monster_gd('email@example.com', 150); // a copy of the internal gd object
header('Content-type: image/avif');
imageavif($gd);
imagedestroy($gd); // it's your responsibility to destroy the resource (PHP <= 8.0)

Object-style

<?php

use SandFox\MonsterID\Monster;

$monster = new Monster('email@example.com', 150);

// output it to browser
header('Content-type: image/png');
echo $monster->getImage();

// save it to file
$monster->writeToStream(fopen('avatar.png', 'w'));

// gd
header('Content-type: image/avif');
imageavif($monster->getGdImage());

Upgrade from 1.x

  • Expect different images to be generated

  • Namespace SandFoxMe\MonsterID is removed, use SandFox\MonsterID

  • Object style changes

    <?php
    
    use SandFox\MonsterID\Monster;
    
    // 1.x
    (new Monster('email@example.com'))->build(150);
    // 2.x
    (new Monster('email@example.com', 150))->getImage();
    
    • Size parameter moved to the constructor

    • build() is now getImage()

License

All graphics were created by Andreas Gohr. The source code and the graphics are provided under the MIT License.

Adaptation as a composer library for modern PHP was performed by Anton “Sand Fox” Smirnov