Skip to content

Instantly share code, notes, and snippets.

@timw4mail
Created January 23, 2012 21:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save timw4mail/1665631 to your computer and use it in GitHub Desktop.
Save timw4mail/1665631 to your computer and use it in GitHub Desktop.
Simple Image gallery script
<?php
$files = array_merge(glob("*.jpg"), glob("*.jpeg"), glob("*.png"), glob("*.gif"), glob("*.bmp"), glob("*.swf"));
sort($files);
$num_files = count($files);
if( ! isset($_GET['curr']))
{
$curr_num = 0;
$curr = $files[0];
$prev = $curr;
$next = $files[1];
}
else if(in_array($_GET['curr'], $files))
{
$curr = $_GET['curr'];
$files_rev = array_flip($files);
$curr_num = $files_rev[$curr];
$prev = ($curr_num > 0) ? $files[$curr_num - 1] : $curr;
$next = (isset($files[$curr_num + 1])) ? $files[$curr_num + 1] : $curr;
}
else
{
header('HTTP/1.0 404 Not Found', TRUE, 404);
echo "<h1>404 Not Found</h1>";
echo "The page that you have requested could not be found.";
}
?>
<a style="position:absolute;top:0;left:0" href="?curr=<?= $prev ?>">Previous</a>
<a style="position:absolute;top:0;right:0"href="?curr=<?= $next ?>">Next</a>
<br />
<?php if(stripos($curr, 'swf') !== FALSE): ?>
<center><object type="application/x-shockwave-flash" data="<?= $curr ?>" width="100%" height="90%">
<param name="movie" value="<?= $curr ?>" />
</object></center>
<?php else: ?>
<a href="?curr=<?= $next ?>">
<center><img src="<?= $curr ?>" style="max-width:100%" /></center>
</a>
<?php endif ?>
<script type="text/javascript">
var onkey = function(e){
e.preventDefault();
var left = 37,
right = 39;
if(e.keyCode === left )
{
document.location='?curr=<?= $prev ?>';
}
else if(e.keyCode === right || e.keyCode === 32)
{
document.location='?curr=<?= $next ?>';
}
};
window.addEventListener("keydown", onkey, false);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment