laravolt / avatar by uyab

Turn name, email, and any other string into initial-based avatar or gravatar.
3,152,811
1,854
34
Package Data
Maintainer Username: uyab
Maintainer Contact: uyab.exe@gmail.com (Bayu Hendra Winata)
Package Create Date: 2015-10-12
Package Last Update: 2024-04-05
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2024-04-18 03:01:16
Package Statistics
Total Downloads: 3,152,811
Monthly Downloads: 83,886
Daily Downloads: 3,732
Total Stars: 1,854
Total Watchers: 34
Total Forks: 173
Total Open Issues: 10

laravolt/avatar

SensioLabsInsight Travis Coverage Status

Preview

Display unique avatar for any user based on their (initials) name.

Preview

Preview

Installation

This package originally built for Laravel, but can also be used in any PHP project.

Read more about integration with PHP project here.

Laravel >= 5.2:

$ composer require laravolt/avatar

Laravel 5.1:

composer require laravolt/avatar ~0.3

Service Provider & Facade

Note: only for Laravel 5.4 and below, because since Laravel 5.5 we use package auto-discovery.

Laravolt\Avatar\ServiceProvider::class,

...

'Avatar'    => Laravolt\Avatar\Facade::class,

Publish Config (Optional)

php artisan vendor:publish --provider="Laravolt\Avatar\ServiceProvider"

This will create config file located in config/laravolt/avatar.php.

Lumen Service Provider

$app->register(Laravolt\Avatar\LumenServiceProvider);

Usage

Output As Base64

//this will output data-uri (base64 image data)
//something like data:image/png;base64,iVBORw0KGg....
Avatar::create('Joko Widodo')->toBase64();

//use in view
//this will display initials JW as an image
<img src="{{ Avatar::create('Joko Widodo')->toBase64() }}" />

Save As File

Avatar::create('Susilo Bambang Yudhoyono')->save('sample.png');
Avatar::create('Susilo Bambang Yudhoyono')->save('sample.jpg', 100); // quality = 100

Output As Gravatar

Avatar::create('uyab@example.net')->toGravatar();
// Output: http://gravatar.com/avatar/0dcae7d6d76f9a3b14588e9671c45879

Avatar::create('uyab@example.net')->toGravatar(['d' => 'identicon', 'r' => 'pg', 's' => 100]);
// Output: http://gravatar.com/avatar/0dcae7d6d76f9a3b14588e9671c45879?d=identicon&r=pg&s=100

Gravatar parameter reference: https://en.gravatar.com/site/implement/images/

Output As SVG

Avatar::create('Susilo Bambang Yudhoyono')->toSvg();

You may specify custom font-family for your SVG text.

<head>
    <!--Prepare custom font family, using Google Fonts-->
    <link href="https://fonts.googleapis.com/css?family=Laravolt" rel="stylesheet">

    <!--OR-->
    
    <!--Setup your own style-->
    <style>
    @font-face {
        font-family: Laravolt;
        src: url({{ asset('fonts/laravolt.woff')) }});
    }
</style>
</head>
Avatar::create('Susilo Bambang Yudhoyono')->setFontFamily('Laravolt')->toSvg();

Get underlying Intervention image object

Avatar::create('Abdul Somad')->getImageObject();

The method will return an instance of Intervention image object, so you can use it for further purposes.

Non-ASCII Character

By default, this package will try to output any initials letter as it is. If the name supplied contains any non-ASCII character (e.g. ā, Ě, ǽ) then the result will depend on which font used (see config). It the font supports characters supplied, it will successfully displayed, otherwise it will not.

Alternatively, we can convert all non-ascii to their closest ASCII counterparts. If no closest coutnerparts found, those characters are removed. Thanks to Stringy for providing such useful functions. What we need is just change one line in config/avatar.php:

    'ascii'    => true,

Configuration

<?php
/*
 * Set specific configuration variables here
 */
return [

    /*
    |--------------------------------------------------------------------------
    | Image Driver
    |--------------------------------------------------------------------------
    | Avatar use Intervention Image library to process image.
    | Meanwhile, Intervention Image supports "GD Library" and "Imagick" to process images
    | internally. You may choose one of them according to your PHP
    | configuration. By default PHP's "GD Library" implementation is used.
    |
    | Supported: "gd", "imagick"
    |
    */
    'driver'    => 'gd',

    // Initial generator class
    'generator' => \Laravolt\Avatar\Generator\DefaultGenerator::class,

    // Whether all characters supplied must be replaced with their closest ASCII counterparts
    'ascii'    => false,

    // Image shape: circle or square
    'shape' => 'circle',

    // Image width, in pixel
    'width'    => 100,

    // Image height, in pixel
    'height'   => 100,

    // Number of characters used as initials. If name consists of single word, the first N character will be used
    'chars'    => 2,

    // font size
    'fontSize' => 48,

    // Font family to be used in SVG text
    'fontFamily' => null,

    // convert initial letter to uppercase
    'uppercase' => false,

    // Fonts used to render text.
    // If contains more than one fonts, randomly selected based on name supplied
    'fonts'    => ['path/to/OpenSans-Bold.ttf', 'path/to/rockwell.ttf'],

    // List of foreground colors to be used, randomly selected based on name supplied
    'foregrounds'   => [
        '#FFFFFF'
    ],

    // List of background colors to be used, randomly selected based on name supplied
    'backgrounds'   => [
        '#f44336',
        '#E91E63',
        '#9C27B0',
        '#673AB7',
        '#3F51B5',
        '#2196F3',
        '#03A9F4',
        '#00BCD4',
        '#009688',
        '#4CAF50',
        '#8BC34A',
        '#CDDC39',
        '#FFC107',
        '#FF9800',
        '#FF5722',
    ],

    'border'    => [
        'size'  => 1,
        
        // border color, available value are:
        // 'foreground' (same as foreground color)
        // 'background' (same as background color)
        // or any valid hex ('#aabbcc')
        'color' => 'foreground'
    ]
];

Overriding config at runtime

We can overriding configuration at runtime by using following functions:

Avatar::create('Soekarno')->setDimension(100);//width = height = 100 pixel
Avatar::create('Soekarno')->setDimension(100, 200); // width = 100, height = 200
Avatar::create('Soekarno')->setBackground('#001122');
Avatar::create('Soekarno')->setForeground('#999999');
Avatar::create('Soekarno')->setFontSize(72);
Avatar::create('Soekarno')->setFont('/path/to/font.ttf');
Avatar::create('Soekarno')->setBorder(1, '#aabbcc'); // size = 1, color = #aabbcc
Avatar::create('Soekarno')->setShape('square');

// chaining
Avatar::create('Habibie')->setDimension(50)->setFontSize(18)->toBase64();

Integration With Other PHP Project

// include composer autoload
require 'vendor/autoload.php';

// import the Avatar class
use Laravolt\Avatar\Avatar;

// create your first avatar
$avatar = new Avatar($config);
$avatar->create('John Doe')->toBase64();
$avatar->create('John Doe')->save('path/to/file.png', $quality = 90);

$config is just an ordinary array with same format as explained above (See Configuration).