* #2 - wip * #2 - wip * #2 - ui fixes to login page * #2 - fix * #2 - fix * #4- wip * #4 - wip * #4 - wip * #4 - wip * #4- wip * #4 - wip * #4 - wip * #4 - tests * #4 - ecs fix * #4 - fix * #4 - wip * #4 - fix * #4 - fix * #4 - fix composer * #4 - cr fix Co-authored-by: EwelinaLasowy <ewelina.lasowy@blumilk.pl>
This commit is contained in:
50
app/Helpers/UserAvatarGenerator.php
Normal file
50
app/Helpers/UserAvatarGenerator.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Toby\Helpers;
|
||||
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
use LasseRafn\InitialAvatarGenerator\InitialAvatar;
|
||||
use SVG\SVG;
|
||||
use Toby\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->name))
|
||||
->color("#F4F8FD")
|
||||
->smooth()
|
||||
->fontSize(0.33)
|
||||
->generateSvg($user->name);
|
||||
}
|
||||
|
||||
protected function getColor(string $name): string
|
||||
{
|
||||
$colors = config("colors");
|
||||
|
||||
return $colors[strlen($name) % count($colors)];
|
||||
}
|
||||
|
||||
protected function generateUuid(): string
|
||||
{
|
||||
return Str::uuid()->toString();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user