You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在PHP Laravel中提取图片拍摄的含毫秒级的精确时间?

Extracting Sub-Second (Millisecond/Microsecond) Data from Image EXIF in Laravel PHP

First off, great call using exif_read_data for the base timestamp—most folks miss that sub-second (millisecond/microsecond) data isn’t included in the standard DateTimeOriginal tag. Instead, modern cameras store this detail in additional EXIF fields that you can combine with your existing timestamp.

Here’s how to pull that sub-second data:

Key EXIF Fields to Check

There are two primary tags that hold sub-second information, depending on the camera/device:

  • SubSecDateTimeOriginal: A single tag that includes the full timestamp with sub-seconds (e.g., 2024:05:20 14:30:45.123). Not all devices support this, but it’s the easiest if available.
  • SubSecTimeOriginal: A standalone tag that stores just the sub-second portion (e.g., 123 for milliseconds, or 123456 for microseconds). You’ll need to append this to DateTimeOriginal manually.

Working Code Example

Here’s a robust snippet for your Laravel app that handles both cases, plus fallbacks if sub-second data doesn’t exist:

// Get the uploaded file (adjust 'image' to your input name)
$uploadedFile = $request->file('image');
$exifData = exif_read_data($uploadedFile->getRealPath());

$fullTimestamp = null;

// First try the combined sub-second timestamp if available
if (isset($exifData['SubSecDateTimeOriginal'])) {
    $fullTimestamp = $exifData['SubSecDateTimeOriginal'];
}
// If not, combine DateTimeOriginal with SubSecTimeOriginal
elseif (isset($exifData['DateTimeOriginal'], $exifData['SubSecTimeOriginal'])) {
    $fullTimestamp = $exifData['DateTimeOriginal'] . '.' . $exifData['SubSecTimeOriginal'];
}
// Fallback to original timestamp if no sub-second data exists
elseif (isset($exifData['DateTimeOriginal'])) {
    $fullTimestamp = $exifData['DateTimeOriginal'];
} else {
    $fullTimestamp = 'No valid timestamp found in EXIF';
}

echo $fullTimestamp;

Important Notes

  • Precision varies: Some cameras store milliseconds, others microseconds—this depends entirely on the device that captured the image. There’s no universal way to force a specific precision unless you parse and format the sub-second value yourself.
  • Data loss risks: If the image was edited, compressed, or stripped of EXIF data (common in social media uploads), these sub-second tags might be missing. Always include existence checks to avoid errors.
  • Laravel-specific tip: When handling uploaded files, use getRealPath() to get the temporary file path required by exif_read_data—don’t use the public URL, as that won’t work with the function.

内容的提问来源于stack exchange,提问作者Wai Yan Hein

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:38:56