a6digital/laravel-default-profile-image
Generate default avatar/profile images in Laravel from a user’s name (initials). Customize size, background and text colors, and optional custom font, then save via Storage or return an encoded image for immediate use.
Installation:
composer require a6digital/laravel-default-profile-image
Register the service provider in config/app.php:
'providers' => [
A6digital\Image\DefaultProfileImageServiceProvider::class,
],
First Use Case: Generate a default avatar for a user in a controller or model:
use DefaultProfileImage;
$userName = "John Doe";
$img = DefaultProfileImage::create($userName);
$path = "avatars/{$userName}.png";
Storage::put($path, $img->encode());
This creates a 512x512px PNG with black background and white initials ("JD").
Where to Look First:
Add a method to generate and cache avatars:
// app/Models/User.php
public function getDefaultAvatarPath()
{
$path = "avatars/{$this->id}.png";
if (!Storage::exists($path)) {
$img = DefaultProfileImage::create($this->name);
Storage::put($path, $img->encode());
}
return $path;
}
Use Laravel’s Storage facade to generate URLs:
// In a controller or Blade template
$avatarUrl = Storage::url($user->getDefaultAvatarPath());
Extend the package by binding a custom class in the service provider:
// app/Providers/AppServiceProvider.php
public function register()
{
$this->app->bind('DefaultProfileImage', function ($app) {
return new App\Services\CustomProfileImage();
});
}
Use Laravel queues for async generation (e.g., during user import):
// app/Jobs/GenerateDefaultAvatars.php
public function handle()
{
$users = User::whereNull('avatar_path')->limit(100)->get();
foreach ($users as $user) {
$img = DefaultProfileImage::create($user->name);
Storage::put("avatars/{$user->id}.png", $img->encode());
$user->update(['avatar_path' => "avatars/{$user->id}.png"]);
}
}
filesystems.php config points to a writable disk (e.g., local or s3).hasUploadedAvatar() check to prioritize user-uploaded images.Storage::put() in unit tests to avoid filesystem I/O:
Storage::shouldReceive('put')->once();
Laravel Version Mismatch:
// app/Providers/AppServiceProvider.php
public function boot()
{
if (app()->version() >= '8.0') {
$this->app->alias('DefaultProfileImage', App\Services\Laravel8ProfileImage::class);
}
}
Unicode/Non-Latin Names:
Roboto or Noto Sans). Example:
$img = DefaultProfileImage::create("Привет Мир", 256, '#000', '#FFF', public_path('fonts/NotoSans-Regular.ttf'));
Font Paths:
/var/www/fonts/) or Laravel’s storage_path():
$fontPath = storage_path('fonts/Roboto-Regular.ttf');
Image Size Limits:
ini_set('memory_limit', '256M') temporarily.Storage Permissions:
Storage::put() may fail if the target directory lacks write permissions.'disks' => [
'avatars' => [
'driver' => 'local',
'root' => storage_path('app/avatars'),
'permissions' => [
'file' => [
'public' => true,
'private' => false,
],
],
],
],
#FFF vs. #FFFFFF).if (!file_exists($fontPath)) {
throw new \RuntimeException("Font file not found at {$fontPath}");
}
Custom Initial Logic: Override how initials are extracted:
// app/Services/CustomProfileImage.php
public static function create($name, $size = 512, $bgColor = '#000', $textColor = '#FFF', $fontPath = null)
{
$initials = self::extractInitials($name); // Custom logic
// ... rest of the method
}
protected static function extractInitials($name)
{
$parts = explode(' ', trim($name));
return count($parts) > 1
? strtoupper(substr($parts[0], 0, 1) . substr($parts[count($parts) - 1], 0, 1))
: strtoupper(substr($name, 0, 2));
}
Dynamic Colors: Use a color palette service to rotate colors per user ID:
$palette = app(ColorPalette::class);
$bgColor = $palette->getForUser($user->id);
SVG Output: Extend the package to support SVG (requires custom implementation):
// Pseudocode
$svg = new \App\Services\SvgProfileImage($name, $size);
Storage::put("avatars/{$user->id}.svg", $svg->render());
local with SSD) and cache URLs in Redis.// app/Providers/AppServiceProvider.php
public function boot()
{
DefaultProfileImage::setDefaults(256, '#212121', '#FFFFFF');
}
How can I help you explore Laravel packages today?