Introduction
Creating user avatars dynamically is a great way to personalize your application without forcing users to upload profile images. In this post, we'll use ui-avatars.com with Laravel to generate avatars based on a user's name.
Why use dynamic avatars?
Dynamic avatars help provide a default identity for users who haven’t uploaded a photo. Services like ui-avatars.com
generate simple, color-based avatars with initials, which look professional and require zero setup.
How to use ui-avatars in Laravel
01: The Base URL
The base API looks like this:
https://ui-avatars.com/api/?name=John+Doe
This will generate an image with "JD" centered on a colored background.
02: Laravel Blade Example
You can use Laravel Blade to dynamically generate the avatar like so:
<img src="https://ui-avatars.com/api/?name={{ urlencode($user->name) }}&background=random&color=fff" alt="Avatar" class="rounded-full w-12 h-12">
This will generate a circular avatar using the user's name with a random background and white text.
03: Customize Colors and Size
You can pass more parameters to control how the avatar looks:
https://ui-avatars.com/api/?name=John+Doe
&background=0D8ABC
&color=fff
&size=128
04: Use in Loops (e.g., list of users)
If you're listing multiple users, you can use it like this:
@foreach ($users as $user)
<img src="https://ui-avatars.com/api/?name={{ urlencode($user->name) }}&background=random&color=fff"
class="w-10 h-10 rounded-full" alt="{{ $user->name }}">
@endforeach
Note: ui-avatars.com is a public API and does not require any API key. However, do not abuse it with rapid, repeated requests. For production systems, you can cache avatar URLs or download the images.
Conclusion
Generating dynamic avatars using ui-avatars.com is a clean and efficient way to handle default profile images in Laravel. It's fast, free, and requires zero backend logic beyond generating the name-based URL.