PHPackages                             sowrensen/svg-avatar-generator - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. sowrensen/svg-avatar-generator

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

sowrensen/svg-avatar-generator
==============================

Offline SVG avatar generator for Laravel

v2.0.1(1y ago)67.7k—1.8%1[2 PRs](https://github.com/sowrensen/svg-avatar-generator/pulls)MITPHPPHP ^8.2CI passing

Since Feb 17Pushed 4mo ago1 watchersCompare

[ Source](https://github.com/sowrensen/svg-avatar-generator)[ Packagist](https://packagist.org/packages/sowrensen/svg-avatar-generator)[ Docs](https://github.com/sowrensen/svg-avatar-generator)[ RSS](/packages/sowrensen-svg-avatar-generator/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (7)Versions (16)Used By (0)

Offline SVG Avatar Generator for Laravel
========================================

[](#offline-svg-avatar-generator-for-laravel)

[![Latest Version on Packagist](https://camo.githubusercontent.com/d078528d1b19681ae8720d9104907773e93c1ecdf3c10e7dfeeefb91ea1036ab/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f736f7772656e73656e2f7376672d6176617461722d67656e657261746f722e737667)](https://packagist.org/packages/sowrensen/svg-avatar-generator)[![GitHub Tests Action Status](https://camo.githubusercontent.com/c089ab352317367d33ad4cfead3a66bd866feb164fae8eb3f3edc90e05318b53/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f736f7772656e73656e2f7376672d6176617461722d67656e657261746f722f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d5465737473)](https://github.com/sowrensen/svg-avatar-generator/actions?query=workflow%3ATests+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/17131a623f7a3dd67e84ae3eb8b0dccd7e30a89863780608369f6930d696f353/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f736f7772656e73656e2f7376672d6176617461722d67656e657261746f722e737667)](https://packagist.org/packages/sowrensen/svg-avatar-generator)

> If you find this package useful, please consider to ⭐ it. That would be lit!

Generating SVG avatars on the fly is nothing new. There are tons of free/paid services and packages available to do that. So, why another package for same task?

Well, this one has some subtle but nifty advantages over available packages, here's a few of them:

- Supports custom font. 🧣
- Supports gradient background. 🦜
- Supports random gradients based on defined presets in config. 🦚
- Multiple shapes: rectangular, rounded-rectangular, or circular. 💎
- Ability to customize initials and extractor. ✍🏼
- No external api call is required, it's totally offline. 🛰️
- Unlike some other available options, doesn't require heavy-weight image processing libraries like **Intervention**. 🧺
- Doesn't have any binary dependency, so nothing needs to be installed on server. 🗃️

Requirements
------------

[](#requirements)

LaravelPHPSVGAvatarGenerator&gt;=11.x&gt;=8.22.x9.x-10.x&gt;=8.11.xInstallation
------------

[](#installation)

Install the package via composer:

```
composer require sowrensen/svg-avatar-generator
```

For older versions:

```
composer require sowrensen/svg-avatar-generator:^1.0
```

Caution

**Breaking change**: Named color support, e.g. red, green in `foreground` and `gradient_colors` is dropped since version 2.0. If you're using such names in config or in code, you should change them to hexadecimal code or keep using version 1.x releases.

Optionally, you can publish the config file with:

```
php artisan vendor:publish --tag="svg-avatar-generator-config"
```

Important

You should republish the config file after updating.

Usage
-----

[](#usage)

### As model accessor

[](#as-model-accessor)

The usage of this package is stupidly simple. Use the `svg-avatar.php` config file to set your preferred decoration. Then use the Facade to generate an avatar on the fly. The recommended way to achieve that is defining an accessor in your model:

```
use Illuminate\Database\Eloquent\Model;
use \Illuminate\Database\Eloquent\Casts\Attribute;

class User extends Model
{
    //...

    public function profilePhoto(): Attribute
    {
        return Attribute::get(function ($value, $attributes) {
            // If profile photo is set, return the original
            if (! is_null($attributes['profile_photo'])) {
                return $attributes['profile_photo'];
            }

            // Else, generate one
            return \Svg::for($attributes['name'])->toUrl();
        });
    }
}
```

Tip

If your accessor is different from the original attribute, you might want to put it in `$appends` array so that it loads automatically with your model.

### Override default config

[](#override-default-config)

If you want you can generate an avatar totally different from your configured style. It has all helpful methods to make that possible:

```
use Sowren\SvgAvatarGenerator\Facades\Svg;
use Sowren\SvgAvatarGenerator\Enums\FontWeight;

Svg::for('John Doe')
    ->asCircle() // or, asRectangle() along with optional setCornerRadius($radius) method
    ->setSize(64)
    ->setCustomFontUrl('https://api.fontshare.com/v2/css?f[]=kola@400&display=swap')
    ->setFontFamily('Kola')
    ->setFontSize(40)
    ->setFontWeight(FontWeight::SEMIBOLD)
    ->setForeground('#FFFFFF')
    ->setGradientColors( // set of 3 different gradients
      ['#4158D0', '#C850C0', '#FFCC70'],
      ['#00DBDE', '#FC00FF'],
      ['#FF9A8B', '#FF6A88', '#FF99AC']
    )
    ->setGradientStops(0, .5, 1)
    ->setGradientRotation(120)
    ->render();
```

### Customize initials

[](#customize-initials)

You can define the second initial using studly case. For example,

Provided stringProduced initialJohn DoeJDJohnDoeJDJohndoeJOJohndoEJE### Customize Extractor

[](#customize-extractor)

The default initial extractor class produces results shown above. However, if you want something different, you can create your own Extractor class. To do so create a new class that implements `Sowren\SvgAvatarGenerator\Extractors\Extractor` interface. An ideal place to put this class would be under `App\Extractors` namespace in your app directory.

```
