This commit is contained in:
39
public/page/api/avatar.php
Normal file
39
public/page/api/avatar.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?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;
|
||||
Reference in New Issue
Block a user