Laravel

The freeplaceholder/laravel Composer package provides a Facade, Blade components, Blade directives, helper functions, and an Eloquent trait.

Installation

bash
composer require freeplaceholder/laravel

The service provider is auto-discovered.

Facade

php
use FreePlaceholder\Facades\FreePlaceholder;

$url = FreePlaceholder::url(600, 400, [
    'format' => 'png',
    'bg' => '3b82f6',
    'text-color' => 'ffffff',
    'text' => 'Hero Banner',
    'font-weight' => 'bold',
    'rounded' => 'lg',
    'blur' => 2,
]);

$avatar = FreePlaceholder::avatarUrl('John Doe', [
    'size' => 256,
    'format' => 'png',
    'grayscale' => true,
    'border' => 3,
    'border-color' => 'ffffff',
]);

Blade Components

Use the <x-placeholder> and <x-avatar> components in any Blade template:

blade
<x-placeholder
  :width="800"
  :height="400"
  text="Banner"
  format="webp"
  bg="3b82f6"
  text-color="ffffff"
  font-weight="bold"
  rounded="lg"
/>

<x-avatar
  name="John Doe"
  :size="64"
  format="png"
  font-weight="bold"
  :border="2"
  border-color="ffffff"
  grayscale
/>

Placeholder Attributes

AttributeTypeDefaultDescription
widthint300Image width
heightint300Image height
formatstringsvgOutput format
bgstringBackground hex color
gradientstringto-t, to-tr, to-r, to-br, to-b, to-bl, to-l, to-tl, radial
fromstringGradient start color — hex without #
viastringGradient middle color — hex without #
tostringGradient end color — hex without #
text-colorstringText hex color
textstringCustom text
text-sizeintFont size in px
text-alignstringcenterleft, center, right
text-transformstringnoneuppercase, lowercase, capitalize
text-decorationstringnoneunderline, overline, line-through
letter-spacingstringtighter, tight, wide, wider, widest
font-weightstringFont weight
opacityint1000–100
blurint0Gaussian blur 0–100 px
brightnessint1000–200
contrastint1000–200
hue-rotateint00–360 degrees
invertboolfalseInvert colors
saturateint1000–200
sepiaboolfalseSepia tone
grayscaleboolfalseGrayscale filter
borderint0Border width in px
border-colorstringBorder hex color
border-stylestringsolidsolid, dashed, dotted, double, none
roundedstring0sm, md, lg, xl, 2xl, 3xl, full, or px
lazybooltrueEnable lazy loading

Avatar Attributes

AttributeTypeDefaultDescription
namestringName to derive initials from (required)
sizeint128Square dimensions in px
formatstringsvgOutput format
bgstringBackground hex color
gradientstringto-t, to-tr, to-r, to-br, to-b, to-bl, to-l, to-tl, radial
fromstringGradient start color — hex without #
viastringGradient middle color — hex without #
tostringGradient end color — hex without #
text-colorstringText hex color
text-decorationstringnoneunderline, overline, line-through
letter-spacingstringtighter, tight, wide, wider, widest
font-weightstringFont weight
opacityint1000–100
blurint0Gaussian blur 0–100 px
brightnessint1000–200
contrastint1000–200
hue-rotateint00–360 degrees
invertboolfalseInvert colors
saturateint1000–200
sepiaboolfalseSepia tone
grayscaleboolfalseGrayscale filter
borderint0Border width in px
border-colorstringBorder hex color
border-stylestringsolidsolid, dashed, dotted, double, none
roundedstring0sm, md, lg, xl, 2xl, 3xl, full, or px
lazybooltrueEnable lazy loading

Blade Directives

blade
@placeholder(width: 600, height: 400, format: 'png', bg: '3b82f6', textColor: 'ffffff', text: 'Banner')
@avatar(name: 'John Doe', size: 64, format: 'png')

Helper Functions

php
$url = placeholder_url(600, 400, ['format' => 'png', 'bg' => '3b82f6']);
$avatar = avatar_url('John Doe', ['size' => 64, 'format' => 'png', 'sepia' => true]);

Eloquent Trait

Add the HasPlaceholder trait to any model to get $model->placeholder and $model->avatar accessors:

php
use FreePlaceholder\Traits\HasPlaceholder;

class User extends Model
{
    use HasPlaceholder;

    // The attribute used for avatar text (default: 'name')
    protected function placeholderTextField(): string
    {
        return 'name';
    }

    // Default options for all placeholder URLs from this model
    protected function placeholderOptions(): array
    {
        return [
            'format' => 'png',
            'font-weight' => 'bold',
            'grayscale' => false,
        ];
    }
}

Usage:

php
$user = User::find(1);

// Uses the user's name for initials and color hashing
$user->avatar;         // https://freeplaceholder.com/avatar/John+Doe.png?font-weight=bold
$user->placeholder;    // https://freeplaceholder.com/300x300.png?text=John+Doe&font-weight=bold

// Override options
$user->avatarUrl(['size' => 256, 'border' => 3, 'border-color' => 'ffffff']);
$user->placeholderUrl(['width' => 600, 'height' => 400, 'blur' => 2]);
blade
{{-- In Blade templates --}}
<img src="{{ $user->avatar }}" alt="{{ $user->name }}" class="rounded-full" />

Overriding Base URL

Publish the config file and update the base URL:

bash
php artisan vendor:publish --tag=freeplaceholder-config

Then update config/freeplaceholder.php:

php
return [
    'base_url' => 'https://my-custom-instance.com',
];