PHP code example of danjamesmills / initials-avatar-generator

1. Go to this page and download the library: Download danjamesmills/initials-avatar-generator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

danjamesmills / initials-avatar-generator example snippets


php artisan vendor:publish --provider="DanJamesMills\InitialsAvatarGenerator\InitialsAvatarGeneratorServiceProvider"

php artisan migrate

'storage_path' => storage_path('app/public/avatars/'),

mkdir storage/app/public/avatars

php artisan storage:link

use Illuminate\Foundation\Auth\User as Authenticatable;
use DanJamesMills\InitialsAvatarGenerator\Traits\HasAvatar;

class User extends Authenticatable
{
    use HasAvatar;

    // ...
}

use Illuminate\Foundation\Auth\User as Authenticatable;
use DanJamesMills\InitialsAvatarGenerator\Traits\HasAvatar;
use Illuminate\Database\Eloquent\Casts\Attribute;

class User extends Authenticatable
{
    /**
     * Get the user's avatar.
     *
     * @return \Illuminate\Database\Eloquent\Casts\Attribute
     */
    protected function avatar(): Attribute
    {
        return Attribute::make(
            get: fn ($value) => url("/storage/avatars/{$value}"),
        );
    }
}

use Illuminate\Foundation\Auth\User as Authenticatable;
use DanJamesMills\InitialsAvatarGenerator\Traits\HasAvatar;

class User extends Authenticatable
{
    /**
     * Used to define the fields of which initials avatar 
     * generator will create initials from.
     *
     * @return string
     */
    protected function defineNameInitialsAvatarGenerator(): string
    {
        return $this->name;
    }
}

use Illuminate\Foundation\Auth\User as Authenticatable;
use DanJamesMills\InitialsAvatarGenerator\Traits\HasAvatar;

class User extends Authenticatable
{
    /**
     * Used to define the column field name of which 
     * you want to save avatar image to.
     *
     * @return string
     */
    protected function defineAvatarColumnName(): string
    {
        return 'avatarColumnName';
    }
}

$user = User::findOrFail(4);

$user->generateAvatarAndSet();
$user->save();

return "<img src='{$user->avatar}' width='250px' />";

$user = User::findOrFail(4);

$user->name = 'John Doe'; // changed from Dan Doe
$user->save();

return "<img src='{$user->avatar}' width='250px' />";