diff --git a/button.php b/button.php new file mode 100644 index 0000000..fbe0ac7 --- /dev/null +++ b/button.php @@ -0,0 +1,22 @@ + +echo substr($raw, $nl + 1); diff --git a/inc/buttons.php b/inc/buttons.php index 9ca190c..8f3bc9e 100644 --- a/inc/buttons.php +++ b/inc/buttons.php @@ -20,10 +20,10 @@ const BUTTONS_88x31 = [ const BUTTON_CACHE_TTL = 7 * 86400; // re-fetch each button at most once a week /** - * Fetch a remote image, cache it locally, and return it as a base64 data: URI. - * Returns null on failure so the caller can fall back to hotlinking. + * Fetch a remote image into the local cache (at most once a week) and return + * the cache file's path, or null if it couldn't be fetched. */ -function button_data_uri(string $url): ?string +function button_cache(string $url): ?string { $dir = DATA_DIR . '/buttons'; @mkdir($dir, 0775, true); @@ -47,19 +47,19 @@ function button_data_uri(string $url): ?string } } - if (!is_file($file)) { - return null; - } + return is_file($file) ? $file : null; +} - $raw = (string) file_get_contents($file); - $nl = strpos($raw, "\n"); - if ($nl === false) { - return null; - } - - $type = substr($raw, 0, $nl); - $data = substr($raw, $nl + 1); - return 'data:' . $type . ';base64,' . base64_encode($data); +/** + * Where the page should load a button from: our own copy (button.php serves + * it, versioned by the cache file's mtime so browsers can keep it), or the + * original if fetching failed. Was inlined as base64 before, which made every + * page ~260 KB heavier. + */ +function button_src(string $url): string +{ + $file = button_cache($url); + return $file ? '/button.php?h=' . basename($file) . '&v=' . filemtime($file) : $url; } /** Render the whole 88x31 button row as HTML. */ @@ -67,7 +67,7 @@ function render_buttons_88x31(): string { $html = ''; foreach (BUTTONS_88x31 as $b) { - $uri = button_data_uri($b['src']) ?? $b['src']; // fall back to hotlink if fetch failed + $uri = button_src($b['src']); $img = '' . htmlspecialchars($b['alt']) . ''; $href = $b['href'] ?? ''; if ($href !== '') { diff --git a/inc/header.php b/inc/header.php index 69053e9..783a275 100644 --- a/inc/header.php +++ b/inc/header.php @@ -9,6 +9,7 @@ require_once __DIR__ . '/../config.php'; <?= htmlspecialchars($page_title ?? SITE_TITLE) ?> +