Provide a simple way to create letter avatar images, like those in your Gmail inbox.
composer require "tequilarapido/letter-avatar"
$letterAvatar = new LetterAvatar\LetterAvatar;
$letterAvatar
->generate('A', 40)
->saveAsPng('/path/to/file.png');
# You can pass a string, the first letter will be picked
$letterAvatar
->generate($user->getFirstName(), 40)
->saveAsPng('/path/to/file.png');
# If you don't specify a size, the max size will be used (240 by default)
The font used by default is Open Sans Regular from google font. If you want, you can use any TTF font by setting the font file full path like so :
$letterAvatar
->setFontFile('path/to/ttf_file.ttf')
->generate('B', 40)
->saveAsPng('/path/to/file.png');
$letterAvatar
->generate('J')
->saveAsJpeg('/path/to/file.jpg');
$letterAvatar
->generate('J')
->saveAsPng('/path/to/file.png');
$letterAvatar
->setTextColor([255, 0, 0])
->generate('J')
->saveAsJpeg('/path/to/file.jpg');
$letterAvatar
->setTextColor([255, 0, 0])
->generate('J')
->showTextShadow(false)
->saveAsJpeg('/path/to/file.jpg');
$letterAvatar
->setBackgroundColors([[255, 0, 0], [0, 255, 0], [0, 0, 255]])
->generate('J')
->saveAsJpeg('/path/to/file.jpg');
You can chain multiple generation to keep the same background color. (Generating multiple sizes)
$letterAvatar
->generate('J', 50)->saveAsJpeg('/path/to/50x50/file.jpg')
->generate('J', 100)->saveAsJpeg('/path/to/100x100/file.jpg');
If you want you can reset background color (so that a new random one will be used) in the middle of chaining
$letterAvatar
->generate('J', 50)->saveAsJpeg('/path/to/50x50/file.jpg')
->generate('J', 100)->saveAsJpeg('/path/to/100x100/file.jpg')
->resetBackgroundColor()
->generate('C', 100)->saveAsJpeg('/path/to/100x100/file.jpg');