Fork of lasserafn/php-initial-avatar-generator updated to use Imagine, resolving issues with PHP 8.1
This library should be pretty much a drop-in replacement for most users with the following notable changes:
- This library uses strictly defined types
- This library returns Imagine objects instead for (non-SVG) generated avatars
- Font numbers in place of files were removed
- AutoFont functionality has been removed - you can reimplement this yourself using the lasserafn/php-string-script-language package
- Only the default Open Sans Regular font is shipped with the package
- FontAwesome files are no longer shipped with the library
You just require using composer and you're good to go!
composer require allenjb/initial-avatar-generator
As with installation, usage is quite simple. Generating a image is done by running:
$avatar = new AllenJB\InitialAvatarGenerator\InitialAvatar();
$image = $avatar->name('Lasse Rafn')->generate();
Thats it! The method will return a instance of an ImageInterface from Imagine so you can save or output the image:
return $image->show('png');
If only one word is passed, the first 2 letters will be used. If more than 2 words are passed, the initials of the first and last words will be used.
$avatar = new AllenJB\InitialAvatarGenerator\InitialAvatar();
echo $avatar->name('Lasse Rafn')->generateSvg()->toXMLString(); // returns SVG XML string
$image = $avatar->name('Albert Magnum')->generate();
// will be 96 pixels wide.
$image = $avatar->width(96)->generate();
// will be 96 pixels tall.
$image = $avatar->height(96)->generate();
// will be 96x96 pixels.
$image = $avatar->size(96)->generate();
// will be red
$image = $avatar->background('#ff0000')->generate();
// will be red
$image = $avatar->color('#ff0000')->generate();
// will be Semibold
$image = $avatar->font('/path/to/fonts/OpenSans-Semibold.ttf')->generate();
$image = $avatar->fontName('Arial, Helvetica, sans-serif')->generate();
$image = $avatar->name('John Doe Johnson')->length(3)->generate(); // 3 letters = JDJ
$image = $avatar->gd()->generate(); // Uses GD driver
$image = $avatar->imagick()->generate(); // Uses Imagick driver
$image = $avatar->rounded()->generate();
Makes rounding smoother with a resizing hack. Could be slower.
$image = $avatar->rounded()->smooth()->generate();
Alternatively consider using CSS instead.
$image = $avatar->fontSize(0.25)->generate(); // Font will be 25% of image size.
If the Image size is 50px and fontSize is 0.5, the font size will be 25px.
return $avatar->name('Lasse Rafn')
->length(2)
->fontSize(0.5)
->size(96) // 48 * 2
->background('#8BC34A')
->color('#fff')
->generate()
->save('png');
Now, using that in a image (sized 48x48 pixels for retina):
<img src="url-for-avatar-generation" width="48" height="48" style="border-radius: 100%" />
Will yield:
Rounded for appearance; the actual avatar is a filled square
First, you need to "find" the respective unicode for the glyph you want to insert. For example, using FontAwesome, to display a typical "user" icon use unicode: f007
. You can usually find the unicode character (code) on the icon font browser.
An example for rendering a red avatar with a white "user" glyph would look like this:
// note that this code
// 1) uses glyph() instead of name
// 2) changes the font to FontAwesome!
return $avatar->glyph('f007')
->font('/path/to/fonts/FontAwesome5Free-Regular-400.otf')
->color('#fff')
->background('#ff0000')
->generate()
->save('png');