Better integration of my existing obscura galleries with Wordpress.
I don't think there is any actual obscura code in here other than the catalog reader, and I am using my own version of taimg, taimagick, but a lot of credit still goes to Alex for putting all this together in the first place.
I still use the obscura directory structure because it's easy, faster than using file upload forms, and all the data is owned by my user rather than the web server. Just makes it simpler.
Note that the obs_do_wordpress_top() and obs_do_wordpress_bottom() functions might need to be switched around depending on the theme currently in use.
The image overlays are provided by the javascript filter lightbox. To enable it without having to do anything fancy (like edit the theme files), I just installed the lightbox 2 wordpress plugin. If you're already using lightbox manually, don't bother with that.
The way this is designed requires a bit of Apache configuration hacking. This is done in the .htaccess file in the root folder of my web site.
The photographs and the script itself are stored in the tree pg but referred to as photos. This is mostly historical but I didn't see much need to change. To get the URLs to actually work, we just tell apache a few things about what to do using mod_rewrite. The relevant bits are here.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^photos/(.*).jpg$ pg/$1.jpg [L]
RewriteRule ^photos(.*) pg/index.php$1 [L]
RewriteRule ^phobos/(.*).jpg$ pg/$1.jpg [L]
RewriteRule ^phobos(.*) pg/o3.php$1 [L]
</IfModule>
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
The first rule tells apache that if it is sent after a file starting with photos and ending in .jpg, to find the real file in the pg tree, then stop processing rules.
The second rule tells apache that if it is sent after a file starting with photos, to send the request to the file pg/index.php and include everything after the word photos behind index.php. So that /photos/pg0032/ gets rewritten to /pg/index.php/pg0032.
<?php require('../wp/wp-config.php'); /* Should we use the wordpress lightbox plugin, or individual image pages? * If this is set to true you will need to enable the plugin in your * wordpress settings page. */ define(USE_LIGHTBOX,false); /* Should we include full size image links? Only applies if USE_LIGHTBOX * is set to false */ define(ALLOW_FULL_SIZE,true); /* Everything in here is obscura. This is shitty procedural code, but hey, * do what works. * * Dispatcher makes the assumption that path info will be of this form: * /gallery/ * * In current form everything after the first path is ignored. * * If we are just at /photos PATH_INFO is empty, so we show the index. If we * are at /photos/ PATH_INFO contains a single /, so we have cases for this. * * The reason for this behavior is the way that the mod_rewrite entry is * constructed */ /* a little init string */ $obs_path = ""; /* The dispatcher. If PATH_INFO is set, check it for a url pattern that * matches the structure of the galleries (/photos/gallery/image/) and * either display the index, gallery, or individual image. */ if(isset($_SERVER['PATH_INFO'])) { $obs_path = trim($_SERVER['PATH_INFO'],'/'); } if(strlen($obs_path) > 0) { $obs_patharray = explode('/',$obs_path); $obs_gallery = $obs_patharray[0]; if(count($obs_patharray) >= 2) { $obs_image = $obs_patharray[1]; obs_image($obs_gallery, $obs_image); } else { obs_gallery($obs_gallery); } } else { obs_index(); } function obs_read_catalog($file) { /*** * Opens a catalog file, reads the data into an array, and closes the file * * Can bomb out with an appropriate error if necessary. * */ $fh = @fopen($file,'r'); if(!$fh) { obs_error('I couldn\'t find a gallery here, <a href="/photos/">try again</a>.'); exit(); } $catalog = array(); while($row = fgetcsv($fh,1024,'|')) { $catalog[] = $row; } fclose($fh); return $catalog; } function obs_find_gallery_info($gallery_name) { /*** * Takes a given gallery name and finds it in the big gallery index. * Uses this to write a pretty header. * */ $obs_gallery_list = obs_read_catalog('index.gal'); foreach($obs_gallery_list as $obs_gallery) { $gallery_shortname = explode('/',$obs_gallery[0]); $gallery_shortname = $gallery_shortname[1]; if($gallery_shortname == $gallery_name) { return $obs_gallery; } } return null; } function obs_get_image_info($gallery_name, $image_name) { /*** * Takes a gallery and image, gets its information out of the index.cat * file, and sets an array of useful information. * * Array structure: * $image_info { * 'description' => contents of image description field * 'full' => url of full sized image * 'sized' => url of 500px resized image * 'thumb' => url of thumbnail image * 'prev' => url of previous image (or null) * 'next' => url of next image (or null) * } * */ $obs_catalog_path = './' . $gallery_name . '/index.cat'; $obs_image_list = obs_read_catalog($obs_catalog_path); for($i = 0; $i < count($obs_image_list); $i++) { $image_shortname = $obs_image_list[$i][0]; if($image_shortname == $image_name) { $image_index = $i; } } if(isset($image_index)) { $image_info = array( 'description' => $obs_image_list[$image_index][1], 'full' => "/photos/$gallery_name/$image_name.jpg", 'sized' => "/photos/$gallery_name/scale/$image_name-obs.jpg", 'thumb' => "/photos/$gallery_name/scale/$image_name-sq.jpg", 'next' => null, 'prev' => null ); if($image_index > 0) { $prev_index = $image_index - 1; $prev_name = $obs_image_list[$prev_index][0]; $image_info['prev'] = "/photos/$gallery_name/$prev_name/"; } if($image_index < count($obs_image_list) - 1) { $next_index = $image_index + 1; $next_name = $obs_image_list[$next_index][0]; $image_info['next'] = "/photos/$gallery_name/$next_name/"; } return $image_info; } else { obs_error('I couldn\'t find a that image, <a href="/photos/">try again</a>.'); } } function obs_do_wordpress_top($status=200) { /*** * The wordpress configuration was in the first part of this file. * This function initializes the main wordpress functions, sets the * page status (200, 404, etc) and writes the header of the page. * */ wp(); status_header($status); get_header(); print '<div id="content"><div id="main">' . "\n"; } function obs_do_wordpress_bottom() { /*** * Performs the equivalent of obs_do_wordpress_top() but writes the page * bottom instead. * */ wp(); print "</div></div>\n"; get_sidebar(); get_footer(); } function obs_index() { /*** * Builds an index based on index.gal in the /pg folder * */ $obs_gallery_list = obs_read_catalog('index.gal'); obs_do_wordpress_top(); print "<h2>Photographs</h2>\n"; foreach($obs_gallery_list as $obs_gallery) { $gallery_shortname = explode('/',$obs_gallery[0]); $gallery_shortname = $gallery_shortname[1]; print '<p><a href="/photos/' . $gallery_shortname . '/">' . $gallery_shortname . '</a>: '; print "$obs_gallery[1]</p>\n"; } obs_do_wordpress_bottom(); } function obs_gallery($gallery_name) { /*** * Builds a gallery based on index.cat in the pg/galleryname folder. * We're not bothering with paging here. * */ $obs_catalog_path = './' . $gallery_name . '/index.cat'; $obs_image_list = obs_read_catalog($obs_catalog_path); $obs_gallery_list = obs_read_catalog('index.gal'); obs_do_wordpress_top(); if($gallery_info = obs_find_gallery_info($gallery_name)) { print "<h2>$gallery_info[1]</h2>\n"; print "<p>$gallery_info[2]</p>\n"; } print '<p>'; foreach ($obs_image_list as $obs_image) { if(USE_LIGHTBOX) { // This is for the use of the lightbox plugin print '<a href="/photos/' . $gallery_name . '/scale/' . $obs_image[0] . '-obs.jpg" rel="lightbox[' . $gallery_name . ']" title="' . $obs_image[1] . '">'; } else { // This is for use of individual image pages print '<a href="/photos/' . $gallery_name . '/' . $obs_image[0]. '/">'; } print '<img src="/photos/' . $gallery_name . '/scale/' . $obs_image[0] . '-sq.jpg" alt="' . $gallery_name . '/' . $obs_image[0] .'" />'; print "</a>\n"; } print "</p>\n"; obs_do_wordpress_bottom(); } function obs_image($gallery_name, $image_name) { /*** * Shows an individual image, and links to previous/next images * */ $image_links = obs_get_image_info($gallery_name, $image_name); obs_do_wordpress_top(); /* Image heading with links */ print '<h2>' . $image_links['description'] . '</h2>'; print '<p>'; if($image_links['prev']) { print '<a href="' . $image_links['prev'] . '">previous</a> — '; } else { print 'previous — '; } if($image_links['next']) { print '<a href="' . $image_links['next'] . '">next</a>'; } else { print 'next'; } print ' — <a href="/photos/' . $gallery_name . '/">back to gallery</a>'; print "</p>\n"; /* Image with link to full size if enabled */ print '<p>'; if(ALLOW_FULL_SIZE) { print '<a href="' . $image_links['full'] . '">'; } print '<img src="' . $image_links['sized'] . '" alt="' . $gallery_name . '/' . $image_name .'" />'; if(ALLOW_FULL_SIZE) { print '</a>'; } print "</p>\n"; obs_do_wordpress_bottom(); } function obs_error($error_text) { /*** * Displays a 404 page with a configurable error message. * */ obs_do_wordpress_top(404); print "<p>$error_text</p>"; obs_do_wordpress_bottom(); } // Close out the page ?>