laravolt/avatar

repository·master·Indexed 24 days ago

https://github.com/laravolt/avatar

A PHP package for generating unique user avatars based on initials. It supports multiple output formats including Base64, SVG, files, and Gravatar URLs. The library provides a Laravel facade, Lumen service provider, and integration options for standard PHP projects. Advanced features include HD avatar generation, responsive sizes, batch exporting, sprite sheet creation, and storage optimization with automated cleanup.

Tokens
4.3K
Snippets
7
Records
41
Agent score
80%

What's inside laravolt/avatar

  1. Publish the Avatar configuration

    master

    To customize the avatar generation settings, publish the configuration file to config/laravolt/avatar.php using the following Artisan command:

    php artisan vendor:publish --provider="Laravolt\Avatar\ServiceProvider"
  2. Configure non-ASCII character handling

    master
    By default, the package attempts to output initials as they are. If the font doesn't support the character, it may not display correctly. To convert non-ASCII characters to their closest ASCII counterparts (or remove them if no counterpart exists), set 'ascii' => true in config/avatar.php.
  3. Publish Avatar configuration files

    master

    The package provides two configuration files that can be published to your application's config directory for customization. Use the standard Laravel vendor:publish command with the config tag.

    Available configuration files:

    • laravolt.avatar: Main configuration.
    • laravolt.avatar.hd: High-definition (HD) configuration.
  4. Integrate Laravolt Avatar into Lumen

    master

    To use Laravolt Avatar in a Lumen application, register the Laravolt\Avatar\LumenServiceProvider in your bootstrap/app.php file. This provider binds the avatar and avatar.generator services to the application container.

    Once registered, you can resolve the avatar service from the container using the avatar key.

  5. Generate avatars as SVG

    master
    Use toSvg() to output the avatar as an SVG. You can customize the font family using setFontFamily() and make the SVG responsive using setResponsive() (which excludes height and width attributes).
  6. Override configuration at runtime

    master

    You can chain methods on the Avatar::create() call to override default configuration settings for a specific instance.

    Avatar::create('Soekarno')->setDimension(100); // width = height = 100
    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');
    Avatar::create('Soekarno')->setBorder(1, '#aabbcc', 10); // radius (only for SVG)
    Avatar::create('Soekarno')->setShape('square');
    Avatar::create('Soekarno')->setTheme('colorful');
    Avatar::create('Soekarno')->setTheme(['grayscale-light', 'grayscale-dark']);
    
    // Chaining example
    Avatar::create('Habibie')->setDimension(50)->setFontSize(18)->toBase64();