PHPackages                             shahrestani/avatar - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. shahrestani/avatar

ActiveLibrary

shahrestani/avatar
==================

Turn name, email, and any other string into initial-based avatar or gravatar.

5.0.0(4y ago)05MITPHPPHP &gt;=7.3

Since Oct 12Pushed 4y agoCompare

[ Source](https://github.com/shahrestani/avatar)[ Packagist](https://packagist.org/packages/shahrestani/avatar)[ Docs](https://github.com/shahrestani/avatar)[ RSS](/packages/shahrestani-avatar/feed)WikiDiscussions master Synced today

READMEChangelog (1)Dependencies (8)Versions (53)Used By (0)

shahrestani/avatar
==================

[](#shahrestaniavatar)

[![SensioLabsInsight](https://camo.githubusercontent.com/32cf45b6f65a041fdfc190d95d0a30ba2dc026e0193651d75e20e1e3970258fa/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f64386134623064392d383630332d343036642d383563392d6530663566613863353635302f6d696e692e706e67)](https://insight.sensiolabs.com/projects/d8a4b0d9-8603-406d-85c9-e0f5fa8c5650)[![Travis](https://camo.githubusercontent.com/bcb82e6d81cf944fe08a1756969030f0fc409c1c3ff84a2180d94bf2c875fa2a/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f6c617261766f6c742f6176617461722e737667)](https://travis-ci.org/laravolt/avatar)[![Coverage Status](https://camo.githubusercontent.com/b1860cf68bcd8ba3a6a0e63eaf23a97af29f59e35768b03cd1932eff7fe5a9e4/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f6c617261766f6c742f6176617461722f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/laravolt/avatar?branch=master)

[![Preview](https://user-images.githubusercontent.com/149716/29503524-840ccd0c-8662-11e7-92f9-9ec3ed8a24af.png)](https://user-images.githubusercontent.com/149716/29503524-840ccd0c-8662-11e7-92f9-9ec3ed8a24af.png)

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

forked from laravolt/avatar

Preview
-------

[](#preview)

[![Preview](https://cloud.githubusercontent.com/assets/149716/26735022/6dbd77e2-47ea-11e7-8a05-7772465309c5.png)](https://cloud.githubusercontent.com/assets/149716/26735022/6dbd77e2-47ea-11e7-8a05-7772465309c5.png)

Installation
------------

[](#installation)

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

[Read more about integration with PHP project here.](#integration-with-other-php-project)

### Laravel &gt;= 5.2:

[](#laravel--52)

```
composer require shahrestani/avatar
```

### Laravel 5.1:

[](#laravel-51)

```
composer require shahrestani/avatar ~0.3
```

Service Provider &amp; Facade
-----------------------------

[](#service-provider--facade)

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

```
Shahrestani\Avatar\ServiceProvider::class,

...

'Avatar'    => Shahrestani\Avatar\Facade::class,
```

Publish Config (optional)
-------------------------

[](#publish-config-optional)

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

This will create config file located in `config/shahrestani/avatar.php`.

Lumen Service Provider
----------------------

[](#lumen-service-provider)

```
$app->register(Shahrestani\Avatar\LumenServiceProvider);
```

Usage
-----

[](#usage)

### Output as base64

[](#output-as-base64)

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

//use in view
//this will display initials JW as an image

```

### Save as file

[](#save-as-file)

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

### Output as Gravatar

[](#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:

### Output as SVG

[](#output-as-svg)

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

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

```

    @font-face {
        font-family: Laravolt;
        src: url({{ asset('fonts/laravolt.woff')) }});
    }

```

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

Get underlying Intervention image object
----------------------------------------

[](#get-underlying-intervention-image-object)

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

The method will return an instance of [Intervention image object](http://image.intervention.io/), so you can use it for further purposes.

Non-ASCII Character
-------------------

[](#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](https://github.com/danielstjules/Stringy) for providing such useful functions. What we need is just change one line in `config/avatar.php`:

```
    'ascii'    => true,
```

Configuration
-------------

[](#configuration)

```
