> For the complete documentation index, see [llms.txt](https://pixiedia.gitbook.io/maginium/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://pixiedia.gitbook.io/maginium/the-basics/avatar.md).

# Avatar

* [Introduction](#introduction)
* [Usage](#usage)

## [Introduction](#introduction)

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

{% hint style="warning" %}
This Module is inspired by \[Laravolt]\(<https://github.com/laravolt/avatar>)
{% endhint %}

## [Usage](#usage)

#### [Output as Base64](#output-as-base64)

```php
// This will output data-uri (base64 image data), e.g., data:image/png;base64,iVBORw0KGg...
Avatar::create('Joko Widodo')->toBase64();

// Use in view to display initials as an image
<img src="{{ Avatar::create('Joko Widodo')->toBase64() }}" />
```

#### [Save as File](#save-as-file)

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

#### [Output as Gravatar](#output-as-gravatar)

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

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

{% hint style="info" %}
Gravatar parameter reference
{% endhint %}

#### [Output as SVG](#output-as-svg)

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

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

```php
<head>
    <!-- Using Google Fonts -->
    <link href="https://fonts.googleapis.com/css?family=Inter" rel="stylesheet">

    <!-- OR Setup custom style -->
    <style>
    @font-face {
        font-family: Inter;
        src: url({{ asset('fonts/inter.woff') }});
    }
    </style>
</head>
```

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

#### [Get Underlying Intervention Image Object](#get-underlying-intervention-image-object)

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

Returns an instance of the Intervention image object, enabling further processing.

#### [Non-ASCII Character](#non-ascii-character)

The library tries to render initials as supplied. For non-ASCII characters (e.g., ā, Ě), their visibility depends on font support. Alternatively, non-ASCII characters can be converted to their closest ASCII counterparts using Stringy.

#### [Overriding Config at Runtime](#override-config-at-runtime)

```php
Avatar::create('Soekarno')->setDimension(100); // Width = Height = 100 pixels
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')->setBorder(1, '#aabbcc', 10); // Border radius = 10 (for SVG only)
Avatar::create('Soekarno')->setShape('square');

// Since v3.0.0
Avatar::create('Soekarno')->setTheme('colorful'); // Set exact theme
Avatar::create('Soekarno')->setTheme(['grayscale-light', 'grayscale-dark']); // Random theme from options

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