Assign Node->Created from EXIF Picture Taken data
hhg - July 20, 2008 - 20:56
| Project: | Image Publishing |
| Version: | 5.x-1.x-dev |
| Component: | Code |
| Category: | feature request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | active |
Jump to:
Description
I think it would be usefull to set the Node Creation time to the Picture Taken Date/Time, usually available in the EXIF information.
Add the following code into image_pub.module at line 253, just above the node_submit:
<?php
$exif = @exif_read_data($node->images['_original'], 0, true);
if(!empty($exif["EXIF"]["DateTimeOriginal"])) {
$DateTimeOriginal = $exif["EXIF"]["DateTimeOriginal"];
$node->date = str_replace(':', '-', substr($DateTimeOriginal,0,10)) . ' '. substr($DateTimeOriginal,11);
}
?>I can roll a proper patch if it is of any use.

#1
This looks like a worthwhile feature, though I wonder if it would actually be better to roll any patches against image.module, so that it would apply to all images submitted, not just ones submitted via image_pub.
Perhaps it might also worth checking the exif module, to make sure there is no duplication there?
Finally, running my code through the drupal code checking perl script code-style.pl in the scripts directory of the drupal installation, has taught me that $DateTimeOriginal shouldn't be in CamelCase; it should be something like $date_time_original.
@hhg: Please let me know what you think. If I don't hear anything after a couple of weeks, I'll close this issue.
#2
I needed something similar and since the Exif module for Drupal 6 is still under development, decided to hack something in.
Add this to image_pub.common.inc around line 192:
// Copy exif data into cck fields:
$exif = exif_read_data($_FILES[$srcfield]['tmp_name'], 0, true);
//watchdog('image_pub', 'debug: ' . print_r($exif, true));
if(!empty($exif["EXIF"]["DateTimeOriginal"])) {
$date = $exif["EXIF"]["DateTimeOriginal"];
//YYYY:MM:DD HH:MM:SS
//preg_match('/([0-9]{4}):([0-9]{2}):([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})/', $date, $matches);
$date = strtotime($date);
$form_state['values']["field_date_picture_taken"][0]["value"]["date"] = date("m/d/Y", $date);
$form_state['values']["field_date_picture_taken"][0]["value"]["time"] = date("H:i", $date);
}
if(!empty($exif["IFD0"]["UndefinedTag:0x4746"])) {
$form_state['values']["field_rating"]["value"] = $exif["IFD0"]["UndefinedTag:0x4746"];
}
/*
if(!empty($exif["IFD0"]["Title"])) {
$form_state['values']["title"]
= mb_convert_encoding($exif["IFD0"]["Title"], "UTF-8");
}
*/
UndefinedTag:0x4746 is Windows Vista's photo gallery rating field. I couldn't get the Title working since it seems to have some whacky encoding, but left it commented out in case somebody else wants to finish it.
To use you need to create a CCK datetime field called date_picture_taken and a CCK integer field with values 1-5 called "rating".
#3
Thanks for the code snippet, bbirtle. I am still holding out for this type of functionality to be incorporated in exif module or image module, but rather than closing this issue (as I suggested earlier), I will leave it open so that people can find the code snippets more easily.
#4
Egfrith, I agree it should be in the Exif module, not here. I'm just trying to hack my site together and this seemed like the easiest way.
PS, here's another tangential code snippit I also added. It reorganizes the uploaded image into folders based on the gallery name I choose. Complete hack, and should probably go into the Image module (since by default it dumps 1,000,000 files into the sites/default/files folder), but figured I'd post it anyhow:
// Move uploaded images into a better organization depending on the gallery name chosen:
// gallery name should be like "2009/Winter/sometripname", where we want to create a folder structure
// to match on the server like that. So first create any missing folders for the path:
$path = file_create_path(variable_get('image_default_path', 'images'));
$base = dirname(__FILE__) . "/../../"; // HACKHERE - we're in modules/image_pub. Must be better way!
foreach(explode("/", $album->name) as $folder) {
$path = $path . '/' . $folder;
if(!is_dir($base . $path)) {
mkdir($base . $path);
}
}
// now move all the files we just created in the above into that folder:
//$files = db_query("select * from files where nid = " . $node->nid);
$files = db_query("select f.* from files f join image i on i.fid = f.fid where i.nid = " . $form_state["nid"]);
while ($file = db_fetch_object($files)) {
if(!file_exists($base . $file->filepath)) {
// bug in image publisher - creates one file that doesn't exist, appearently in the temp folder...
continue;
}
$newPath = $path . "/photo_" .$file->fid . "_" . $file->filename . ".jpg";
rename($base . $file->filepath, $newPath);
$file->filepath = $newPath;
drupal_write_record('files', $file, "fid");
}
With that and the EXIF stuff, I think I'm golden with image uploading. Thanks to all who created both this and the Gallery Remote modules since they're really a powerful addition to my and others sites!