January 03, 2005
Introducing Asif: Convert EXIF data to RSS, Atom, or HTML
When you take a picture with a digital camera, most cameras store some data along with the picture, such as the date and time you took the photo, whether you used the flash, the focal length, the f-stop, and the make and model of the camera. This information is called EXIF data.
I thought it would be useful to be able to automatically publish this data in an RSS or Atom feed, so I wrote a PHP script to do just that.
My script, named Asif, will process a directory containing JPEGs and output an HTML, RSS, or Atom feed containing some or all of the Exif data for each picture, as well as (optionally) a thumbnail version of the picture.
See it in action:
This demo processes the files in this directory. The thumbnails are generated dynamically, as needed. (The good pictures are from manyhighways.com.)
Asif has two primary benefits:
- It allows you to instantly generate a gallery from a directory full of images without having to write a single line of HTML or set any configuration options. To accomplish this, just drop asif.php into a directory, and access the page.
- It allows you to instantly provide an RSS or Atom feed containing Exif data for any directory full of photos. You could use this for a photoblog, or, really, for instantly creating a photoblog by just dropping your photos into a directory on your server and renaming asif.php to index.php and pointing readers to that directory. Your gallery will automatically update every time you drop a file in the directory, and the RSS/Atom feed will always be fresh.
The output format is controlled by the user. Simply append the name of the format to the URL, and you get what you asked for. So, http://yourdomain.com/photos/asif.php?format=rss gets you RSS, and asif.php?format=atom gets you Atom. Omitting the format parameter produces HTML.
Similarly, the user controls whether to display thumbnails: asif.php?thumbnails=yes displays them. Omitting that parameter omits them. If thumbnails are requested, they will be included in the RSS or Atom feeds as well.
Asif can handle any directory path you pass to it on the URL. So, asif.php?format=rss&directory=sample will process the web directory called "sample". Directory paths are relative to the path of the script. So, asif.php?format=rss&directory=sample/travel/nyc will go down three directories and display the images. Omitting the directory directive tells Asif to display the data for the current directory.
Cameras often store far more Exif data than is really useful, such as manufacturers' notes that are essentially encoded gibberish. For this reason, Asif by default will only show you certain Exif fields. However, if you wish to see all available Exif data for a file, pass the directive &alldata=yes on the URL, and Asif will return everything it finds in the file.
By default, Asif produces valid XHTML 1.0, valid RSS 2.0, and valid Atom .3. As long as you don't have any weird stuff mixed into your Exif data, it should always validate. The appearance of the HTML is controlled entirely via CSS, which of course you can customize.
Requirements
Asif requires PHP5, compiled with the GD libraries and with the directive --enable-exif. However, I believe only one line of code -- a call to scandir() -- needs to be rewritten to work in PHP4. (I included the alternate PHP4-compliant code in the comments.) If you don't have PHP5 installed on your server, I discuss how to do so in another post.
Installation
- Get the source code.
- Get the CSS file asif.css so you can control the HTML formatting.
- Install both files in a directory on your webserver, and rename asif.phps to asif.php.
It will be instantly ready to use, assuming the GD and Exif libraries are working on your server. If desired, change the constants defined at the top of the file to meet your needs.
Asif is free software and covered by the GPL.
Using Asif
Asif is controlled by passing parameters on the URL. These parameters are described above and in the source code. Available parameters are: format, directory, alldata, and thumbnails.
formatcan be "RSS", "Atom", or "HTML". It defaults to HTML.directoryis the path you want to process. Omit the trailing slash on the path. For security reasons, Asif does not, by default, process directories above the one where the Asif script resides. So, if asif.php is inyourdomain.com/photos/galleries/travel/, you can't do something likeasif.php?directory=../sportsto display photos in/galleries/sports. Instead, put asif.php in thegalleriesdirectory, and pass the directory name on the url, likeasif.php?directory=sportsorasif.php?directory=travel. Get it? Also, if you omit thedirectoryparameter, Asif processes whatever directory it resides in.alldatacan be "yes" or "no"/left blank. It defaults to "no." If set to yes, alldata will show all available Exif data for each file. If left blank or set to something other than "yes", it will only show certain common pieces of Exif data: date/time taken, filesize, height/width, f-stop, camera model, ISO, flash, focal length, and user comment. It is not hard to customize the code to add or omit these fields.thumbnailscan be "yes" or "no"/left blank. If set to "yes," thumbnails will be generated (if necessary) and displayed. They will be included in the RSS or Atom feeds asimgtags, with thesrcattribute pointing to the URL for the thumbnail, and thelinktags pointing to the URL for the full image.
Support
If you have questions or comments, please post them below.
Disclaimer
I am not the world's greatest PHP programmer. The code is inefficient, it may not work, and it may expose your server to security risks. Be sure to read all of the comments in the code, and secure your own webserver properly. Use this software at your own risk. It is offered without warranty or assumption of liability.
Update (2005-02-09): I figured out how to generate RSS feeds using PHP5's DOM support, which I recommend using. When I get some time, I will update this script to use the DOM.
categories:
Software
posted by adm at January 3, 2005 08:22 PM
Trackback Pings
TrackBack URL for this entry:
http://www.thousandrobots.com/mt/mt-tb.cgi/752
Comments
Post a comment
Thanks for signing in, . Now you can comment. (sign out)
(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)
http://feedvalidator.org/check?url=http://thousandrobots.com/projects/asif/sample.php?directory=sample&thumbnails=yes&format=rss
your feed does not parse :-(
-C-
[sorry....it looks like the validator is somehow getting the HTML content instead of the XML....even though a regular browser gets the XML/RSS. not sure what's going on there. i'll take a look at it when i get a minute. in the meantime, i've come up with a much better way of accomplishing this using PHP5 and DOM. See thousandrobots.com for details. -adm]
Posted by: chris at March 29, 2005 05:37 PM