BigBear Design

Websites for Small Businesses

Create A Simple Photo Gallery With 6 Lines of Code

July 15th, 2006 · No Comments · PHP, web development

A client’s website needed a simple one-page photo gallery with small thumbnails in a few categories, probably Lightbox opening the photo when clicked. While there are more powerful photo gallery apps or scripts already available, they were too large, or are already taking too long to figure out how to use with the site’s design. Thankfully, I stumbled upon a link to a PHP article from DevSource and found out about the glob() function. Here’s a slightly edited version of the article’s example:


<?php
function getphotos($photogroup) {
  $files = glob ("images/thumbnails/$photogroup-{*.jpg,*.JPG}",  GLOB_BRACE);
  if (is_array($files)) {
    foreach ($files as $image_small) {
      $image_large = str_replace("thumbnails/", "", $image_small);
      echo "<a href=\"$image_large\"><img src=\"$image_small\" /></a> \\n";
  }
}
?>

This loads all jpegs in images/thumbnails/ then links to their full-sized counterparts in images/. The files are named with a photogroup- prefix so you can easily load only a particular group of photos. Ex. to load all images beachparty-01.jpg, beachparty-02.jpg, beachparty-03.jpg, etc., use <?php getphotos("beachparty"); ?>.

P.S. Turn off visual rich editor before trying to post code in Wordpress.

Tags:

0 responses so far ↓

  • There are no comments yet...Kick things off by filling out the form below.

Leave a Comment