--- Shad <[EMAIL PROTECTED]> wrote:

> hi people. I hate it when I go to a url only to be confronted with a long
> list of filenames for the graphics that are in that directory.
> 
> I want to write some php that can be pointed at a directory on any site on
> the net that is publically accessible and have it create thumbnails so I can
> see at a glance whats there. at least that's the first step
> 
> I have no idea where to start. I mean the way I thought of doing it would be
> to query the folder for the file info and then looping with each file, insert
> the code to build the html page. I just don't know where to start. any
> helpful pointers would be great and I'll just go from there.
> 
> thanks peeps :D
> 
> s.
> 
> p.s. I know that maybe theres something out there that already does this, but
> I wanna write it anyway :p

If you want true thumbnails (ie small files) then you will have to have a tool
on the server to do this.  It can be done with ImageMagick or even with the GD
library loaded into PHP.

The directory which holds the thumbnails must be writable by the web server
user if you expect to create new thumbs as needed.

If you don't need to have the thumbnails generated, you can do something like
this:

<html>
<head>
<style type='text/css'>
div { height: 290px; width: 260px; float: left; text-align: center; 
      background: #eee; font: bold 10px Arial Narrow, sans-serif; 
      margin: 5px; }
img { max-height: 235px; max-width: 235px; 
      margin: 10px; border: 2px solid black; padding: 2px; }
</style>
</head>
<body>
<?php
$allowed = array("png", "jpg", "jpeg", "gif");

$dir = "."; # this directory (code is in image directory)

if (is_dir($dir)) 
{
 if ($dh = opendir($dir)) 
 {
  while (($file = readdir($dh)) !== false) 
  {
   $ext  = substr($file, strrpos($file, '.')+1);
   if (in_array($ext, $allowed))
   { 
    $thumb = (file_exists("$dir/th/$file")) ? "$dir/th/$file" : "$dir/$file";
    $list[] = "<div><a href='$dir/$file' target='_blank'><img src='$thumb'
/><br />$file</a></div>\n";
   }
  }
  closedir($dh);
 }
}

natcasesort($list);

foreach ($list as $item)
{
 print "$item";
}

?>
</body>
</html>

The th directory holds the thumbnails and the program looks for a thumbnail
file with the exact same name as the file in the main image directory.  For
example:

dir
dir/imageA.jpg
dir/imageB.jpg
dir/imageC.jpg
dir/th/imageA.jpg
dir/th/imageB.jpg
dir/th/imageC.jpg

To make this program generate thumbnails you'd want to check to see if the file
exists in an if statement (instead of the conditional operator I use for
$thumb) and have the program create needed thumb files.  Otherwise, you could
have a cron job look for missing thumbs and create them on some periodic basis.
 This could be done with a Bash script or another tool available on your
server.  I'd use ImageMagick for this since the results are often better than
using the GD library tools.

The above script will work without thumbnails and simply send the full size
image and let the browser show it smaller.  It doesn't conserve on bandwidth
but it does let you throw a bunch of images on the screen.  I had planned to
add some features but this served my immediate needs.

Naturally the script could be named index.php but in my example I have it
called gal.php:

http://www.keeline.com/Verne/Verne2/gal.php
http://www.keeline.com/QuinbyAutos/gal.php

James

Reply via email to