From the Publisher
About the Author
Jack Herrington is an engineer, author and presenter who lives and works in the Bay Area. His mission is to expose his fellow engineers to new technologies. That covers a broad spectrum, from demonstrating programs that write other programs in the book Code Generation in Action. Providing techniques for building customer centered web sites in PHP Hacks. All the way writing a how-to on audio blogging called Podcasting Hacks. All of which make great holiday gifts and are available online here, and at your local bookstore. Jack also writes articles for O'Reilly, DevX and IBM Developerworks.
Jack lives with his wife, daughter and two adopted dogs. When he is not writing software, books or articles you can find him on his bike, running or in the pool training for triathlons. You can keep up with Jack's work and his writing at http://jackherrington.com.
Excerpt. © Reprinted by permission. All rights reserved.
Use PHP s XML capabilities to parse through iPhoto s picture database. Apple is a company known for producing innovative and easy-to-use products.I was a bit dismayed by my options for sharing my photos from iPhoto, though. In particular, after having imported my digital photos from my camera and organizing them using iPhoto, I wanted to show off these pictures to family and friends. I didnt want to sign up for hosting, open an account with a photo printing service, wait for hundreds of files to upload somewhere, export photos to a smaller size, or reorganize all of my images in some other program after having already done the work in iPhoto. I wanted them available to everybody right now and I didnt want to have to lift a finger to make it so.When you combine this and a broadband connection with all of the information readily available in iPhoto, sharing photos becomes (as it should be)a snap.
If your PHP project requires a photo gallery component, it might be tempting to place the burden on users to upload, caption, and organize all of their photos into your system. However, if users have already done the work in iPhoto, do the rest for them! Armed with a simple XML parser, it s possible to extract all of the meaningful data from iPhoto and reformat it into a simpler format that s more appropriate and convenient for use with PHP.
A Look Behind the Scenes:iPhoto Data
The first logical step is to get up close and personal with iPhoto so that you know what data is easily available.
I am basing this discussion on iPhoto Version 5.x,the most current version of iPhoto available as of this writing. With a few small tweaks here or there, though, it s trivial to apply these same concepts to other versions of iPhoto something I ve been doing since iPhoto 2.0.
Figure 4-14 shows a small selection from my iPhoto album.
A quick look in ~/Pictures/iPhoto Library/shows almost everything we could ever need from iPhoto:
Directories broken down by date
For instance,~/Pictures/iPhoto Library/2005/07/02/contains photos from July 2,2005.The image files in this directory are the actual full size photos, but they contain all of the edits the user made from within iPhoto (i.e., rotations, color corrections, etc.).It also contains two other subdirectories: Thumbs ,which contains 240 ×180 thumbnails corresponding to each image, and Originals ,which contains the original, unmodified versions of the images (only if the user has performed any edits in iPhoto).Furthermore, in nearly all cases, these photos are in JPEG format, which is perfect for the Web.
One notable exception: if the user takes photos in RAW format (available on higher-end cameras),the Originals directory contains the RAW files and all other images are JPEG representations.
This XML document contains all of the really interesting (and uninteresting) data surrounding these photos: file paths for a given photo, captions, ratings, modification dates, etc. This file also contains information about groups of photos also called albums as well as user-defined keywords. Some version information and meta-information is included as well, but that s not terribly helpful.
So now we need to make some sense of that AlbumData.xml file. First off, it s not just any XML file; it s an Apple Property List. This means that a limited set of XML tags is being used to represent common programmatic data structures like strings, integers, arrays, and dictionaries (also known as associative arrays in some languages).Therefore, for the interesting structures within this file, we should look at some sample content, since the XML tags themselves arent terribly descriptive. Rather, the tagged content is where the meaty structure is. Ive cut some pieces out for the sake of brevity, but the more important parts of the file are here.
The beginning of the file looks something like this not terribly interesting:
But further down is a listing of all the photos in the dictionary keyed by unique identifiers for each photo. In the following example, you can see that we re looking at an individual photo with a unique ID of 5 .Furthermore, it s an image (rather than, say, a video)which has a caption of "No more pictures, please "as well as an optional keyword associated with it (the keyword s unique keyword ID is 2 ):
Master Image List
No more pictures,please
...and so on...