如何在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.,123for milliseconds, or123456for microseconds). You’ll need to append this toDateTimeOriginalmanually.
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 byexif_read_data—don’t use the public URL, as that won’t work with the function.
内容的提问来源于stack exchange,提问作者Wai Yan Hein
相关产品推荐
相关产品推荐

