fabian-tomischka/php-avatar-generator

This package is abandoned and no longer maintained. No replacement package was suggested.

Easily create user avatars based on provided names or strings

dev-master 2018-08-27 23:01 UTC

This package is auto-updated.

Last update: 2021-09-20 10:49:08 UTC


README

Display unique avatars based on the initials of your users names. Easy to use with loads of options to extend by yourself.

Images

Installation

Require from composer and start using it!

composer require fabian-tomischka/php-avatar-generator

Usage

Quick-start with a simple line of code

use Fabito\AvatarGenerator\Avatar;

$generator = new Avatar();
$avatar = $generator->name('Fabian Tomischka')->toJpeg();

You can also use a string instead of a given name. However, when providing a string, it will ignore any given whitespaces and always take the characters from the string directly.

// Will result in Fa on the avatar instead of FT
$avatar = $generator->string('Fabian Tomischka')->toJpeg();

You can also access the image manager or canvas of the underlying Intervention/Image in case you need it

$manager = $generator->getImageManager();
$canvas = $generator->name('Fabian Tomischka')->getImageCanvas();

Customizing

The package offers multiple ways of adjusting and tweaking the settings.

driver('driver')

The driver which is used for generating the images. Currently supported: gd and imagick. Default: gd

$avatar = $generator->driver($generator::DRIVER_IMAGICK)->toBase64();

height('px'), width('px'), size('px'), dimensions('width', 'height')

Change the dimensions of the avatar. Usually size will do the job. Default: 100px

$avatar = $generator->size(100)->toBase64();

backgroundColor('color')

In case you do not want to use the default colors provided by the package you can manually set the color here

$avatar = $generator->backgroundColor('#000000')->toBase64();

fontColor('color')

Change the font color. Default: #F7F7F7

$avatar = $generator->fontColor('#000000')->toBase64();

fontSize('px')

Change the font size on the avatar. Default: 42px

$avatar = $generator->fontSize(42)->toBase64();

length('int')

In case you require less or more characters to be on the avatar. Default: 2

// Output: F
$avatar = $generator->name('Fabian Tomischka')->length(1)->toBase64();
$avatar = $generator->string('FT')->length(1)->toBase64();

Loading custom fonts

By default, the package comes with pre-installed OpenSans Light and Regular fonts. They usually do the job. In case you want to change your font, you can do that by calling the font method and providing a path to the new font:

$avatar = $generator->font('/dir/to/the/font.ttf')->name('Fabian Tomischka')->toBase64();

Make sure that the font is available as TTF!

Getting image data

For easy access the package offers multiple direct methods to access the image in different file formats.

$avatar = $generator->name('Fabian Tomischka')->toBase64(); // Base 64
$avatar = $generator->name('Fabian Tomischka')->toPng(); // PNG
$avatar = $generator->name('Fabian Tomischka')->toJpeg(); // JPEG
$avatar = $generator->name('Fabian Tomischka')->toWebp; // WebP

As mentioned above, in case you need access to the underlying manager or image, you can access them via

$manager = $generator->getImageManager();
$canvas = $generator->name('Fabian Tomischka')->getImageCanvas();

Rounded avatars

The package potentially offers you an option to generate rounded avatars.

$avatar = $generator->rounded()->name('Fabian Tomischka')->toBase64();

However I do highly suggest using border radius in CSS to display your avatar as a circle. Generating rounded avatars will always result in a lower quality.

Requirements

  • PHP 7.1.7 or higher
  • Intervention/Image 2.4 or higher
  • Imagick extension (in case you do not use the default GD)