Radiance: Generate Deterministic Mesh Gradient Avatars in PHP
In the world of web development, creating visually appealing user profiles and placeholders is essential for enhancing user experience. Radiance, a PHP library developed by Tomás López, provides an innovative solution for generating unique, deterministic mesh gradient avatars from any input string. This library is especially useful for developers looking to create consistent and recognizable user identities across applications.
What is Radiance?
Radiance is a PHP library that allows developers to generate avatars based on a given seed input. The key feature of Radiance is its deterministic nature; it ensures that the same input will always produce the same avatar. This is particularly beneficial for user profiles, identicons, and placeholder images, as it maintains consistency across different sessions and platforms.
To utilize Radiance, you need to have PHP version 8.2 or higher. The library can be easily installed using Composer, a dependency manager for PHP. The installation command is:
composer require tomloprod/radianceGetting Started with Radiance
Radiance employs a fluent API for configuration, making it intuitive for developers to customize avatars. Below is a basic example of how to generate an SVG avatar using Radiance:
radiance()->seed('ericlbarnes')->text('EB')->baseColor('#f9332b')->toSvg();This example demonstrates how to create an avatar by specifying a seed, text, and base color. The output will be an SVG image that can be displayed on a web page.
Customizing Avatars
Radiance offers a wide range of customization options to tailor avatars to specific needs. Here are some of the key features that developers can utilize:
- Shape and Size: Control the dimensions of the avatar and choose between different shapes such as circles, squares, or squircles (rounded squares).
- Colors: Set a base color using
baseColor()or opt for a solid color withsolidColor()for non-gradient outputs. - Filters: Adjust visual properties like saturation, contrast, and fade distance to enhance the avatar’s appearance.
- Text Customization: Modify text overlays with options for font family, font size ratio, and text shadow effects.
- Pixel Patterns: Add identicon-style overlays with configurations for pixel grid size, opacity, density, and shape options.
Advanced Usage Examples
To illustrate the capabilities of Radiance, here are a couple of advanced examples that showcase its flexibility:
Example 1: Creating a Custom Avatar
radiance()->seed('predmond@example.com')
->text('PR')
->size(128)
->baseColor('#f9332b')
->squircle()
->saturation(1.6)
->textShadow(2)
->toBase64();This example generates a squircle avatar with a specified size, base color, and text shadow effect. The output is encoded in Base64, making it easy to embed directly in HTML.
Example 2: Generating Multiple Avatars
Developers can also create multiple avatars programmatically. For instance, if you want to generate avatars for a list of users, you could use a loop:
$users = ['alice@example.com', 'bob@example.com', 'charlie@example.com'];
foreach ($users as $user) {
echo radiance()->seed($user)
->text(substr($user, 0, 2))
->baseColor('#3498db')
->toSvg();
}This code snippet generates SVG avatars for each user in the array, utilizing the first two letters of their email addresses as text overlays.
Integration with Other Libraries
Radiance is built on top of Colority, another library by Tomás López that focuses on color manipulation. This integration allows developers to leverage advanced color features while generating avatars. By utilizing Colority, Radiance can create visually appealing gradients and color effects that enhance the overall design of the avatars.
Interactive Playground
To explore the features of Radiance further, developers can visit the interactive playground available at radiance.tomloprod.dev. This platform allows users to experiment with various configurations and instantly see the resulting avatars. It serves as an excellent resource for understanding the library’s capabilities and finding inspiration for avatar designs.
Source Code and Documentation
The source code for Radiance is available on GitHub, providing transparency and opportunities for contributions. Developers can access the repository at GitHub – Radiance. Comprehensive documentation is also provided, detailing all available methods, parameters, and examples to assist developers in utilizing the library effectively.
Use Cases for Radiance
Radiance can be applied in various scenarios, making it a versatile tool for developers. Here are some common use cases:
- User Profiles: Generate unique avatars for user accounts, enhancing personalization and recognition.
- Identicons: Create visually distinct identicons for users without profile pictures, ensuring a consistent look across platforms.
- Placeholder Images: Use Radiance to generate placeholder images during the loading of user-generated content.
- Branding: Customize avatars to reflect brand colors and styles, reinforcing brand identity.
Performance Considerations
When implementing Radiance in applications, it is essential to consider performance. Generating avatars dynamically can introduce overhead, especially if done frequently or for large user bases. To mitigate performance issues:
- Cache Avatars: Store generated avatars in a cache to minimize repeated computations.
- Use SVGs: SVG images are lightweight and can be easily scaled without loss of quality, making them ideal for avatars.
- Batch Processing: Generate multiple avatars in a single request to reduce server load.
Frequently Asked Questions
Radiance is a PHP library that generates unique, deterministic mesh gradient avatars based on a given seed input. It uses a fluent API for configuration, allowing developers to customize the appearance of avatars easily.
Radiance can be installed using Composer by running the command composer require tomloprod/radiance. Ensure that your PHP version is 8.2 or higher before installation.
Radiance offers various customization options, including shape and size, colors, text customization, filters, and pixel patterns. Developers can tailor avatars to meet specific design requirements effectively.
Call To Action
Are you ready to enhance your web applications with unique and customizable avatars? Explore Radiance today and integrate it into your projects for a better user experience.
Note: Radiance is a powerful tool for generating avatars that can enhance user engagement and personalization in web applications. By utilizing its features, developers can create visually appealing and consistent user identities.

