Skip to main content

API reference

A summary of the public classes. All live under Renfordt\AvatarSmithy.

Avatar​

Static factory.

MethodReturnsDescription
Avatar::engine(string $engine)AvatarBuilderStart a builder with a named primary engine.
Avatar::for(mixed $user)AvatarBuilderStart a builder, pre-filling seed/name from a user.

for() reads email/name properties or getEmail()/getName() methods on an object (methods win when both exist), or email/name keys on an array. The email is used as the seed.

AvatarBuilder​

Fluent configuration and generation. Constructor: __construct(?string $engine = null, LoggerInterface $logger = new NullLogger()).

Selected methods:

MethodReturnsDescription
try(string)selfSet primary engine, or append a fallback.
fallbackTo(string)selfAppend a fallback engine.
seed(string)selfSet the seed.
name(string)selfSet the display name.
size(int)selfSet output size (8–2048).
setLogger(LoggerInterface)selfSet a PSR-3 logger.
debugMode(bool)selfRethrow the first engine failure.
getLastErrors()arrayFailures from the last generate() call.
generate()GeneratedAvatarRun the engine chain.

Option setters are listed in The builder.

GeneratedAvatar​

Implements Stringable. Constructor: __construct(string $content, string $contentType = 'image/svg+xml', ?string $name = null, int $size = 200).

MethodReturnsDescription
alt(string)selfSet alt text.
toHtml()string<img> tag.
accessibleHtml()string<img> with role/aria-label.
toBase64()stringdata: URI.
toUrl()stringRemote URL, else data URI.
toSvg()stringRaw content.
toPng(?int, ?int, int)GeneratedAvatarPNG conversion.
toJpg(?int, ?int, int)GeneratedAvatarJPG conversion.
toWebp(?int, ?int, int)GeneratedAvatarWebP conversion.
save(string)boolWrite content to a path.
getContent()stringRaw content.
getContentType()stringMIME type.
__toString()stringRaw content.

Http\AvatarResponder​

final readonly. Constructor takes a GeneratedAvatar.

MethodReturnsDescription
toResponse()mixedSend as an HTTP response.
download(string $filename)mixedSend as an attachment.
stream(int $chunkSize, bool $headers)voidWrite the body in chunks.

Support\Name​

Helper used by local engines. Name::make(string) constructs it.

MethodReturnsDescription
getInitials()stringUppercase, UTF-8-aware initials.
getHexColor(int $offset=0)HexColorColor derived from the MD5 hash of the name.
getName()stringThe original name.
getHash()stringMD5 hash of the name.

Converters\ImageConverter​

Static conversion helpers used by GeneratedAvatar. See Image conversion.

Engines\EngineInterface​

Contract for engines. See Custom engines.