0) { $value .= str_repeat('=', 4 - $pad); } $decoded = base64_decode($value, true); return $decoded === false ? null : $decoded; } /** * Locate a cached image by key (sha1 of source URL or "book-"/"calendar-"). * Checks public first, then the storage fallback. Returns an absolute path or null. */ function findImageCacheFile($key, array $exts = ['jpg', 'jpeg', 'png', 'webp', 'gif']) { $key = basename((string) $key); if ($key === '') { return null; } foreach ([imageCacheDir(), imageStorageCacheDir()] as $dir) { foreach ($exts as $ext) { $file = $dir . '/' . $key . '.' . $ext; if (is_file($file) && @filesize($file) > 0) { return $file; } } } return null; } /** Static URL for a public cached image, or null when it lives in storage/none. */ function publicImageCacheUrl($file) { $dir = imageCacheDir(); if (strpos($file, $dir . '/') !== 0) { return null; } $url = url(imageCacheUrlBase() . '/' . basename($file)); $mtime = @filemtime($file); return $mtime ? $url . '?v=' . $mtime : $url; } /** * Normalise any image reference for use in an . * - Remote URL already mirrored -> local static URL (fast, offline-safe) * - Remote URL not mirrored yet -> /img?src= (server-side cache/proxy) * - Local path/URL -> unchanged (app base path is kept) */ function imageUrl($url, $fallback = 'assets/images/book-placeholder.svg') { $url = trim((string) $url); if ($url === '') { return url($fallback); } if (!isRemoteImageUrl($url)) { if (preg_match('#^(https?:)?//#i', $url) || (isset($url[0]) && $url[0] === '/')) { return $url; } return url($url); } $file = findImageCacheFile(imageCacheKey($url)); if ($file !== null) { $static = publicImageCacheUrl($file); if ($static !== null) { return $static; } } return url('/img?src=' . imageB64UrlEncode($url)); } /** * Stable cover URL for a book. Prefers a mirrored local cover, then a dynamic * /cover/book- endpoint that mirrors on first request, then the generic proxy. */ function bookCoverUrl(array $book) { static $memo = []; $id = preg_replace('/[^A-Za-z0-9_-]/', '', (string) ($book['ID'] ?? '')); $fallback = 'assets/images/book-placeholder.svg'; if ($id !== '') { $name = 'book-' . $id; if (array_key_exists($name, $memo)) { return $memo[$name]; } $file = findImageCacheFile($name); if ($file !== null) { $static = publicImageCacheUrl($file); if ($static !== null) { return $memo[$name] = $static; } } // Not mirrored yet: let /cover fetch + resize it on demand. return $memo[$name] = url('/cover/' . $name); } return imageUrl($book['imageURL'] ?? '', $fallback); } /** Stable cover URL for a calendar event poster. */ function calendarPosterUrl(array $event) { static $memo = []; $id = preg_replace('/[^A-Za-z0-9_-]/', '', (string) ($event['ID'] ?? '')); $fallback = 'assets/images/logo.png'; if ($id !== '') { $name = 'calendar-' . $id; if (array_key_exists($name, $memo)) { return $memo[$name]; } $file = findImageCacheFile($name); if ($file !== null) { $static = publicImageCacheUrl($file); if ($static !== null) { return $memo[$name] = $static; } } return $memo[$name] = url('/cover/' . $name); } return imageUrl($event['poster'] ?? '', $fallback); } /** Absolute image URL for Open Graph / JSON-LD. */ function absoluteImageUrl($url, $fallback = 'assets/images/book-placeholder.svg') { $local = imageUrl($url, $fallback); if (preg_match('#^https?://#i', $local)) { return $local; } return absoluteUrl(ltrim($local, '/')); }