2.9.1:EXIF 包裹层改 span、跳过 {flex} 画廊、findExif 多候选精确比对
This commit is contained in:
111
Plugin.php
111
Plugin.php
@@ -4,12 +4,20 @@
|
||||
*
|
||||
* @package Qiniu
|
||||
* @author 石头厝
|
||||
* @version 2.9.0
|
||||
* @version 2.9.1
|
||||
* @link https://www.shitoucuo.com/
|
||||
* @since 1.2.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* 说明(2.9.1 EXIF 与画廊/正文结构兼容):
|
||||
* 1. 包裹层 div → span。markdown 输出的图片是 `<p><img></p>`,在 `<p>` 里插 `<div>`
|
||||
* 会被解析器提前闭合,主题 `.markdown-body p` 的 10px 下边距于是变成空行。
|
||||
* 2. `{flex}` 画廊整块跳过:FlexPhoto 的横排布局按「figure 是 .flexphoto 的直接子项」
|
||||
* 写死,EXIF 包裹层会把 figure 顶到孙级,多图必破版。宁可在画廊里不显示 EXIF。
|
||||
* 3. findExif 改为「多取候选 + PHP 精确比对」:LIKE 里 `_`/`%` 是通配符,文件名前缀
|
||||
* 会带来大量下划线,`limit(1)` 撞到无关行就静默丢 EXIF。
|
||||
*
|
||||
* 说明(2.9.0 文件名前缀):
|
||||
* 新增「文件名前缀」设置,最终对象键为「保存路径前缀/文件名前缀_原文件名.扩展名」。
|
||||
* 前缀只作用于新上传;修改(替换)附件时沿用原路径,不会改名 —— 改名等于换地址,
|
||||
@@ -41,7 +49,7 @@ class Qiniu_Plugin implements Typecho_Plugin_Interface
|
||||
/**
|
||||
* 插件版本
|
||||
*/
|
||||
const VERSION = '2.9.0';
|
||||
const VERSION = '2.9.1';
|
||||
|
||||
/** 文件名前缀最大字符数(多字节按 1 个字符计) */
|
||||
const PREFIX_MAX_LEN = 40;
|
||||
@@ -1433,14 +1441,19 @@ CSS;
|
||||
*/
|
||||
private static function css()
|
||||
{
|
||||
return '.qiniu-exif-container{display:inline-block;position:relative;max-width:100%;}'
|
||||
// 包裹层用 span + line-height:0:图片在 <p> 里是行内元素,行高会在下方留出
|
||||
// 几个像素的空隙;换成 span 后必须靠 vertical-align/line-height 收掉。
|
||||
return '.qiniu-exif-container{display:inline-block;position:relative;max-width:100%;line-height:0;vertical-align:top;}'
|
||||
. '.qiniu-exif-info{position:absolute;top:0;left:0;right:0;background:rgba(0,0,0,0.85);color:#fff;'
|
||||
. 'padding:8px 10px;font-size:12px;line-height:1.4;opacity:0;visibility:hidden;'
|
||||
. 'padding:8px 10px;font-size:12px;line-height:1.4;opacity:0;visibility:hidden;text-align:left;'
|
||||
. 'transition:opacity .3s ease,visibility .3s ease;z-index:9999;border-radius:4px;'
|
||||
. 'box-shadow:0 2px 8px rgba(0,0,0,.2);pointer-events:none;}'
|
||||
. '.qiniu-exif-container:hover .qiniu-exif-info{opacity:1;visibility:visible;}'
|
||||
. '.qiniu-exif-info div{margin:3px 0;}'
|
||||
. '.qiniu-exif-info strong{font-weight:600;color:#fff;}';
|
||||
. '.qiniu-exif-info span{display:block;margin:3px 0;}'
|
||||
. '.qiniu-exif-info strong{font-weight:600;color:#fff;}'
|
||||
// 兜底:万一包裹层仍落进了 FlexPhoto 的画廊(例如主题/插件改过标记写法),
|
||||
// 缩略图已被 4:3 裁切,浮层意义不大,直接不显示,避免盖住画面。
|
||||
. '.flexphoto .qiniu-exif-info{display:none!important;}';
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1469,6 +1482,45 @@ CSS;
|
||||
return $content;
|
||||
}
|
||||
|
||||
// 画廊区块整块跳过。两种形态都要认:
|
||||
// - `{flex ...}...{/flex}`:本插件先执行,FlexPhoto 还没渲染
|
||||
// - `<div class="flexphoto ...">`:FlexPhoto 先执行,标记已被替换
|
||||
// FlexPhoto 的横排样式按「figure 是 .flexphoto 的直接子项」写死,
|
||||
// 中间插一层包裹元素会把 figure 顶到孙级,多图横排必坏,宁可不显示 EXIF。
|
||||
$parts = preg_split(
|
||||
'/(\{flex[^}]*\}.*?\{\/flex\}|<div\s+class="[^"]*flexphoto[^"]*"[^>]*>.*?<\/div>)/is',
|
||||
$content,
|
||||
-1,
|
||||
PREG_SPLIT_DELIM_CAPTURE
|
||||
);
|
||||
|
||||
if (!is_array($parts)) {
|
||||
return self::wrapImages($content, $domain);
|
||||
}
|
||||
|
||||
foreach ($parts as $index => $part) {
|
||||
if ('' === $part || preg_match('/^(?:\{flex[^}]*\}|<div\s+class="[^"]*flexphoto)/i', $part)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$parts[$index] = self::wrapImages($part, $domain);
|
||||
}
|
||||
|
||||
return implode('', $parts);
|
||||
}
|
||||
|
||||
/**
|
||||
* 给一段 HTML 里的七牛图片套上 EXIF 浮层
|
||||
*
|
||||
* 包裹层用 span 而不是 div:markdown 的图片在 `<p>` 里,
|
||||
* `<p>` 一旦遇到 `<div>` 会被解析器提前闭合,留下一段空段落。
|
||||
*
|
||||
* @param string $content
|
||||
* @param string $domain
|
||||
* @return string
|
||||
*/
|
||||
private static function wrapImages($content, $domain)
|
||||
{
|
||||
$pattern = '/<img\s+[^>]*src=["\']([^"\']+)["\'][^>]*>/i';
|
||||
|
||||
return preg_replace_callback($pattern, function ($matches) use ($domain) {
|
||||
@@ -1494,10 +1546,10 @@ CSS;
|
||||
return $imgTag;
|
||||
}
|
||||
|
||||
$exifHtml = '<div class="qiniu-exif-info">';
|
||||
$exifHtml = '<span class="qiniu-exif-info">';
|
||||
|
||||
if (!empty($exifData['camera'])) {
|
||||
$exifHtml .= '<div><strong>' . htmlspecialchars($exifData['camera']) . '</strong></div>';
|
||||
$exifHtml .= '<span><strong>' . htmlspecialchars($exifData['camera']) . '</strong></span>';
|
||||
}
|
||||
|
||||
$details = array();
|
||||
@@ -1515,12 +1567,12 @@ CSS;
|
||||
}
|
||||
|
||||
if (!empty($details)) {
|
||||
$exifHtml .= '<div>' . implode(' ', $details) . '</div>';
|
||||
$exifHtml .= '<span>' . implode(' ', $details) . '</span>';
|
||||
}
|
||||
|
||||
$exifHtml .= '</div>';
|
||||
$exifHtml .= '</span>';
|
||||
|
||||
return '<div class="qiniu-exif-container">' . $imgTag . $exifHtml . '</div>';
|
||||
return '<span class="qiniu-exif-container">' . $imgTag . $exifHtml . '</span>';
|
||||
}, $content);
|
||||
}
|
||||
|
||||
@@ -1534,30 +1586,37 @@ CSS;
|
||||
{
|
||||
try {
|
||||
$db = Typecho_Db::get();
|
||||
$row = $db->fetchRow($db->select()->from('table.contents')
|
||||
// LIKE 的 `_` / `%` 是通配符,文件名前缀又会带来大量下划线,
|
||||
// 只取一条容易先撞到无关附件(随后 path 比对不过就静默丢 EXIF)。
|
||||
// 这里多取几条候选,再在 PHP 里按 path 精确比对。
|
||||
$rows = $db->fetchAll($db->select()->from('table.contents')
|
||||
->where('type = ?', 'attachment')
|
||||
->where('text LIKE ?', '%' . $path . '%')
|
||||
->limit(1));
|
||||
->limit(20));
|
||||
|
||||
if (!$row || empty($row['text'])) {
|
||||
return array();
|
||||
}
|
||||
foreach ((array) $rows as $row) {
|
||||
if (empty($row['text'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$attachment = json_decode($row['text'], true);
|
||||
if (!is_array($attachment)) {
|
||||
// 1.2 及更早版本的可能序列化存储
|
||||
$attachment = @unserialize($row['text']);
|
||||
}
|
||||
$attachment = json_decode($row['text'], true);
|
||||
if (!is_array($attachment)) {
|
||||
// 1.2 及更早版本的可能序列化存储
|
||||
$attachment = @unserialize($row['text']);
|
||||
}
|
||||
|
||||
if (is_array($attachment) && isset($attachment['path']) && $attachment['path'] !== $path) {
|
||||
return array();
|
||||
}
|
||||
if (!is_array($attachment) || !isset($attachment['path']) || $attachment['path'] !== $path) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (is_array($attachment) && !empty($attachment['exif']) && is_array($attachment['exif'])) {
|
||||
return $attachment['exif'];
|
||||
if (!empty($attachment['exif']) && is_array($attachment['exif'])) {
|
||||
return $attachment['exif'];
|
||||
}
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
// 查询失败不影响正文
|
||||
} catch (Throwable $e) {
|
||||
// PHP 7+ 的 Error 同样不能让正文崩掉
|
||||
}
|
||||
|
||||
return array();
|
||||
|
||||
Reference in New Issue
Block a user