MG2 0.5.1 Modification for XHTML 1.0 Strict Validation

MG2 / MiniGal 2 is a free and easy-to-use PHP image gallery script. It is being offered as a non-profit service for photographers and webmasters who want a simple, fast and good-looking image gallery on their web site. No database is required, which makes MG2 ideal for people who want a PHP image gallery script but don’t have access to a SQL database.

I’ve been using MG2 / MiniGal 2 for over a year, and it’s perfect for sites that have a few dozen images to display, but that don’t require a massive image CMS. MG2 is very lightweight, weighing in at around 200k zipped, and customizable via template-based themes. It installs easily and features a web-based admin panel which allows images to be uploaded and titles and descriptions assigned to them.

MG2 finally validates as XHTML 1.0 Strict

MG2 claims to produce valid HTML out of the box, but I’ve found it to be otherwise. Because some lines of code bypass the template system, and some of those lines happened to contain attributes which are illegal in XHTML 1.0 Strict, it was necessary to make a number of changes to the core files before the gallery would output valid code. Until these core files were modified, even the most carefully constructed template would fail to validate because of elements beyond the template’s control.

The developer, Thomas Rybak, is assumed to be busy working on a new version, so I decided to pick up the baton and modify the most recent version, 0.5.1, to make the output code validate as XHTML 1.0 Strict. This update is not intended to be a new version or a fork; to whatever extent possible, the script remains the property of Thomas Rybak.

I have also created a new MiniGal theme template, called “minimalist,” which uses valid XHTML and CSS, and have included it in the download. Minimalist is loaded as the default theme during new installations and incorporates some improvements over the previous default theme, “rounded,” particularly in the area of search engine optimization. For example, the thumbnails page’s title tag now shows the current folder name and the gallery name, and the view image page’s title tag shows the image title, folder name, and gallery name. Each individual image page now contains a meta description tag which outputs the description for that image. (This description should be kept brief, less than 20 words, for best effect.)

Changes to the core and template files

The following changes to the MG2 / MiniGal 2 core and theme template files do not add any new functionality, but in some cases make existing functions more useful:

  • Removed all the illegal attributes that were preventing validation as XHTML 1.0 Strict, such as “align”, “border”, and “target”.
  • Replaced some unnecessary tables with divs.
  • Added a PHP “if statement” to hide the slide show link on those pages where it would be invalid. (Ex. pages that contain only subfolders)
  • Added a PHP “if statement” to hide the pages navigation on those pages where it would be pointless.
  • Clicking on an image during a slide show opens that image’s view image page, stopping the slide show.
  • Added a PHP “if/else” to display “(untitled)” if an image has no title.
  • Added a link back to the main gallery page from all subfolder pages.
  • Removed the automatic insertion of “<br />” at the end of the image description. The description is now meta tag friendly (see included templates).
  • The “Powered by…” td colspan now knows how many columns the page displays, rather than just using 100 to be safe.
  • Removed some of the meta robots instructions. Google is able to index the image gallery, so why not allow it to do so? (Robots are still banned from the slide show.)
  • Each image’s alt and title tags now return the image’s title.
  • Copyright changed to 2006.

Further refining MG2 usability and SEO with new functionality

One of my major complaints with MG2’s folder handling was that when viewing an image, the user couldn’t see what folder the image belonged to. My changes to the templates permit the view image page template (and optionally, the slide show template) to display the last folder to be opened, which in most cases should be the folder within which the currently viewed image belongs. This is accomplished by assigning the name of the last viewed folder to a variable: $lastfolder. The value of $lastfolder is then stored in an external file named “lastfolder.php” located in the gallery root where it can be retrieved later via an include in the view image page template.

Each thumbnails page can be thought of as a folder containing more folders, images, or a combination of both. Each thumbnails page has a name, which is defined by the gallery admin during folder creation, and which is stored by MG2 as the variable $currentfolder. The value of $currentfolder changes every time a new folder is opened. The gallery title is permanently assigned to the value of $mg2->gallerytitle. My script uses these two rules to determine the current location. While the user is viewing the gallery root, $currentfolder will equal that gallery’s gallery title. While the user is viewing any thumbnails page, the value of $lastfolder will be equal to the MG2-defined variable $currentfolder.

In an attempt to lighten the processing load, the script checks to see if the user has changed folders by comparing the value of $lastfolder with that of $currentfolder, and only writes to the file if the values are not equal. (This means that going back and forth between a folder and one of the images it contains does not necessitate writing to the file.)

Detail of the changes to the thumbnails page script

The page title is generated as follows: each time the thumbnails page loads, it checks to see if the current location is the image gallery root. If the location is the gallery root, the page title displays the gallery title. (ex.: My gallery) If the location is not the gallery root, the title displays the name of the current folder, followed by a separator, followed by the gallery title. (ex.: Folder A | My gallery)

Breadcrumb navigation is handled in a similar way. I’ve arranged the navigation in the Minimalist theme template to expand left to right, but the elements could easily be reordered to expand in another direction. If the current location is the gallery root, then the location is displayed as just the gallery title. (ex.: Location: My gallery) If the current location is not the root, then the location is displayed as the gallery title acting as a link back to the gallery root, followed by a separator, followed by the name of the current folder. (ex.: Location: My gallery | Folder A)

The thumbnails page also defines a variable, $lastfolder, as being equal to the name of the current folder, and saves the value in an external file to be retrieved later by the view image page. Each time the thumbnail page loads, it reads the value, if any, of the variable $lastfolder from the file “lastfolder.php” located in the gallery root. The page determines if the current location is other than the gallery root, then checks to see if we have changed folders by comparing the value of $lastfolder to the MG2-defined value of the variable $currentfolder. If we have changed folders, the page updates $lastfolder by redefining $lastfolder as equal to $currentfolder and then writing the new value to lastfolder.php.

Phew. Had enough? Ok, the download below contains the full MG2 / MiniGal 2 v0.5.1 PHP image gallery script, with the core files modified to produce valid XHTML 1.0 Strict code and the above described changes to functionality, plus the new “minimalist” custom theme template files. Please treat this as a work in progress. It should function as described, but if you notice anything amiss, please post a comment.

Download MG2 v0.5.1-Ardamis

  • E-mail this story to a friend!
  • Facebook
  • Digg
  • StumbleUpon
  • del.icio.us
  • Google Bookmarks
  • Technorati
  • LinkedIn
  • Reddit
  • MySpace
  • Slashdot
  • SphereIt
  • Sphinn
  • Mixx

2 Responses to “MG2 0.5.1 Modification for XHTML 1.0 Strict Validation”

  1. Colin says:

    Hi Ardamis, this is nicely done :) just wondering if I could enlist your assistance on something. I know MG2 is intended for small galleries, but what happens when you feed it ALOT of photos. I have been having problems, sometimes when a photo is uploaded it drops the index and all the photos then appear in the root folder. its quite a bug, if I can manage to fix that, then I could potentially use it for a number of sites. Does your version address this issue please? or can I please ask your assistance to help program a fix? please drop me a line. Many thanks, Cheers, Colin

  2. ardamis says:

    Hey there, Colin.

    Thanks for the interest. Actually, I haven’t used MG2 in well over a year, for the very reason you mentioned. After losing the folder structure of a site, I decided to get away from the flat file database and to start looking for a MySQL alternative. I’m not sure MG2 could be ‘fixed’ to make it able to handle ALOT of images, due to its use of a text file as a database.

    So, to answer your questions, no, my changes don’t make MG2 any more stable, they just make the output valid HTML, and I can’t code a fix for the instability issues.

    The search for a replacement to MG2 led me to Plogger ( http://plogger.org/ ), which I’ve found to be more stable than MG2. Plogger has its shortcomings, but on the whole I find it much more dependable.

    I’ve developed a few themes for Plogger, too:
    http://www.ardamis.com/2007/09/10/plogger-3-theme-broadway/
    http://www.ardamis.com/2007/07/05/plogger-beta-3-theme-air/

    Good luck.

Leave a Reply

Wrap code snippets in <code></code> tags.