toby/app/Eloquent/Helpers/UserAvatarGenerator.php
Krzysztof Rewak f6d59f8bfb
- directory refactor (#32)
* - directory refactor

* - readme.md update

* Update readme.md

Co-authored-by: Adrian Hopek <adrian.hopek@blumilk.pl>

* Update readme.md

Co-authored-by: Ewelina Lasowy <56546832+EwelinaLasowy@users.noreply.github.com>

* Update readme.md

Co-authored-by: Ewelina Lasowy <56546832+EwelinaLasowy@users.noreply.github.com>

* Update readme.md

Co-authored-by: Ewelina Lasowy <56546832+EwelinaLasowy@users.noreply.github.com>

* Update readme.md

Co-authored-by: Adrian Hopek <adrian.hopek@blumilk.pl>
Co-authored-by: Ewelina Lasowy <56546832+EwelinaLasowy@users.noreply.github.com>
2022-01-26 12:31:26 +01:00

51 lines
1.1 KiB
PHP

<?php
declare(strict_types=1);
namespace Toby\Eloquent\Helpers;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use LasseRafn\InitialAvatarGenerator\InitialAvatar;
use SVG\SVG;
use Toby\Eloquent\Models\User;
class UserAvatarGenerator
{
public function __construct(
protected InitialAvatar $generator,
) {
}
public function generateFor(User $user): string
{
$path = "avatars/{$this->generateUuid()}.svg";
Storage::put($path, $this->generate($user));
return $path;
}
protected function generate(User $user): SVG
{
return $this->generator->rounded()
->background($this->getColor($user->fullName))
->color("#F4F8FD")
->smooth()
->fontSize(0.33)
->generateSvg($user->fullName);
}
protected function getColor(string $name): string
{
$colors = config("colors");
return $colors[strlen($name) % count($colors)];
}
protected function generateUuid(): string
{
return Str::uuid()->toString();
}
}