2017 © Pedro Peláez
 

library php-initial-avatar-generator

A package to generate avatars with initials for PHP

image

lasserafn/php-initial-avatar-generator

A package to generate avatars with initials for PHP

  • Sunday, February 11, 2018
  • by lasserafn
  • Repository
  • 11 Watchers
  • 102 Stars
  • 16,895 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 12 Forks
  • 4 Open issues
  • 24 Versions
  • 29 % Grown

The README.md

Generate avatars with initials

Ever seen those avatars (basically everywhere) that has your initials — mine would be LR; Lasse Rafn — well this package allows you to generate those, in a simple manner., (*1)

Banner , (*2)

Build Status OtterWise Coverage OtterWise Coverage StyleCI Status Total Downloads Latest Stable Version License , (*3)

OtterWise Code Coverage reporting , (*4)

Backers

Thank you to all our backers! 🙏 [Become a backer], (*5)

, (*6)

Sponsors

Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor], (*7)

, (*8)

There's also a api you can use: https://ui-avatars.com, (*9)

Installation

You just require using composer and you're good to go!, (*10)

composer require lasserafn/php-initial-avatar-generator

Rad, and long, package name.. huh? Sorry. I'm not very good with names., (*11)

Usage

As with installation, usage is quite simple. Generating a image is done by running:, (*12)

$avatar = new LasseRafn\InitialAvatarGenerator\InitialAvatar();

$image = $avatar->name('Lasse Rafn')->generate();

Thats it! The method will return a instance of Image from Intervention so you can stream, download or even encode the image:, (*13)

return $image->stream('png', 100);

You can also just pass along the initials, and it will use those. Should you just include a first name, it will use the first two letters of it., (*14)

SVG generation

$avatar = new LasseRafn\InitialAvatarGenerator\InitialAvatar();

echo $avatar->name('Lasse Rafn')->generateSvg()->toXMLString(); // returns SVG XML string

Supported methods and parameters

Of cause, passing a name is not the only thing this sweet thing does!, (*15)

Name (initials) - default: JD

$image = $avatar->name('Albert Magnum')->generate();

AutoFont - default: false

Will detect language script (using lasserafn/php-string-script-language) and use a font that supports it., (*16)

$image = $avatar->autoFont()->generate();

Width - default: 48

// will be 96 pixels wide.
$image = $avatar->width(96)->generate();

Height - default: 48

// will be 96 pixels tall.
$image = $avatar->height(96)->generate();

Size - default: 48 (proxy for $avatar->width(X)->height(X))

// will be 96x96 pixels.
$image = $avatar->size(96)->generate();

Background color - default: #f0e9e9

// will be red
$image = $avatar->background('#ff0000')->generate();

Font color - default: #8b5d5d

// will be red
$image = $avatar->color('#ff0000')->generate();

Auto Color

// Will choose a background color based on `name` and a contrasting font color. The color for a specific name will always be the same.
$image = $avatar->autoColor()->generate();

Font file - default: /fonts/OpenSans-Regular.ttf

Two fonts with two variants are included: * /fonts/OpenSans-Regular.ttf * /fonts/OpenSans-Semibold.ttf * /fonts/NotoSans-Bold.ttf * /fonts/NotoSans-Regular.ttf, (*17)

The method will look for the font, if none found it will append __DIR__ and try again, and if not it will default to the first GD Internal Font. If you input an integer between 1 and 5, it will use a GD Internal font as per that number., (*18)

// will be Semibold
$image = $avatar->font('/fonts/OpenSans-Semibold.ttf')->generate();

Font name (for SVGs) - default: Open Sans, sans-serif

$image = $avatar->fontName('Arial, Helvetica, sans-serif')->generate();

Length - default: 2

$image = $avatar->name('John Doe Johnson')->length(3)->generate(); // 3 letters = JDJ

Switching driver - default: gd

$image = $avatar->gd()->generate(); // Uses GD driver
$image = $avatar->imagick()->generate(); // Uses Imagick driver

Rounded - default: false

$image = $avatar->rounded()->generate();

Smooth - default: false

Makes rounding smoother with a resizing hack. Could be slower., (*19)

$image = $avatar->rounded()->smooth()->generate();

If you are going to use rounded(), you want to use smooth() to avoid pixelated edges. Disabled by default because it COULD be slower. I would recommend just rounding with CSS., (*20)

Font Size - default: 0.5

$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., (*21)

Chaining it all together

We will not use the ->font() method in this example; as I like the regular one., (*22)

return $avatar->name('Lasse Rafn')
              ->length(2)
              ->fontSize(0.5)
              ->size(96) // 48 * 2
              ->background('#8BC34A')
              ->color('#fff')
              ->generate()
              ->stream('png', 100);

Now, using that in a image (sized 48x48 pixels for retina):, (*23)

<img src="url-for-avatar-generation" width="48" height="48" style="border-radius: 100%" />

Will yield:, (*24)

Result, (*25)

Rounded for appearance; the actual avatar is a filled square, (*26)

Font Awesome Support

The package supports FontAwesome (v5) and already distributes the free version as otf format (see /fonts folder)., (*27)

However, when using FontAwesome you may want to display one specific icon instead of the user's initials. This package, therefore, provides a handy glyph($code) method to be used along with FontAwesome., (*28)

First, you need to "find" the respective unicode for the glyph you want to insert. For example, you may want to display a typical "user" icon (unicode: f007). The unicode is located near the name of the icon (e.g., see here the user icon as an example here: https://fontawesome.com/icons/user )., (*29)

An example for rendering a red avatar with a white "user" glyph would look like this:, (*30)

// note that we
// 1) use glyph() instead of name
// 2) change the font to FontAwesome!
return $avatar->glyph('f007')
              ->font('/fonts/FontAwesome5Free-Regular-400.otf')
              ->color('#fff')
              ->background('#ff0000')
              ->generate()
              ->stream('png', 100);

Requirements

  • PHP +7.0
  • Fileinfo Extension (from intervention/image)

Script/Language support

Some letters are not supported by the default font files, so I added some fonts to add support. You must use autoFont() to enable this feature. Supported are:, (*31)

  • Arabic
  • Armenian
  • Bengali
  • Georgian
  • Hebrew
  • Mongolian
  • Chinese
  • Thai
  • Tibetan

Contributors

Open Source is best when supported by a community. Any size of contribution is very appreciated., (*32)

Supported Image Libraries (from intervention/image)

  • GD Library (>=2.0)
  • Imagick PHP extension (>=6.5.7)

The Versions

11/02 2018

dev-master

9999999-dev

A package to generate avatars with initials for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

image avatar initials

31/01 2018

dev-analysis-z4oGrn

dev-analysis-z4oGrn

A package to generate avatars with initials for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

image avatar initials

31/01 2018

2.3.0

2.3.0.0

A package to generate avatars with initials for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

image avatar initials

13/10 2017

2.2.2

2.2.2.0

A package to generate avatars with initials for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

image avatar initials

13/10 2017

2.2.1

2.2.1.0

A package to generate avatars with initials for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

image avatar initials

13/10 2017

2.2.0

2.2.0.0

A package to generate avatars with initials for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

image avatar initials

07/10 2017

2.1.1

2.1.1.0

A package to generate avatars with initials for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

image avatar initials

02/10 2017

2.1.0

2.1.0.0

A package to generate avatars with initials for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

image avatar initials

25/09 2017

2.0.8

2.0.8.0

A package to generate avatars with initials for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

image avatar initials

24/09 2017

2.0.7

2.0.7.0

A package to generate avatars with initials for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

image avatar initials

14/06 2017

v2.0.6

2.0.6.0

A package to generate avatars with initials for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

image avatar initials

01/04 2017

dev-analysis-8jvGZR

dev-analysis-8jvGZR

A package to generate avatars with initials for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

image avatar initials

31/03 2017

v2.0.5

2.0.5.0

A package to generate avatars with initials for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

image avatar initials

14/02 2017

v2.0

2.0.0.0

A package to generate avatars with initials for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

image avatar initials

16/01 2017

v1.2

1.2.0.0

A package to generate avatars with initials for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

image avatar initials

16/01 2017

v1.1

1.1.0.0

A package to generate avatars with initials for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

image avatar initials

15/01 2017

v1.0

1.0.0.0

A package to generate avatars with initials for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

image avatar initials

15/01 2017

v0.6.5

0.6.5.0

A package to generate avatars with initials for PHP

  Sources   Download

MIT

The Requires

 

image avatar initials

15/01 2017

v0.6

0.6.0.0

A package to generate avatars with initials for PHP

  Sources   Download

MIT

The Requires

 

image avatar initials

15/01 2017

v0.5

0.5.0.0

A package to generate avatars with initials for PHP

  Sources   Download

MIT

The Requires

 

image avatar initials

15/01 2017

v0.1.7

0.1.7.0

A package to generate avatars with initials for PHP

  Sources   Download

MIT

The Requires

 

15/01 2017

v0.1.6

0.1.6.0

A package to generate avatars with initials for PHP

  Sources   Download

MIT

The Requires

 

15/01 2017

v0.1.5

0.1.5.0

A package to generate avatars with initials for PHP

  Sources   Download

MIT

The Requires

 

15/01 2017

v0.1

0.1.0.0

A package to generate avatars with initials for PHP

  Sources   Download

MIT

The Requires