#!/usr/local/php/bin/php -q
<?
    /*
     *  Reformat iPhoto HTML output into XML, supplementing with data from
     *  image Exif data.
     *
     *  Usage: iPhotoToXML.php folder
     *
     *  In order for this to work, please use the following settings when
     *  exporting from iPhoto:
     *
     *      o Use enough rows or columns to get all the thumbnails on
     *        one page.
     *
     *      o Click show title on the thumbnail, but none of the others
     *
     *  This work is licensed under the Creative Commons Attribution-ShareAlike
     *  License. To view a copy of this license, visit
     *  http://creativecommons.org/licenses/by-sa/1.0/ or send a letter to
     *  Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305,
     *  USA.
     *
     *  Afternoon <noon at aftnn.org>, 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("{<title>\s+(.*?)\s+</title>}s", $index, $titleMatches))
            $pageTitle = $titleMatches[1];

        // get all thumnails and build XML
        $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>
';

            // 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 .= '   <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);

            // 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");
?>
		
aftnn.orgcontentcode → iphototoxml