#!/usr/bin/php
<?php

/*
 * Given a base path in $root, this script will parse the EXIF data from all ./<year>/<month>/*.jpg images in the
 * directory tree that don't have a resolution specified before the extension (e.g. skip image800x600.jpg).  The
 * resulting dataset is written in CSV form to $outfile.  A separate script is run beforehand to read the
 * manufacturer-specific EXIF tag corresponding to the lens used, and to store this value in a custom "LensID" tag.
 * Without the lens script, the "LensID" field of the output table will be empty.
 */

$root = "/var/www/hackology.co.uk/public_html/wp-content/uploads";
$outfile = "images.csv";
$dataset = array();

echo "Scanning files...";

foreach (scandir($root) as $year) {
	$path = $root . '/' . $year;
	if (!is_dir($path))
		continue;
	foreach (scandir($path) as $month) {
		$path = $root . '/' . $year . '/' . $month;
		if (!is_dir($path))
			continue;
		foreach (scandir($path) as $filename) {
			$file = $root . '/' . $year . '/' . $month . '/' . $filename;
			if (preg_match('#[0-9]+x[0-9]+.jpg$#i', $file) || !preg_match('#.jpg$#i', $file))
				continue;
			$exif_out = shell_exec('exiftool -php -FocusDistance -Aperture -FocalLength -ShutterSpeed -ISO -LensID -Model -DateTimeOriginal ' . $file);
			$data = eval('return ' . $exif_out);
			$dataset[] = $data[0];

			echo "Parsed $year/$month/$filename\n";
		}
	}
}

function endsWith($haystack, $needle)
{
    $length = strlen($needle);
    if ($length == 0) {
        return true;
    }

    return (substr($haystack, -$length) === $needle);
}

function parse_si($val, $unit, $to) {
	$prefixes = array( 'p' => 1e-12, 'n' => 1e-9, 'u' => 1e-6, 'm' => 1e-3, 'k' => 1e+3, 'M' => 1e+6, 'G' => 1e+9, 'T' => 1e+12 );
	$num = floatVal($val);
	$mag = 1;
	foreach ($prefixes as $prefix => $multiplier)
		if (endsWith($val, $prefix . $unit)) {
			$mag = $multiplier;
			break;
		}
	$num /= $mag;
	if (strlen($to))
		$num *= $prefixes[$to];
	return $num;
}

function ident($val) {
	return $val;
}

function metre($val) {
	return parse_si($val, "m", "");
}

function mmetre($val) {
	return parse_si($val, "m", "m");
}

function shutter($val) {
	return eval('return ' . $val . ';');
}

function dateVal($val) {
	$date = date_create_from_format('Y:m:d H:i:s', $val);
	return $date->getTimestamp();
}

echo "Building database... ";

$keys = array('SourceFile' => 'ident', 'FocusDistance' => 'metre', 'Aperture' => 'floatVal', 'FocalLength' => 'mmetre', 'ShutterSpeed' => 'shutter', 'ISO' => 'intVal', 'LensID' => 'ident', 'Model' => 'ident', 'DateTimeOriginal' => 'dateVal');

$fh = fopen($outfile, "w");
$headers = implode(",", array_keys($keys));
fwrite($fh, $headers . "\n");
foreach ($dataset as $row) {
	$cols = array();
	foreach ($keys as $key => $func) {
		$val = $row[$key];
		if (strlen($val))
			$val = $func($val);
		$cols[] = $val;
	}
	$line = implode(",", $cols);
	fwrite($fh, $line . "\n");
}
fclose($fh);

echo "Done!\n";

?>
