#!/usr/local/php/bin/php -q , 2003 */ 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); // re-organise folders and ditch pre-generated HTML pages for each image 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'"); // translate HTML to XML if (is_readable("$galleryRoot/$galleryName.html")) $index = implode("", file("$galleryRoot/$galleryName.html")); if ($index != "") { // get page title if (preg_match("{\s+(.*?)\s+}s", $index, $titleMatches)) $pageTitle = $titleMatches[1]; // get all thumnails and build XML $pictureCount = preg_match_all('{(.*?)}s', $index, $pictureMatches); if ($pictureCount) { print("Creating gallery.xml with $pictureCount pictures... "); $output = fopen("$galleryRoot/gallery.xml", "w"); $preamble = ' <![CDATA[' . $pageTitle . ']]> ' . $pictureCount . ' 0 '; // loop through the thumbs and compile information about them and their larger relatives // draws width, height and timestamps from the Exif data of the large version $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 .= ' ' . $pictureMatches[4][$i] . ' ' . $width . ' ' . $height . ' ' . $pictureMatches[3][$i] . ' ' . $pictureMatches[1][$i] . ' ' . $timestamp . ' '; if ($i == 0) $first = $timestamp; if ($i == $pictureCount - 1) $last = $timestamp; } $timeInfo = ' ' . $first . ' ' . $last . ' '; fputs($output, $preamble . $timeInfo . $picData . ''); fclose($output); // kill the old HTML index file 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"); ?>