#!/usr/bin/php 
<?php
/**
 * Tiny script that simply prints the Date to stdout when a picture was taken
 * according to the image EXIF date.
 * I used it for renaming my holidy pictures.
 * 
 * @author Julius Beckmann
 * @link http://juliusbeckmann.de/
 * @license GPL
 */

/**
 * Output format for date().
 */
define('FORMAT_DATE''Y-m-d_H-i-s');

/**
 * Prints error message to STDOUT and exits with 1.
 */
function error($msg) {
  
fwrite(STDERR"[Error] ".$msg."\n");
  exit(
1);
}

// Check for usage call
if($argc 2) {
  
fwrite(STDOUT"Usage: '".basename($argv[0])." IMAGE_FILENAME'\n");
  exit(
0);
}

// Check argument count
if($argc != 2) {
  
error("Need exactly 1 argument: IMAGE_FILENAME");
}

// Check file
$file $argv[1];
if(!
is_file($file)) {
  
error("File '$file'does not exist");
}

// Read time from exif data and convert to timestamp
$exif exif_read_data($file);

if(!isset(
$exif['DateTime'])) {
  
error("No EXIF 'DateTime' found in file '$file'");
}

// Convert and print time
$timestamp strtotime($exif['DateTime']);
fwrite(STDOUTdate(FORMAT_DATE$timestamp));