2.9.4:修复 EXIF 未约分分数(光圈 f/180.0、快门乱码)

This commit is contained in:
XIGE
2026-12-03 23:14:49 +08:00
parent fe550b7068
commit 8ff2c5bbb2
2 changed files with 124 additions and 13 deletions

View File

@@ -4,12 +4,22 @@
*
* @package Qiniu
* @author 石头厝
* @version 2.9.3
* @version 2.9.4
* @link https://www.shitoucuo.com/
* @since 1.2.0
*/
/**
* 说明(2.9.4 EXIF 数值解析):
* 1. 根因:`exif_read_data()` 不按 section 分组调用时,RATIONAL 类型返回的是**未约分的
* 分数字符串**(华为 P30 实测:曝光 `548000/1000000000`、光圈 `180/100`、
* 焦距 `5580/1000`)。老代码只处理了「数组形态」,字符串形态直接进 number_format(),
* 而 PHP 字符串转数字只取前缀 —— 光圈 180.0、焦距 5580.0,快门则原样贴出乱码。
* 现在统一走 `num()` 解析分数,快门再经 `formatShutter()` 写成 `1/2000` 这种写法。
* 2. 快门吸附标准档:厂商给的精确值带浮点误差(0.004228s),硬取整会得到 1/237
* 这种没人这么标的数字,所以吸附到最近标准快门档;偏离超过 1/6 档则按真实值输出。
* 3. ISO 兼容 EXIF 2.3 的 `PhotographicSensitivity` 字段名。
*
* 说明(2.9.3 拍摄时间与设置文案):
* 1. 拍摄时间(`date`)此前只抓不用,现已接到两处显示:正文悬停浮层第三行、
* 灯箱大图信息条第三行。字段同时加进页内数据表,否则大图查不到。
@@ -63,7 +73,7 @@ class Qiniu_Plugin implements Typecho_Plugin_Interface
/**
* 插件版本
*/
const VERSION = '2.9.3';
const VERSION = '2.9.4';
/** 文件名前缀最大字符数(多字节按 1 个字符计) */
const PREFIX_MAX_LEN = 40;
@@ -1944,6 +1954,91 @@ JS;
return array();
}
/**
* 把 EXIF 的数值统一转成 float
*
* PHP 的 exif_read_data() 在「不按 section 分组」的调用方式下,RATIONAL 类型会原样
* 返回**未约分的分数字符串**,而不是数组:
* 华为 P30(ELE-AL00)曝光 548000/1000000000、光圈 180/100、焦距 5580/1000。
* 老代码 `is_array($v) ? $v[0]/$v[1] : $v` 只处理了数组形态,字符串形态直接进入
* number_format(),而 PHP 转数字只取前缀 —— "180/100" 变成 180.0,输出「光圈 f/180.0」;
* 快门更是把 "548000/1000000000" 原样贴出去。所以这里统一按字符串解析。
*
* @param mixed $value 形如 "180/100"、"1/250"、[180, 100] 或纯数字
* @return float|null 无法解析或分母为 0 时返回 null
*/
private static function num($value)
{
if (is_array($value)) {
$num = isset($value[0]) ? (float) $value[0] : 0.0;
$den = isset($value[1]) ? (float) $value[1] : 1.0;
return (0.0 == $den) ? null : $num / $den;
}
$value = trim((string) $value);
if ('' === $value) {
return null;
}
if (false !== strpos($value, '/')) {
$parts = explode('/', $value, 2);
$den = (float) trim($parts[1]);
if (0.0 == $den) {
return null;
}
return (float) trim($parts[0]) / $den;
}
return is_numeric($value) ? (float) $value : null;
}
/**
* 快门速度格式化
*
* 分数 → 摄影习惯写法:0.000548s 显示为 `1/2000`,0.5s 显示为 `0.5s`。
* 倒数会吸附到标准快门档(1/250、1/2000 这种),因为厂商给的精确值往往是
* 0.004228s 这类浮点误差,直接取整会得到 1/237 这种没人这么标的数字。
* 偏离最近标准档超过 1/6 档(约 12%)时按真实值输出,不做硬套。
*
* @param float|null $seconds
* @return string
*/
private static function formatShutter($seconds)
{
if (null === $seconds || $seconds <= 0) {
return '';
}
if ($seconds >= 1) {
$text = number_format($seconds, 1, '.', '');
return rtrim(rtrim($text, '0'), '.') . 's';
}
$inverse = 1 / $seconds;
$standard = array(
2, 3, 4, 5, 6, 8, 10, 13, 15, 20, 25, 30, 40, 50, 60, 80,
100, 125, 160, 200, 250, 320, 400, 500, 640, 800,
1000, 1250, 1600, 2000, 2500, 3200, 4000, 5000, 6400, 8000,
10000, 12800, 16000, 20000, 25600, 32000,
);
$best = 0;
$bestDistance = null;
foreach ($standard as $candidate) {
$distance = abs(log($inverse / $candidate));
if (null === $bestDistance || $distance < $bestDistance) {
$bestDistance = $distance;
$best = $candidate;
}
}
if ($best < 2 || null === $bestDistance || $bestDistance > 0.12) {
return '1/' . (int) round($inverse);
}
return '1/' . $best;
}
/**
* 提取EXIF信息
*
@@ -1970,30 +2065,42 @@ JS;
$result['camera'] = trim($make . ' ' . $model);
}
// 曝光时间
// 曝光时间(原值是分数,必须解析后按摄影习惯重写)
if (!empty($exif['ExposureTime'])) {
$result['exposure'] = $exif['ExposureTime'];
$shutter = self::formatShutter(self::num($exif['ExposureTime']));
if ('' !== $shutter) {
$result['exposure'] = $shutter;
}
}
// 光圈值
if (!empty($exif['FNumber'])) {
$fnumber = is_array($exif['FNumber']) ? $exif['FNumber'][0] / $exif['FNumber'][1] : $exif['FNumber'];
$result['aperture'] = number_format($fnumber, 1);
$fnumber = self::num($exif['FNumber']);
if (null !== $fnumber && $fnumber > 0) {
$result['aperture'] = number_format($fnumber, 1);
}
}
// ISO
// ISO:EXIF 2.3 改名为 PhotographicSensitivity,两种都认
$iso = null;
if (!empty($exif['ISOSpeedRatings'])) {
$result['iso'] = is_array($exif['ISOSpeedRatings']) ? reset($exif['ISOSpeedRatings']) : $exif['ISOSpeedRatings'];
$iso = $exif['ISOSpeedRatings'];
} elseif (!empty($exif['PhotographicSensitivity'])) {
$iso = $exif['PhotographicSensitivity'];
}
if (is_array($iso)) {
$iso = reset($iso);
}
if (!empty($iso)) {
$result['iso'] = $iso;
}
// 焦距
if (!empty($exif['FocalLength'])) {
if (is_array($exif['FocalLength'])) {
$focal = $exif['FocalLength'][0] / $exif['FocalLength'][1];
} else {
$focal = $exif['FocalLength'];
$focal = self::num($exif['FocalLength']);
if (null !== $focal && $focal > 0) {
$result['focal_length'] = number_format($focal, 1);
}
$result['focal_length'] = number_format($focal, 1);
}
// 拍摄时间