Larvatar is a package that combines different avatar styles, like Gravatar, Initials Avatar.
The recommended way of installing Larvatar is to use Composer. Run the following command to install it to you project:
composer require renfordt/larvatar
The general usage is simple. Create a new Larvatar class, insert name and email and the avatar type you wish.
<?php
use Renfordt\Larvatar\Enum\LarvatarTypes;
use Renfordt\Larvatar\Larvatar;
$larvatar = new Larvatar('Test Name', 'test@test.com', LarvatarTypes::InitialsAvatar);
$larvatar->setFont('Roboto,sans-serif', './font/Roboto-bold.ttf');
echo $larvatar->getImageHTML();
// if you need base64 encryption, currently this works only for InitialsAvatar
echo $larvatar->getImageHTML('base64');
// or if you need just the base64 string:
echo $larvatar->getBase64();
There are currently eight different types of avatars available:
\Renfordt\Larvatar\Enum\LarvatarTypes::InitialsAvatar; // Microsoft Teams like avatar with initials
\Renfordt\Larvatar\Enum\LarvatarTypes::Gravatar; // Gravatar
\Renfordt\Larvatar\Enum\LarvatarTypes::mp; // (Gravatar) MysticPerson, simple cartoon-style silhouette (default)
\Renfordt\Larvatar\Enum\LarvatarTypes::identicon; // (Gravatar) A geometric pattern based on a email hash
\Renfordt\Larvatar\Enum\LarvatarTypes::monsterid; // (Gravatar) A generated monster different colors and faces
\Renfordt\Larvatar\Enum\LarvatarTypes::wavatar; // (Gravatar) generated faces with differing features and backgrounds
\Renfordt\Larvatar\Enum\LarvatarTypes::retro; // (Gravatar) 8-bit arcade-style pixelated faces
\Renfordt\Larvatar\Enum\LarvatarTypes::robohash; // (Gravatar) A generated robot with different colors, faces, etc
The InitialsAvatar gives you the possibility to choose between three different forms. A circle, which is the default, a
hexagon and a square. Choose it by using the setForm()
method. The input is either a string or a value of the
Enum FormTypes
.
$larvatar = new Larvatar('Your Name', type: LarvatarTypes::InitialsAvatar);
$larvatar->initialsAvatar->setForm('circle');
$larvatar->initialsAvatar->setForm('square');
$larvatar->initialsAvatar->setForm('hexagon');
$larvatar->initialsAvatar->setForm(FormTypes::Circle);
$larvatar->initialsAvatar->setForm(FormTypes::Square);
$larvatar->initialsAvatar->setForm(FormTypes::Hexagon);
If you are using the hexagon form, you have additionally the possibility to rotate the form:
$larvatar = new Larvatar('Your Name', type: LarvatarTypes::InitialsAvatar);
$larvatar->initialsAvatar->setForm(FormTypes::Hexagon);
$larvatar->initialsAvatar->setRotation(30);
Usually the colors will be automatically selected by the provided name.
If you for some case want to manually set the contrast of the colors, you can use the methods setBackgroundLightness()
and setTextLightness()
. The parameter is a float with a value range 0
to 1
where 0
means a darker color and 1
is a lighter color.
$larvatar = new Larvatar('Your Name', type: LarvatarTypes::InitialsAvatar);
$larvatar->initialsAvatar->setBackgroundLightness(0.1);
$larvatar->initialsAvatar->setTextLightness(0.8);
Additionally, you can change the offset which will generate a different color.
$larvatar = new Larvatar('Your Name', type: LarvatarTypes::InitialsAvatar);
$larvatar->initialsAvatar->setOffset(4);
You can also change the font weight with the method setFontWeight()
.
$larvatar = new Larvatar('Your Name', type: LarvatarTypes::InitialsAvatar);
$larvatar->initialsAvatar->setFontWeight('bold');