PHP code example of joke-php / avatar

1. Go to this page and download the library: Download joke-php/avatar 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/ */

    

joke-php / avatar example snippets


use Vasoft\JokeAvatar\AvatarProvider;

$config->addProvider(AvatarProvider::class);

use Vasoft\JokeAvatar\AvatarConfig;

return [
    AvatarConfig::class => static function () use ($env): AvatarConfig {
        return (new AvatarConfig())
            ->setSize(200)
            ->setThemes([
                'default' => [
                    'background_colors'    => ['#3498db', '#2980b9', '#1abc9c', '#16a085'],
                    'text_color'           => '#ffffff',
                    'decorative_colors'    => ['#2ecc71', '#e74c3c', '#f39c12', '#9b59b6'],
                    'decorative_min'       => 3,
                    'decorative_max'       => 6,
                    'decorative_size_min'  => 20,
                    'decorative_size_max'  => 60,
                ],
                // ... другие темы
            ]);
    },
];

use Vasoft\JokeAvatar\AvatarGenerator;
use Vasoft\JokeAvatar\Http\Response\ImageResponse;

class AvatarController
{
    public function __construct(
        private readonly AvatarGenerator $generator,
    ) {}

    public function __invoke(string $nickname): ImageResponse
    {
        $path = $this->generator->generate($nickname, 'dark');
        return (new ImageResponse())->load($path);
    }
}

$router->get('/avatar/{nickname}', AvatarController::class, 'avatar.generate');
bash
composer 
nginx
server {
    listen 80;
    server_name avatar.example.com;

    location / {
        proxy_pass http://localhost:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}