May 2, 2008

Bringing your photos from F-Spot to the Web

Author: Ben Martin

F-Spot is a graphical photo manager that allows you to tag your image files and search and view images based on those tags. With phpfspot, you can share the photo collection you manage with F-Spot with others through a Web interface and let them navigate through your photos using the tags you have set up.

No packages exist for phpfspot for Ubuntu, Fedora, or openSUSE. I built it from source using version 1.4 and installed phpfspot on the desktop machine that I have F-Spot installed on. If you're running Debian Etch you might like to see this HOWTO for installation. phpfspot requires PHP 5 with GD and SQLite3 extensions, as well as the Calendar and HTML_AJAX PEAR extensions and the Smarty template engine.

To see if your PHP installation includes support for SQLite and GD, run php -i at the command line and check the ./configure line that was used to compile PHP. Support for GD might be distributed in a separate package from PHP itself. For example, on Fedora 8, I had to install the php-gd package and restart Apache. You might find that SQLite support is available as a PHP Data Objects (PDO) driver; this is fine for phpfspot. If SQLite is available as PDO then your php -i will include the text pdo_sqlite on a line by itself. Determining whether you have SQLite support through PDO is a little tricky; if you do, the ./configure line shown by php -i will include the two options --without-sqlite and something like --with-pdo-sqlite=shared,/usr.

The Smarty dependency is packaged in php-Smarty for Fedora, the smarty package for Ubuntu, and php5-smarty for openSUSE. To install the PEAR extensions you must first install the php-pear package. With this you can use the pear command to install these dependencies:

# pear channel-update
# pear install HTML_AJAX
Failed to download pear/HTML_AJAX within preferred state "stable", latest release is version 0.5.5...
# pear install channel://
# pear install Calendar
Failed to download pear/Calendar within preferred state "stable", latest release is version 0.5.3...
# pear install channel://

Now you should expand the phpfspot tarball into your Web DocumentRoot, typically /var/www/html, and rename the phpfspot-1.4 directory to phpfspot.

phpfspot needs read access to your F-Spot $HOME/Photos directory and access to your F-Spot SQLite database. The documentation to phpfspot mentions that only read access is required for the F-Spot database, which makes sense, but I found that phpfspot would complain and fail to work if it did not have write access to my F-Spot database.

Instead of allowing Apache to read into your home directory, you might like to use a bind mount. A bind mount lets you mount a directory from a filesystem into another location. The filesystem can be accessed and modified from either mount point, and you don't have to remember which is the original and which is the bind mount or treat either in a special way. While you are at it, you might as well add a bind mount to allow phpfspot access to your $HOME/.gnome2/f-spot/photos.db file. The commands shown below create two new directories for the bind mounts and set up fstab to allow your photos and F-Spot database to be accessed from these new directories. The final commands are needed to make sure that normal filesystem protection does not stop Apache from reading your photo files. You might need to allow Apache to read and write to your photos.db file in /var/www/html/phpfspot/fspotdb if that directory is not already accessible to Apache.

# cd /var/www/html/phpfspot
# mkdir fspotdb
# mkdir Photos
# vi /etc/fstab
/home/ben/.gnome2/f-spot /var/www/html/phpfspot/fspotdb bind bind 0 0
/home/ben/Photos /var/www/html/phpfspot/Photos bind bind 0 0
# mount -a

# cd /var/www/html/phpfspot/Photos
# chgrp -R apache *
# find . -type d -exec chmod o+rx {} \;
# find . -type f -exec chmod o+r {} \;
# find . -type d -exec chmod o+s {} \;

# cd /var/www/html/phpfspot/fspotdb
# chgrp apache photos.db
# chmod g+rw photos.db

A sample configuration file, phpfspot_cfg.php.dist, is provided with the software. You should copy it to phpfspot_cfg.php and make changes to it before you can use phpfspot. The sample configuration assumes that your DocumentRoot is /var/www instead of /var/www/html (the default on Fedora). You will have to update the base_path and thumb_path declaration to reflect your DocumentRoot. On Fedora, SQLite databases are available through the PDO data provider, so you must change the db_access variable to use PDO instead of native database access. The Smarty package is installed into a directory with a capital "S," so that path needs a slight modification. phpfspot also expects the directory structure for Smarty to be slightly different than what Fedora provides, so I created a softlink in the Smarty directory below to let phpfspot access files using its file paths. fspot_db and phpfspot_db are paths to SQLite database files; the former is the database file that F-Spot has created. The F-Spot database is available through the bind mount that I set up above, so the fspot_db variable needs to point to the full path of that database. phpfspot_db is a path that phpfspot can write its own SQLite database into, and must be writable by the Web server. I also found that, although the comments in the sample configuration file mention that the file will be created for you, unless there is a writable file at that path, phpfspot will fail to work. To get around this, you can touch the path to create a zero-byte file, and phpfspot will generate that SQLite database from there. The other variables that you need to change are path_replace_from and path_replace_to, which let you modify the path to your image files in your fspot_db database. You will want to do this because the original image file names will be something like /home/ben/Photos/foo, which will most probably be outside your DocumentRoot directory.

# cd /var/www/html/phpfspot
# vi phpfspot_cfg.php
var $base_path = "/var/www/html/phpfspot";
var $thumb_path = "/var/www/html/phpfspot/thumbs";
var $db_access = "pdo";
var $smarty_path = "/usr/share/php/Smarty";
var $fspot_db = "/var/www/html/phpfspot/fspotdb/photos.db";
var $phpfspot_db = "/var/www/html/phpfspot/phpfspot-db/phpfspot.db";
var $path_replace_from = "/home/ben";
var $path_replace_to = "/var/www/html/phpfspot/";
# cd phpfspot-db
# touch phpfspot.db
# chgrp apache phpfspot.db
# chmod g+rw phpfspot.db

# cd /usr/share/php/Smarty
# ln -s `pwd` libs

At this stage I loaded the phpfspot URL into a browser and found that although tags were working I could not see any thumbnails, and clicking on an image to view it left a loading screen visible and never showed the image. I attempted to get around this by invoking the gen_thumbs.php PHP script to force the thumbnails to be updated. It turns out that the 10-megapixel images I was using were too large for the memory limits that PHP was being executed with. To change the limits, edit /etc/php.ini and change the memory_limit directive as shown below:

# vi /etc/php.ini
; Resource Limits ;

max_execution_time = 30 ; Maximum execution time of each script, in seconds
max_input_time = 60 ; Maximum amount of time each script may spend parsing request data
memory_limit = 64M ; Maximum amount of memory a script may consume (16MB)

# service httpd restart

At this point you should finally have phpfspot installed and working. You will be able to search for files based on time and tag restrictions as shown on the left side of the screenshot. The buttons in the top right of the screen allow you to zoom, show a slide show of the images that meet the current time/tag restrictions, get a link that you can use to load the current image again in the future, and show the current image full-size in a new window. When you are searching images, the buttons on the top right allow you to see all images matching the current search as a slide show, generate a link that you can use to directly load the current search into a browser, and generate an RSS feed for images that match the current search.

If you want to use phpfspot on a different machine than you use F-Spot on -- for example, on a Web server -- the main thing you have to change is the handling of the Photos directory and the $HOME/.gnome2/f-spot/photos.db file. Of course, you have to install phpfspot itself on the publicly accessible Web server too. Having phpfspot installed on both your desktop and a Web server is handy because you can verify changes locally before updating Web server collection. The bind mount trick won't work for your photos.db and Photos directory on the remote server. Instead you can use rsync to keep the Photos directory and photos.db up-to-date on the Web server by executing a command like the one below on the desktop machine, with replaced with your remote machine name:

$ cd /var/www/html/phpfspot
$ rsync -av Photos fspotdb

While installing phpfspot can be a bit of an adventure, once you have it up and running it offers a nice Web interface to your image collection, and one that uses the tags you have applied in F-Spot. If you already have your own Web space, phpfspot can be a welcome alternative to paying a monthly or yearly subscription fee to a photo-sharing service.


  • Graphics & Multimedia
  • Internet & WWW
  • Tools & Utilities