#!/usr/local/php/bin/php -q
<?
if ($argc < 2) die("Usage: iPhotoToXML.php folder\n");
$galleryRoot = $argv[1];
if (substr($galleryRoot, -1) == "/") $galleryRoot = substr($galleryRoot, 0, -1);
$galleryBase = explode("/", $galleryRoot);
$galleryName = array_pop($galleryBase);
$galleryBase = implode("/", $galleryBase);
$outputName = strtolower($galleryName);
if (is_dir("$galleryRoot/$galleryName-Thumbnails"))
rename("$galleryRoot/$galleryName-Thumbnails", "$galleryRoot/thumbnails");
if (is_dir("$galleryRoot/$galleryName-Images"))
rename("$galleryRoot/$galleryName-Images", "$galleryRoot/images");
if (is_dir("$galleryRoot/$galleryName-Pages"))
system("rm -r '$galleryRoot/$galleryName-Pages'");
if (is_readable("$galleryRoot/$galleryName.html")) $index = implode("", file("$galleryRoot/$galleryName.html"));
if ($index != "") {
if (preg_match("{<title>\s+(.*?)\s+</title>}s", $index, $titleMatches))
$pageTitle = $titleMatches[1];
$pictureCount = preg_match_all('{<img height="(\d+)" alt="(.*?)" width="(\d+)" src="' . $galleryName . '-Thumbnails/(.*?)\.jpg">}s', $index, $pictureMatches);
if ($pictureCount) {
print("Creating gallery.xml with $pictureCount pictures... ");
$output = fopen("$galleryRoot/gallery.xml", "w");
$preamble = '<?xml version="1.0" encoding="utf-8"?' . '>
<gallery>
<title><![CDATA[' . $pageTitle . ']]></title>
<pictureCount>' . $pictureCount . '</pictureCount>
<thumbnailId>0</thumbnailId>
<description><![CDATA[None]]></description>
';
$picData = "";
for ($i = 0; $i < $pictureCount; $i++) {
$exif = @read_exif_data("$galleryRoot/images/" . $pictureMatches[4][$i] . ".jpg");
if (array_key_exists("DateTimeOriginal", $exif) and $exif["DateTimeOriginal"] != -1) $datetime = $exif["DateTimeOriginal"];
else $datetime = $exif["DateTime"];
$s = sscanf($datetime, "%d:%d:%d %d:%d:%d");
$timestamp = mktime($s[3], $s[4], $s[5], $s[1], $s[2], $s[0]);
if (array_key_exists("Width", $exif) and array_key_exists("Width", $exif)) {
$width = $exif["Width"];
$height = $exif["Height"];
}
else if (array_key_exists("Width", $exif["COMPUTED"]) and array_key_exists("Width", $exif["COMPUTED"])) {
$width = $exif["COMPUTED"]["Width"];
$height = $exif["COMPUTED"]["Height"];
}
else {
$width = 1024;
$height = 768;
}
$picData .= ' <picture>
<id>' . $pictureMatches[4][$i] . '</id>
<image>
<width>' . $width . '</width>
<height>' . $height . '</height>
</image>
<thumbnail>
<width>' . $pictureMatches[3][$i] . '</width>
<height>' . $pictureMatches[1][$i] . '</height>
</thumbnail>
<comment><![CDATA[' . $pictureMatches[2][$i] . ']]></comment>
<date>' . $timestamp . '</date>
</picture>
';
if ($i == 0) $first = $timestamp;
if ($i == $pictureCount - 1) $last = $timestamp;
}
$timeInfo = ' <firstTime>' . $first . '</firstTime>
<lastTime>' . $last . '</lastTime>
';
fputs($output, $preamble . $timeInfo . $picData . '</gallery>');
fclose($output);
if (is_writeable("$galleryRoot/$galleryName.html")) unlink("$galleryRoot/$galleryName.html");
print("done\n");
}
else die("No pictures found in index file, $galleryRoot/$galleryName.html\n");
}
else die("$galleryRoot/$galleryName.html is empty\n");
?>