Dima
Dima

Reputation: 527

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

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_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.

Upvotes: 16

Views: 39663

Answers (5)

paidforbychrist
paidforbychrist

Reputation: 359

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

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
)

Upvotes: 14

Dima
Dima

Reputation: 527

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

$exif_data['DateTimeOriginal'];

Upvotes: 11

edigu
edigu

Reputation: 10099

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

<?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:

is_null( $takenDate );

Upvotes: 3

Baba
Baba

Reputation: 95131

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

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 exif information but does not include

  • Datetime
  • DateTaken
  • DateModified
  • DateTimeOriginal
  • DateTimeDigitized

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

Upvotes: 2

Marek Prymula
Marek Prymula

Reputation: 31

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

Upvotes: 3

Related Questions