download my media viewer

Years ago, I wrote the simple PHP media viewer that I use all over this website, and I have been expanding on it ever since. It displays images, MP4 videos and flash animations. It's called img.php because once, it only displayed images.

a stick figure labeled 'the user' with his head opened to receive 'content' which is being fired out of a catapult labeled 'the web' by two fingers labeled 'you'; the rubber band in the catapult is labeled 'php'. the background is one of Hieronymus Bosch' hell paintings. It displays your item of choice on a page with links that lead to the next and/or previous item. It also uses JavaScript to allow keyboard navigation, appropriately disabling links and button presses when it's at the start or end of a filelist.

Here are working examples of each option (more can be found all over this homepage, including the image beside this text):

There are two scenarios. The simplest one is where you have numbered images, numbered 001.jpg, 002.jpg, 003.jpg, or 001.gif, 002.gif, 003.gif, et cetera. Then all you need to do is place img.php in the same directory as those images, include an index.php like this (code that includes img.php highlighted):
<html>
<head>
<title>page title</title>
</head>
<body>

<?php $img = $_GET['n'] || $_GET['f'] || $_GET['v'] || $_GET['i']; if (!empty ($img)){include("img.php");} else {?>

Here are some links to numbered JPEGs:<p>

<a id="001" href="?n=001">image 001</a><br>
<a id="002" href="?n=002">image 002</a><br>
<a id="003" href="?n=003">image 003</a><br>

Here are some links to numbered GIFs:<p>

<a id="001" href="?n=001&g">image 001</a><br>
<a id="002" href="?n=002&g">image 002</a><br>
<a id="003" href="?n=003&g">image 003</a><br>

<?php };?>

</body>
</html>
and you're off to the races. However, if your files are not numbered, you have to give the code a file called files.json that lists the files in their proper order and on a page like the one above, include links like these:
<a id="image" href="?i=image.jpg">look at an image</a><br>
<a id="025" href="?n=025">look at a numbered image</a><br>
<a id="030" href="?n=030&g">look at a numbered GIF image</a><br>
<a id="video" href="?v=video">look at a video!</a><br>
<a id="flash" href="?f=flash">look at a flash animation</a>
Some notes for how the code is set up out of the box:
  1. The videos must be .mp4, the animations must be .swf, the numbered images must be .jpg or .gif.
  2. For the videos, animations and numbered images, do not include the file extension in your links.
  3. For the unnumbered images, you must include the file extension in your links.
  4. The links include IDs that match the item linked to; if the user clicks the "back" button they're taken back to where they were in the list of items. You can exlude these IDs and users will just be taken to the top of the page.
  5. For Flash files, you must also include their desired height and width in files.json. Check the examples.
I know that's a lot of really nitty-gritty info and I also know that I personally prefer working examples to get going with new stuff, so the zip below includes img.php and a full set of working examples, including folder structure, images, animations and videos. Unzip it, upload it to your server, and it'll work. The download file is beefy at over 400 kB (by itself, img.php is only 5 kB), but it's a soup-to-nuts experience.

download img.zip 417 kB, including examples and the text of this page as a README
I publish this media viewer into the public domain, so you can do whatever you want with it, no questions asked. Linkbacks are, of course, appreciated but not required.