All checks were successful
Deploy / deploy (push) Successful in 1m43s
40 lines
1.1 KiB
PHP
40 lines
1.1 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
use App\Avatar;
|
|
|
|
$seed = Avatar::normalizeSeed((string)($_GET['seed'] ?? 'papa-kind-treff'));
|
|
$cacheDir = dirname(__DIR__, 3) . '/assets/avatars/dicebear-cache';
|
|
|
|
if (!is_dir($cacheDir)) {
|
|
@mkdir($cacheDir, 0775, true);
|
|
}
|
|
|
|
$cacheFile = $cacheDir . '/' . $seed . '.svg';
|
|
$svg = is_file($cacheFile) ? file_get_contents($cacheFile) : false;
|
|
|
|
if ($svg === false) {
|
|
$url = 'https://api.dicebear.com/10.x/lorelei/svg?seed=' . rawurlencode($seed) . '&size=256';
|
|
$context = stream_context_create([
|
|
'http' => [
|
|
'method' => 'GET',
|
|
'timeout' => 6,
|
|
'header' => "User-Agent: Papa-Kind-Treff/1.0\r\nAccept: image/svg+xml\r\n",
|
|
],
|
|
]);
|
|
|
|
$remote = @file_get_contents($url, false, $context);
|
|
if (is_string($remote) && str_contains($remote, '<svg')) {
|
|
$svg = $remote;
|
|
@file_put_contents($cacheFile, $svg);
|
|
}
|
|
}
|
|
|
|
if (!is_string($svg) || $svg === '') {
|
|
$svg = Avatar::fallbackSvg($seed, 'Avatar');
|
|
}
|
|
|
|
header('Content-Type: image/svg+xml; charset=utf-8');
|
|
header('Cache-Control: public, max-age=604800, immutable');
|
|
echo $svg;
|