adhir2099 / initials-avatar
Zero-dependency PHP 8.2+ initials avatar generator supporting SVG and GD PNG rendering, deterministic color palettes, WCAG auto-contrast, and full UTF-8 support.
v1.0.0
2026-11-22 17:47 UTC
Requires
- php: >=8.2
- ext-mbstring: *
Requires (Dev)
None
Suggests
- ext-gd: Required for raster PNG avatar generation (toPng / save .png); SVG generation works without ext-gd.
Provides
None
Conflicts
None
Replaces
None
README
A zero-dependency, framework-agnostic PHP library for generating deterministic SVG, PNG (GD), and Base64 Data URI initial avatars from names, usernames, or email addresses.
Extracted and modernized from adhir2099/php-avatar-registration into a standalone PSR-4 Composer package.
✨ Features
- Zero External Dependencies — Runs on pure PHP
>= 8.2withext-mbstring(plus optionalext-gdfor raster PNG output). - Dual Rendering Engines —
- SVG Engine (
toSvg()): Crisp, infinite-resolution vector avatars with<circle>,<rect rx="...">(rounded), or<rect>(square) shapes. - GD Raster Engine (
toPng()): TrueColor PNG generation with alpha-channel transparency, custom TrueType (.ttf) bounding-box centering (imagettfbbox), and automatic built-in bitmap glyph fallback when no.ttffile is provided.
- SVG Engine (
- Deterministic Color Palettes — Same user name or email always produces the same background color (
vibrant,pastel,dark,retro), or generate random bounded RGB colors (randomBackground()). - Automatic WCAG Contrast — Automatically selects dark slate (
#1e293b) or pure white (#ffffff) text based on background relative luminance. - Unicode & Email Ready — Full multi-byte UTF-8 support (
Érika Núñez→ÉN) and automatic email local-part parsing (adhir.serrano@example.com→AS).
Installation
composer require adhir2099/initials-avatar
Quick Start
1. Fluent SVG & Data URI Generation
use Adhir2099\InitialsAvatar\AvatarGenerator; // Render inline SVG markup $svg = AvatarGenerator::make('Adhir Serrano') ->size(128) ->shape('circle') // 'circle' | 'rounded' | 'square' ->palette('vibrant') // 'vibrant' | 'pastel' | 'dark' | 'retro' ->border(4, '#ffffff') ->toSvg(); // Or embed directly in an HTML <img> tag as a Base64 Data URI: $dataUri = AvatarGenerator::make('Érika Núñez') ->palette('pastel') ->toDataUri('svg'); // data:image/svg+xml;base64,...
2. Raster PNG Generation & Saving to Disk (GD)
use Adhir2099\InitialsAvatar\AvatarGenerator; $avatar = AvatarGenerator::make('Adhir 2099') ->size(256) ->shape('rounded') ->background('#0e75b6') ->color('#ffffff'); $avatar->save(__DIR__ . '/storage/avatars/user_42.png'); $avatar->save(__DIR__ . '/storage/avatars/user_42.svg');
API Reference
| Method | Description | Default |
|---|---|---|
AvatarGenerator::make(string $name) |
Instantiate a new generator for a name, username, or email | '' |
->size(int $pixels) |
Canvas width & height in pixels (16–2048) |
128 |
->length(int $chars) |
Number of initials to extract (1–4) |
2 |
->uppercase(bool $upper) |
Uppercase extracted UTF-8 initials | true |
->shape(string $shape) |
'circle', 'rounded', or 'square' |
'circle' |
->palette(string $name) |
Deterministic palette: 'vibrant', 'pastel', 'dark', 'retro' |
'vibrant' |
->background(string $hex) |
Override background color with #rrggbb or #rgb |
Auto from seed |
->randomBackground(int $min, int $max) |
Random RGB color with channel bounds (50–200) |
— |
->color(string $hex) |
Override text color (otherwise auto-contrasted via WCAG luminance) | Auto contrast |
->fontSize(float $scale) |
Font size ratio relative to avatar size (0.1–0.9) |
0.44 |
->font(?string $ttfPath) |
Path to .ttf font for GD toPng() rendering |
null (built-in fallback) |
->border(int $width, string $hex) |
Add an inset border around the avatar | 0, '#ffffff' |
->toSvg() |
Returns raw <svg>...</svg> string |
— |
->toPng() |
Returns raw PNG binary string (ext-gd) |
— |
->toDataUri(string $format) |
Returns data:image/svg+xml;base64,... or data:image/png;base64,... |
'svg' |
->save(string $path, ?string $format) |
Writes .svg or .png file to disk |
Auto from extension |
Running Tests & Examples
# Generate PSR-4 autoload files composer dump-autoload # Run the test suite composer test # Generate sample SVG & PNG avatars into examples/output/ php examples/generate_showcase.php
License
Released under the MIT License. Created by adhir2099