Zum Inhalt springen

exif2folder - Changing the folder date

A little script that propably no one would ever want, except me. However, as I couldn't find anything similar, here my share. This little php shell script will scan through your foto collection (which has images in subfolders) and find out the oldest jpeg in each folder. The date of the oldest jpg is read out of the jpg's exif data. It will then change the folder date (mtime) to the same date of the oldest jpeg in the respective folder.

Requirements

It should be run from shell as otherwise you'd need to change the fileowner of all your jpegs to the whatever user your webserver is running under ... and that's something we don't want to do. "touch" will only work if you are the owner. So running it from shell is the best.

The script code

#!/usr/bin/php
<?php
## execute with "sudo php /path/to/script/exif2folder.php" ##
$rootpath "/mnt/raid/fotos/Geocaching"//path to you main fotos folder (must have subfolders)

function get_oldest_jpg($directory) { 
  if (
$handle opendir($directory)) { 
    while (
false !== ($file readdir($handle))) { 
      
$files[] = $file
    }         
    foreach (
$files as $val) {
      
$extension substr($valstrrpos($val'.'));
      if (
is_file($directory.$val) && ($extension == ".JPG" || $extension == ".jpg")) { 
        
$file_date[$val] = filemtime($directory.$val); 
      } 
    } 
  } 
  
  
closedir($handle); 
  
asort($file_dateSORT_NUMERIC); 
  
reset($file_date); 
  
$oldest key($file_date); 
  return 
$oldest


echo 
"Exif2Folder\\n";
echo 
"Folder mtime changer according to oldest JPEG in folder from exif\\n";
echo 
"-----------------------------------------------------------------\\n";

clearstatcache();
$count 0;
$folders glob($rootpath.'/*',GLOB_ONLYDIR);

foreach (
$folders as $folder) {
  
$count++;
  
$mtime date("d-m-Y H:i:s"filemtime($folder));
  echo 
$count.")\t$folder\tOrig. date [".$mtime."]\tNew date [";
    
  
//get oldest file in folder, (filemtime)
  
$oldest_jpg get_oldest_jpg("$folder/");
    
  
//read exif date from oldest_jpg
  
$exif_data exif_read_data ($folder."/".$oldest_jpg);
  
$edate $exif_data['DateTime'];
    
  
//result from edate = 2010:12:05 20:15:57;
  //convert edate to a touch usable date, e.g. 201012052015.57
  
$touchdate str_replace(":","",$edate);
  
$touchdate str_replace(" ","",$touchdate);
  
$seconds substr($touchdate, -2);
  
$touchdate substr($touchdate,0,-2);
  
$touchdate $touchdate.".".$seconds;

  
//now change the folder date to the exif date of oldest image in that folder
  
exec("touch -m -t {$touchdate} {$folder}");

  
$human_newdate str_replace(":","-",substr($edate,0,10)).substr($edate,-9);
  echo 
$human_newdate."]\\n";
}
?>

The output

The output will look similar to the image below. Click to see a larger version.

Exif2Folder Output

jpgTime - Image Timestamp Modifier

By the way, if you want to change the filedate of your images to the exif date of the image, there is a great little Windows program available called "jpgTime - Image Timestamp Modifier". It's available at http://jpgtime.learsy.com/.


Kommentiere diesen Artikel

Kommentar (erforderlich) (keine Anzeige) (optional)
Ja, erkenne mich, wenn ich das nächste mal hier bin