mbonghanoy/avatar

Generate simple SVG avatars from names or labels.

Maintainers

Package info

github.com/mbonghanoy/php-avatar-generator

pkg:composer/mbonghanoy/avatar

Statistics

Installs: 0

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2026-04-21 10:29 UTC

This package is auto-updated.

Last update: 2026-04-21 10:32:52 UTC


README

Generate simple SVG avatars from names or labels.

Features

  • Deterministic background colors based on the input text
  • UTF-8 safe initials generation
  • Circle and square avatar shapes
  • SVG output or base64 data URI output
  • No external runtime dependencies beyond ext-mbstring

Installation

composer require mbonghanoy/avatar

Usage

<?php

require 'vendor/autoload.php';

use Markb\Avatar\Avatar;

$svg = Avatar::make('John Doe')->svg();

$dataUri = Avatar::make('Jane Smith')
    ->size(160)
    ->background('#1d4ed8')
    ->color('#ffffff')
    ->circle()
    ->toDataUri();

API

Avatar::make(string $text): Avatar

Creates a new avatar instance using the provided text.

size(int $size): Avatar

Sets the avatar width and height in pixels.

  • The size must be greater than 0.

background(string $color): Avatar

Sets the background color using a hex value like #fff or #ffffff.

color(string $color): Avatar

Sets the text color using a hex value like #fff or #ffffff.

circle(): Avatar

Renders the avatar as a circle.

square(): Avatar

Renders the avatar as a square.

svg(): string

Returns the generated avatar as an SVG string.

toDataUri(): string

Returns the generated avatar as a base64-encoded SVG data URI.

Example Output

echo Avatar::make('Élodie Durand')->svg();

Returns SVG similar to:

<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 100 100" role="img" aria-label="Avatar for Élodie Durand"><rect width="100" height="100" rx="50" fill="#b8cfcc" /><text x="50%" y="50%" dominant-baseline="middle" text-anchor="middle" fill="#ffffff" font-size="40" font-family="Arial, sans-serif">ÉD</text></svg>

Testing

Run the local test suite with:

composer test

License

MIT