I just did some modifications to the script (Twey's).
Strangeplant, the main thing that didn't work about your script was that it didn't include subdirectories. Twey's did that, so I could base what I did off of his.
So... here's what I ended up with.
I'd consider submitting to DD, assuming Twey is ok with it (I'd assume so, since he just wrote it up for this, from the looks of it.) Let me know what you think.
Not promising the code isn't messy... but it works.
This is the displaying page:
PHP Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Image Browser</title>
<style type="text/css">
ul.dirlist, ul.dirlist li {
list-style-type: none;
padding-left: 20;
}
a {
color: #0000FF;
}
</style>
</head>
<body bgcolor="#000000" text="#0000FF">
<?php
$page = substr($_SERVER['SCRIPT_NAME'],(strrpos($_SERVER['SCRIPT_NAME'],"/")+1));
function checkDir($c,$skipimages=0) {
echo('<ul class="dirlist">');
$d = opendir($c);
while($f = readdir($d)) {
if(strpos($f, '.') === 0) continue;
$ff = $c . '/' . $f;
if (strpos(strtolower($f), '.jpg') !== FALSE && $skipimages != 1) {
$fff = '<img src="thumbnail.php?image='.urlencode($ff).'" border="">';
echo('<a href="' . $ff . '">' . $fff . '</a>'."\n");
}
if(is_dir($ff)) {
$ffdisp = $ff;
if (substr($ff,0,2) == "./") $ffdisp = substr($ffdisp,2);
echo '<li><a href="'.$page.'?'.urlencode($ffdisp).'"'.">$ffdisp</a></li>\n";
global $dir;
if ($ff == $dir) $skipimages = 0;
else $skipimages = 1;
checkDir($ff,$skipimages);
}
}
echo("</ul>");
}
$uri = explode("?",$_SERVER['REQUEST_URI'],2);
$dir = urldecode($uri[1]);
if(is_dir($dir)) {
checkDir($dir);
if(strpos($dir,"/") !== FALSE) {
$updir = "?".substr($dir,0,strrpos($dir,"/"));
echo '<a href="'.$page.$updir.'">^up^</a><br>'."\n";
}
echo '<a href="'.$page.'">Images Main</a>'."\n";
}
else checkDir(".");
?>
</body>
</html>
thumbnail.php (required)
PHP Code:
<?php
// The file
if ($_GET['image'] == "") die('Image Does Not Exist');
$filename = urldecode($_GET['image']);
// Set a maximum height and width
$width = 200;
$height = 200;
// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// Content type
header('Content-type: image/jpeg');
// Output
imagejpeg($image_p, null, 30);
?>
thumbnail.php is just cut and paste from php.net's page on the imagecopyresampled() filter, plus changes in the top couple lines to include the get functions. I also moved the header down, 'cause that made sense.
The CSS/layout is customized to my site, but it's clean and shouldn't look too bad for others.
Test page:
http://ci-pro.com/truthandlies/images/
(This is for my current movie project. It has a bunch of images, mostly in the lowest level folders, but some in the middle ones. The script would work fine if they were in the top level, etc. as well.)
Problems:
1. There's a slight lag, especially with a lot of images. The GD rendering of the thumbnails is obviously server intensive. It isn't all that much slower, though, than loading the images would be anyway, and certainly not as slow as if the full size images were included with small height/width attributes and the whole image had to load.
2. I would like to display the number of images in a directory that isn't selected. This isn't crucial, but seems like the nice little piece that's missing. I did this, but it was a problem because I did it backwards.... the number was only available after the line with the link had been output.
3. It only works with JPGs. Not too hard to fix, but that's all I needed. That can be updated soon.
4. Just found an error. Images in a folder with subdirectories don't display. I'll figure this out tomorrow. For now, sleep. (It worked earlier. Something must be in conflict now.)
Bookmarks