From 40e80d4fdaccbbe174d351e517633489a0582d36 Mon Sep 17 00:00:00 2001 From: XIGE <710062962@qq.com> Date: Thu, 3 Dec 2026 22:40:32 +0800 Subject: [PATCH] =?UTF-8?q?2.9.2=EF=BC=9A=E7=94=BB=E5=BB=8A=E5=A4=A7?= =?UTF-8?q?=E5=9B=BE=E7=81=AF=E7=AE=B1=E9=A1=B6=E9=83=A8=E6=98=BE=E7=A4=BA?= =?UTF-8?q?=20EXIF=EF=BC=88=E7=BC=A9=E7=95=A5=E5=9B=BE=E4=BB=8D=E4=B8=8D?= =?UTF-8?q?=E6=98=BE=E7=A4=BA=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 4 + Plugin.php | 319 +++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 316 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bcf3c08..b130644 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ > 本文件记录本插件的所有修改与变化。仓库建立于 2026-12-03,此前历史见插件目录内的 `更新日志.MD`。 +## 2026-12-03 · 2.9.2:画廊大图灯箱顶部显示 EXIF(缩略图仍不显示) + +- 同步源码更新 1 个文件(新增/修改 1,删除 0)。 + ## 插件信息 - 名称:七牛云存储 diff --git a/Plugin.php b/Plugin.php index a44d391..55acd86 100644 --- a/Plugin.php +++ b/Plugin.php @@ -4,12 +4,20 @@ * * @package Qiniu * @author 石头厝 - * @version 2.9.1 + * @version 2.9.2 * @link https://www.shitoucuo.com/ * @since 1.2.0 */ /** + * 说明(2.9.2 画廊大图 EXIF): + * 1. `{flex}` 横排里不注入包裹层(否则破版),但**照样把 EXIF 收进页内数据表**, + * 由页脚一段脚本在灯箱打开大图时把信息贴到图片顶部 —— 缩略图不显示、大图显示。 + * 2. 页内数据表(path → exif)只在文章详情页输出一次,键为 URL 解码后的对象键, + * 前端用同样的解码规则比对,避免中文文件名对不上。 + * 3. 灯箱适配不绑定具体实现:ViewImage(主题自带)与 Fancybox/PhotoSwipe 的 + * 大图容器都认,谁开图都能贴。 + * * 说明(2.9.1 EXIF 与画廊/正文结构兼容): * 1. 包裹层 div → span。markdown 输出的图片是 `

`,在 `

` 里插 `

` * 会被解析器提前闭合,主题 `.markdown-body p` 的 10px 下边距于是变成空行。 @@ -49,7 +57,7 @@ class Qiniu_Plugin implements Typecho_Plugin_Interface /** * 插件版本 */ - const VERSION = '2.9.1'; + const VERSION = '2.9.2'; /** 文件名前缀最大字符数(多字节按 1 个字符计) */ const PREFIX_MAX_LEN = 40; @@ -84,6 +92,15 @@ class Qiniu_Plugin implements Typecho_Plugin_Interface */ private static $_sdkLoaded = false; + /** + * 本页图片的 EXIF 数据表(对象键 → exif 数组) + * + * 画廊里的图片不注入包裹层,但仍要收进来,供灯箱大图显示。 + * + * @var array + */ + private static $_exifMap = array(); + // 激活插件 public static function activate() { @@ -1431,9 +1448,208 @@ CSS; } echo ''; + + // 数据表 + 灯箱补丁:正文渲染时收集到的 EXIF 在这时候已经齐了 + if (!empty(self::$_exifMap)) { + echo self::exifMapScript(); + echo self::lightboxScript(); + } + self::$_cssLoaded = true; } + /** + * 页内 EXIF 数据表 + * + * 用 script[type=application/json] 而不是 JS 字面量:不参与脚本执行, + * 中文与引号只需 JSON 转义一次;JSON_HEX_TAG 顺手把 `<` 转成 \u003C,避免提前闭合标签。 + * + * @return string + */ + private static function exifMapScript() + { + $map = array(); + + foreach (self::$_exifMap as $path => $exif) { + $item = array(); + foreach (array('camera', 'exposure', 'aperture', 'iso', 'focal_length') as $key) { + if (!empty($exif[$key])) { + $item[$key] = (string) $exif[$key]; + } + } + + if (!empty($item)) { + $map[$path] = $item; + } + } + + if (empty($map)) { + return ''; + } + + $json = json_encode($map, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_HEX_TAG); + if (false === $json) { + return ''; + } + + return ''; + } + + /** + * 灯箱大图 EXIF 补丁 + * + * 缩略图上的浮层靠 hover,大图上是整屏没有 hover 可言,所以改成跟灯箱走: + * 监听 DOM 变化,找到「正在显示的大图」,按 src 反查数据表,把一条信息条 + * 贴到图片顶边。条子 pointer-events:none,不挡上一张/下一张与关闭。 + * + * @return string + */ + private static function lightboxScript() + { + $script = <<<'JS' +(function () { + var node = document.getElementById('qiniu-exif-map'); + if (!node) { return; } + + var MAP = {}; + try { MAP = JSON.parse(node.textContent || '{}') || {}; } catch (e) { return; } + if (!MAP || typeof MAP !== 'object') { return; } + + /* 各灯箱的「大图」容器;主题用的是 ViewImage,另两种是常见备选 */ + var BIG = '.view-image-lead img, .fancybox-slide--image img, .pswp__img'; + + var bar = null; + var shown = ''; + + function esc(text) { + var d = document.createElement('div'); + d.appendChild(document.createTextNode(String(text))); + return d.innerHTML; + } + + function keyOf(src) { + if (!src) { return ''; } + var path = String(src); + var proto = path.indexOf('//'); + if (proto > -1) { + path = path.slice(proto + 2); + var slash = path.indexOf('/'); + path = slash > -1 ? path.slice(slash) : ''; + } + path = path.split('?')[0].split('#')[0]; + try { path = decodeURIComponent(path); } catch (e) {} + return path.replace(/^\/+/, ''); + } + + function lookup(src) { + var path = keyOf(src); + if (!path) { return null; } + if (MAP[path]) { return MAP[path]; } + + /* CDN 前缀 / 代理路径可能让整串对不上,退化为尾部比对 */ + for (var key in MAP) { + if (!Object.prototype.hasOwnProperty.call(MAP, key)) { continue; } + if (key.length <= path.length ? path.slice(-key.length) === key : key.slice(-path.length) === path) { + return MAP[key]; + } + } + return null; + } + + function html(data) { + var out = ''; + if (data.camera) { out += '' + esc(data.camera) + ''; } + + var parts = []; + if (data.exposure) { parts.push('快门 ' + esc(data.exposure)); } + if (data.aperture) { parts.push('光圈 f/' + esc(data.aperture)); } + if (data.iso) { parts.push('ISO ' + esc(data.iso)); } + if (data.focal_length) { parts.push('焦距 ' + esc(data.focal_length) + 'mm'); } + if (parts.length) { out += '' + parts.join(' · ') + ''; } + + return out; + } + + function place(img) { + if (!bar) { return; } + var rect = img.getBoundingClientRect(); + var vw = window.innerWidth || document.documentElement.clientWidth || 0; + var width = Math.min(rect.width, vw - 16); + var left = rect.left + (rect.width - width) / 2; + if (left < 8) { left = Math.min(8, vw - width); } + + bar.style.width = Math.max(width, 0) + 'px'; + bar.style.left = Math.max(left, 0) + 'px'; + bar.style.top = Math.max(rect.top, 0) + 'px'; + } + + function hide() { + shown = ''; + if (bar) { + bar.className = 'qiniu-exif-lightbox'; + bar.innerHTML = ''; + } + } + + function update() { + var img = document.querySelector(BIG); + if (!img) { hide(); return; } + + var data = lookup(img.currentSrc || img.src); + if (!data) { hide(); return; } + + var body = html(data); + if (!body) { hide(); return; } + + if (!bar) { + bar = document.createElement('div'); + bar.className = 'qiniu-exif-lightbox'; + document.body.appendChild(bar); + } + + if (shown !== (img.currentSrc || img.src)) { + shown = img.currentSrc || img.src; + bar.innerHTML = body; + } + + if (bar.className.indexOf('is-on') === -1) { + bar.className = 'qiniu-exif-lightbox is-on'; + } + place(img); + } + + var pending = false; + function schedule() { + if (pending) { return; } + pending = true; + var run = function () { pending = false; update(); }; + if (window.requestAnimationFrame) { window.requestAnimationFrame(run); } else { setTimeout(run, 16); } + } + + if (window.MutationObserver) { + new MutationObserver(schedule).observe(document.body, {childList: true, subtree: true}); + } + + /* 大图是异步插入的(ViewImage 先占位再换 innerHTML),点多几次兜底 */ + document.addEventListener('click', function () { + setTimeout(schedule, 60); + setTimeout(schedule, 320); + setTimeout(schedule, 640); + }, true); + + document.addEventListener('load', function (e) { + if (e.target && e.target.tagName === 'IMG') { schedule(); } + }, true); + + window.addEventListener('resize', schedule); + window.addEventListener('orientationchange', schedule); + schedule(); +})(); +JS; + + return ''; + } + /** * EXIF 展示样式 * @@ -1453,7 +1669,17 @@ CSS; . '.qiniu-exif-info strong{font-weight:600;color:#fff;}' // 兜底:万一包裹层仍落进了 FlexPhoto 的画廊(例如主题/插件改过标记写法), // 缩略图已被 4:3 裁切,浮层意义不大,直接不显示,避免盖住画面。 - . '.flexphoto .qiniu-exif-info{display:none!important;}'; + . '.flexphoto .qiniu-exif-info{display:none!important;}' + // 灯箱大图顶部的信息条:贴在大图顶边,跟随图片尺寸,不挡任何操作 + . '.qiniu-exif-lightbox{position:fixed;top:0;left:0;display:block;z-index:2147483000;' + . 'pointer-events:none;background:rgba(0,0,0,0.78);color:#fff;text-align:left;' + . 'padding:10px 14px;border-radius:0 0 8px 8px;font-size:13px;line-height:1.5;' + . '-webkit-backdrop-filter:blur(6px);backdrop-filter:blur(6px);' + . 'box-shadow:0 4px 16px rgba(0,0,0,.28);opacity:0;visibility:hidden;' + . 'transition:opacity .2s ease,visibility .2s ease;}' + . '.qiniu-exif-lightbox.is-on{opacity:1;visibility:visible;}' + . '.qiniu-exif-lightbox strong{display:block;font-weight:600;color:#fff;}' + . '.qiniu-exif-lightbox span{display:block;margin-top:2px;color:rgba(255,255,255,.86);}'; } /** @@ -1499,8 +1725,14 @@ CSS; } foreach ($parts as $index => $part) { - if ('' === $part - || preg_match('/^(?:\{flex[^}]*\}|]*\bclass="[^"]*flexphoto))/i', $part)) { + if ('' === $part) { + continue; + } + + if (preg_match('/^(?:\{flex[^}]*\}|]*\bclass="[^"]*flexphoto))/i', $part)) { + // 画廊块不注入包裹层,但 EXIF 仍要收进本页数据表: + // 缩略图不显示,点开大图时由页脚脚本贴到图片顶部。 + self::collectExif($part, $domain); continue; } @@ -1534,8 +1766,7 @@ CSS; } // 从URL中提取文件路径 - $path = str_replace($domain, '', $src); - $path = ltrim((string) parse_url($path, PHP_URL_PATH), '/'); + $path = self::pathFromSrc($src, $domain); if ('' === $path) { return $imgTag; @@ -1547,6 +1778,9 @@ CSS; return $imgTag; } + // 灯箱大图共用同一份数据,顺手记进本页数据表 + self::$_exifMap[$path] = $exifData; + $exifHtml = ''; if (!empty($exifData['camera'])) { @@ -1577,6 +1811,77 @@ CSS; }, $content); } + /** + * 从图片地址反推对象键 + * + * HTML 里的地址可能是百分号编码(中文文件名尤甚),而附件表里存的是原始文件名, + * 所以统一解码后再比对;只剥 path 部分,域名可能带协议差异。 + * + * @param string $src + * @param string $domain + * @return string + */ + private static function pathFromSrc($src, $domain) + { + $path = (string) parse_url(str_replace($domain, '', $src), PHP_URL_PATH); + $path = ltrim($path, '/'); + + if ('' === $path) { + return ''; + } + + return rawurldecode($path); + } + + /** + * 收集一段 HTML 里七牛图片的 EXIF(不改写 HTML) + * + * 用于 `{flex}` 画廊:包裹层会破版,所以只取数据,不插节点。 + * + * @param string $html + * @param string $domain + * @return void + */ + private static function collectExif($html, $domain) + { + if (false === strpos($html, ']*>/i', $html, $tags)) { + return; + } + + foreach ($tags[0] as $tag) { + // FlexPhoto 优先读 data-src(懒加载时 src 常是占位图),两个都收, + // 免得灯箱拿到哪个地址都能对上号。 + $candidates = array(); + if (preg_match('/\bdata-src=["\']([^"\']+)["\']/i', $tag, $m)) { + $candidates[] = $m[1]; + } + if (preg_match('/\bsrc=["\']([^"\']+)["\']/i', $tag, $m)) { + $candidates[] = $m[1]; + } + + foreach (array_unique($candidates) as $src) { + if ('' === $src || false === strpos($src, $domain)) { + continue; + } + + $path = self::pathFromSrc($src, $domain); + if ('' === $path || isset(self::$_exifMap[$path])) { + continue; + } + + $exif = self::findExif($path); + if (!empty($exif)) { + self::$_exifMap[$path] = $exif; + } + } + } + } + /** * 从附件记录里读 EXIF *