Image Gallery: Yet Another
Here's yet another image gallery. This one requires no database, but requires you to create a file called captions.php in each directory of images, and populate it thusly:
<?php
$captions = array (
"image1.jpg" => "Caption for Image 1",
"image2.jpg" => "Caption for Image 2",
"image3.jpg" => "Caption for Image 3",
"image4.jpg" => "Caption for Image 4"
);
?>It features a potentially annoying "paramater hiding" feature, one-image-ahead-preload, and very simple navigation. It was written at the request of Eternal_Student on Experts Express.
<?php
###### user settings ################################
$defaultDir = 'img'; // this must be set
#####################################################
$args = array_keys($_GET);
$arg = $args[0];
list($dir,$image) = unserialize(base64_decode($arg));
// set a default dir
if (!$dir) $dir = 'img';
// validate directory
if (preg_match("#(\.|/)#", $dir)) die('dots and slashes in dir disallowed');
if (!is_dir($dir)) die('bad directory');
include($dir."/captions.php"); // loads in captions
// load all the images into an array
$images = array_keys($captions);
// if there's no image specified, use a default
if (!$image) $image=$images[0];
if (preg_match("#/#", $image)) die('slashes in image disallowed');
if (!file_exists("$dir/$image")) die("image does not exist");
// draw the navigation
$nav = '';
for($i=0; $i < count($images); $i++)
{
$img = $images[$i];
$arg = base64_encode(serialize(array($dir,$img)));
if ($img != $image)
$nav .= "<a href=?$arg>".($i+1)."</a>";
else
$nav .= ($i+1).' ';
if ($i < count($images) - 1) $nav .= ', ';
}
$nextImage = $images[$i];
if (!$nextimage) $nextImage = $images[$i-1];
?>
<html>
<head>
<style type="text/css">
body {
background-color: #eee;
font-size: 80%;
font-family: "HelveticaNeue","Arial","Verdana";
}
.nav {
text-align: center;
padding: 5px;
background-color: #ddd;
}
.content {
text-align: center;
padding: 30px;
background-color: white;
height: 100%;
}
.caption {
padding: 10px;
}
</style>
<body>
<div class="nav">
<?=$nav?>
</div>
<div class="content">
<div class="image">
<img src='<?=$dir?>/<?=$image?>'><br />
</div>
<div class="caption">
<?=$captions[$image]?>
</div>
</div>
<img src="/drupal/<?=$dir?>/<?=$nextImage?>" width="1" height="1" />
</body>
</html>
