使用 PHP exif_read_data 获取照片的“拍摄日期”而不是“修改日期”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13674296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:52:52  来源:igfitidea点击:

Getting "Taken Date" of a photo instead of "Modified Date" with PHP exif_read_data

phpimageexif

提问by Dima

I'm retrieving the creation date from a photo with exif_read_data PHP function (see the code below.) The dates retrieved from photos that haven't been modified return "Date Taken". Those that have been modified - "Date Modified". Is there a way to get the date a photo was taken, ignoring the "Date Modified" field?

我正在使用 exif_read_data PHP 函数从照片中检索创建日期(请参阅下面的代码。)从未修改的照片中检索的日期返回“拍摄日期”。已修改的那些 - “修改日期”。有没有办法获取照片的拍摄日期,而忽略“修改日期”字段?

$exif_data = exif_read_data ($filename);
if (!empty($exif_data['DateTime'])) {
    $exif_date = $exif_data['DateTime'];
}

Thank you.

谢谢你。

Edit: I think $exif_data['DateTime'] uses the first available date field. Since unmodified images had the same value for "Date Modified" and "Date Taken" it was always retrieving "Date Modified" in my case.

编辑:我认为 $exif_data['DateTime'] 使用第一个可用的日期字段。由于未修改的图像对于“修改日期”和“拍摄日期”具有相同的值,因此在我的情况下总是检索“修改日期”。

回答by paidforbychrist

Okay, I know this is a bit late as this question was posted a year ago, but I am posting this answer because I had the same question and my husband showed me a trick or two about how to get the answer, so I am sharing it. Write a php script to print out the exif_read_data array and you will find all sorts of interesting information. This (below) was printed on command line to stdout using print_r(). If you scroll down you will see two very interesting keys: [DateTime] => 2011:06:21 17:50:57 and [DateTimeOriginal] => 2011:06:04 08:56:22

好的,我知道这有点晚了,因为这个问题是一年前发布的,但我发布这个答案是因为我有同样的问题,我丈夫向我展示了一两个关于如何获得答案的技巧,所以我正在分享它。写一个php脚本打印出exif_read_data数组,你会发现各种有趣的信息。这(下面)是使用 print_r() 在命令行上打印到标准输出的。如果向下滚动,您会看到两个非常有趣的键:[DateTime] => 2011:06:21 17:50:57 和 [DateTimeOriginal] => 2011:06:04 08:56:22

I hope these will help you get what you need.

我希望这些能帮助你得到你需要的东西。

Array
(
    [FileName] => Pirate(F).JPG
    [FileDateTime] => 1405733742
    [FileSize] => 4017033
    [FileType] => 2
    [MimeType] => image/jpeg
    [SectionsFound] => ANY_TAG, IFD0, THUMBNAIL, COMMENT, EXIF
    [COMPUTED] => Array
        (
            [html] => width="2592" height="3888"
            [Height] => 3888
            [Width] => 2592
            [IsColor] => 1
            [ByteOrderMotorola] => 1
            [ApertureFNumber] => f/16.0
            [Thumbnail.FileType] => 2
            [Thumbnail.MimeType] => image/jpeg
        )

    [Make] => Canon
    [Model] => Canon EOS DIGITAL REBEL XS
    [Orientation] => 1
    [XResolution] => 4718592/65536
    [YResolution] => 4718592/65536
    [ResolutionUnit] => 2
    [Software] => QuickTime 7.6.9
    [DateTime] => 2011:06:21 17:50:57
    [HostComputer] => Mac OS X 10.5.8
    [YCbCrPositioning] => 1
    [Exif_IFD_Pointer] => 260
    [THUMBNAIL] => Array
        (
            [Compression] => 6
            [XResolution] => 4718592/65536
            [YResolution] => 4718592/65536
            [ResolutionUnit] => 2
            [JPEGInterchangeFormat] => 628
            [JPEGInterchangeFormatLength] => 4867
            [YCbCrPositioning] => 1
        )

    [COMMENT] => Array
        (
            [0] => AppleMark

        )

    [ExposureTime] => 1/200
    [FNumber] => 16/1
    [ExposureProgram] => 2
    [ISOSpeedRatings] => 400
    [ExifVersion] => 0220
    [DateTimeOriginal] => 2011:06:04 08:56:22
    [DateTimeDigitized] => 2011:06:04 08:56:22
    [ShutterSpeedValue] => 499712/65536
    [ApertureValue] => 524288/65536
    [ExposureBiasValue] => 0/1
    [MeteringMode] => 5
    [Flash] => 9
    [FocalLength] => 18/1
    [ColorSpace] => 1
)

回答by Dima

The solution is easier then I thought. I was referring to a wrong tag. To get date taken use:

解决方案比我想象的要容易。我指的是错误的标签。要获取使用日期:

$exif_data['DateTimeOriginal'];

$exif_data['DateTimeOriginal'];

回答by edigu

DateTime info exists in the Image File Directory (IFD) which a recurring data structure within the EXIFdata. To get taken date of photo and represent as a native php DateTimeobject, you need to fetch it from right IFD section :

日期时间信息存在于图像文件目录 (IFD) 中,它是EXIF数据中的重复数据结构。要获取照片的拍摄日期并将其表示为原生 php DateTime对象,您需要从右侧的 IFD 部分获取它:

<?php
$filename  = "/path/to/your/image.jpg";
$exifData  = exif_read_data( $filename, 'IFD0');
$takenDate = NULL;

if( $exifData !== FALSE ) {
  if( array_key_exists('DateTime', $exifData ) ) {
    $takenDate = new DateTime( $exifData['DateTime'] );
  } else {
    // No DateTime field available
  }

} else {
  // No exif data available
}

After that you can validate the exif DateTime data simply:

之后,您可以简单地验证 exif DateTime 数据:

is_null( $takenDate );

回答by Marek Prymula

echo "test1.jpg:<br />\n";
$exif = exif_read_data('tests/test1.jpg', 'IFD0');
echo $exif===false ? "No header data found.<br />\n" : "Image contains headers<br />\n";

$exif = exif_read_data('tests/test2.jpg', 0, true);
echo "test2.jpg:<br />\n";
foreach ($exif as $key => $section) {
foreach ($section as $name => $val) {
    echo "$key.$name: $val<br />\n";
}
}

will output

会输出

test1.jpg:
No header data found.
test2.jpg:
FILE.FileName: test2.jpg
FILE.FileDateTime: 1017666176
FILE.FileSize: 1240
FILE.FileType: 2
FILE.SectionsFound: ANY_TAG, IFD0, THUMBNAIL, COMMENT
COMPUTED.html: width="1" height="1"
COMPUTED.Height: 1
COMPUTED.Width: 1
COMPUTED.IsColor: 1
COMPUTED.ByteOrderMotorola: 1
COMPUTED.UserComment: Exif test image.
COMPUTED.UserCommentEncoding: ASCII
COMPUTED.Copyright: Photo (c) M.Boerger, Edited by M.Boerger.
COMPUTED.Copyright.Photographer: Photo (c) M.Boerger
COMPUTED.Copyright.Editor: Edited by M.Boerger.
IFD0.Copyright: Photo (c) M.Boerger
IFD0.UserComment: ASCII
THUMBNAIL.JPEGInterchangeFormat: 134
THUMBNAIL.JPEGInterchangeFormatLength: 523
COMMENT.0: Comment #1.
COMMENT.1: Comment #2.
COMMENT.2: Comment #3end
THUMBNAIL.JPEGInterchangeFormat: 134
THUMBNAIL.Thumbnail.Height: 1
THUMBNAIL.Thumbnail.Height: 1

source http://php.net/manual/en/function.exif-read-data.php

http://php.net/manual/en/function.exif-read-data.php

回答by Baba

Am not sure where you got your information but exifinformation is dependent on the image or captured device. Even if its modified the exif can sill be striped

不确定您从何处获得信息,但exif信息取决于图像或捕获的设备。即使它被修改,exif 也可以被条纹化

Example

例子

array (size=7)
  'FileName' => string 'img.jpg' (length=7)
  'FileDateTime' => int 1332747844
  'FileSize' => int 22569
  'FileType' => int 2
  'MimeType' => string 'image/jpeg' (length=10)
  'SectionsFound' => string 'IFD0' (length=4)
  'COMPUTED' => 
    array (size=5)
      'html' => string 'width="338" height="506"' (length=24)
      'Height' => int 506
      'Width' => int 338
      'IsColor' => int 1
      'ByteOrderMotorola' => int 0

This is a valid exifinformation but does not include

这是一个有效的exif信息,但不包括

  • Datetime
  • DateTaken
  • DateModified
  • DateTimeOriginal
  • DateTimeDigitized
  • 约会时间
  • 拍摄日期
  • 修改日期
  • 日期时间原件
  • 日期时间数字化

You really need to rethink your strategy and work with FileDateTimethats the only information am aware always present

您真的需要重新考虑您的策略并使用FileDateTime我知道的唯一信息始终存在