RE: [PHP] ### Image Display Problem ### From: Lawrence.Sheed <email protected>
Date: 09/25/00

Hi Theo,

Below is some sample code (modified & copied from what I use).

I have a fully working thumbnail creation / directory browse php script
which I use wrote for my site
You can have a look at the results by going to www.shanghaiguide.com/pics

What I do is set my .htaccess file to use my script - /pics/readdir.php
and I pull the current directory from the uri. That way people can jump
into /pics/[directory name here] and it gets parsed correctly

The bits you'll be interested in below are Get_Image_List ($sPath)

This parses the current directory, and builds a list of picture files (with
associated sizes and other info),
If the current file is a directory, it gets the number of files in the
directory - GetDirCount, and continues.

You could use recursion to iterate through all the subdirectories if needed,
although that would probably incur more array depth.

This should give you a start at least.

If you want the full version, give me an email.

Currently its a collection of 3 scripts - one which does click tracking &
postcard sending, another which shows the top 5 pictures, and the browse one
below.

cheers,

Loz.
==========================================
<?php

$theight=96;
$djpeg = "/usr/bin/djpeg"; /* decompresses a jpeg to ppm format */
$cjpeg = "/usr/bin/cjpeg"; /* compreses a ppm to jpeg format */
$pnmscale = "/usr/bin/pnmscale"; /* scales a ppm image */
$giftopnm = "/usr/bin/giftopnm"; /* convert a gif to ppm format */
$ppmtogif = "/usr/bin/ppmtogif"; /* convert a ppm to gif format */
$ppmquant = "/usr/bin/ppmquant"; /* colour quantize a ppm image */
$pnmpad = "/usr/bin/pnmpad";
$shadow = "/usr/local/bin/shadow"; /* Put a drop-shadow on a GIF */

function thumbnail($filename) {
        global $theight, $djpeg, $cjpeg, $pnmscale, $giftopnm, $ppmtogif,
$ppmquant, $pnmpad, $shadow;

        $tdir = dirname($filename)."/thumbnails"; /* thumbnail directory */
        if(!filetype($tdir)) {
                if(! <email protected>($tdir,0777)) {
                        echo "Unable to create $tdir directory - check
permissions<br>\n";
                        return;
                }
        }
        $tfile = $tdir."/".basename($filename); /* thumbnail file */
        //$tfile = ereg_replace("\.[^\.]+$",".gif",$tfile);
        if(!filesize($tfile)) {
                if(eregi("\.gif$",$filename)) { /* Look for .gif extension
*/
                        //echo "$giftopnm $filename | $pnmscale -height
$theight | $pnmpad -white -l6 -b6 | $ppmquant 248 | $ppmtogif > $tfile";
                        exec("$giftopnm $filename | $pnmscale -xy 200 175 |
$pnmpad -white -l6 -b6 | $ppmquant 248 | $ppmtogif > $tfile");
                } elseif(eregi("\.jpg$",$filename)) { /* Look for .jpg or
.jpeg extension */
                        //echo "$djpeg $filename | $pnmscale -height
$theight | $cjpeg -outfile $tfile";
                        exec("$djpeg $filename | $pnmscale -xy 200 175 |
$cjpeg -outfile $tfile");
                } else { /* not a GIF or JPG file */
                        echo "Not Gif or JPG";
                        return("");
                }
        }
        return($tfile);
}

function GetDirCount ($sPath) {
        $handle=opendir ($sPath);
        while ($file=readdir($handle)){
                $count++;
        }
        return ($count);
}

function Get_Image_list($sPath) {
        global $dir_array,$file_array,$gen;
        if(!$sPath) {
                $sPath = ".";
        }
        $dir_handle = opendir($sPath);
        $count = $a = 0;
        while($file = readdir($dir_handle)) {
                if((preg_match('/jpg/i',$file)) ||
(preg_match('/png/i',$file)) || (preg_match('/gif/i',$file)) ||
(preg_match('/jpeg/i',$file))) {
                        if (!(substr($file,1,1)=="_")) { //I prefix
underscore to my non picture files - ie site graphics, I don't want them
listed.
                                //for adding the result to the array in PHP
3, which doesn't have push
                                $size = GetImageSize ( "$sPath/$file");
                                $file_array [$a]=
array($file,$size[0],$size[1],filesize ("$sPath/$file"),date("F j Y h:i:s
A", filemtime("$sPath/$file") ) );
                                $a++;
                                if (!is_file ("/$sPath/thumbnails/$file")) {
                                        //Generating Thumbnails
                                        echo "Gen
Thumbnail:$sPath/$file<br>";
                                        thumbnail ("$sPath/$file");

                                }
                        }
                }
                elseif (is_dir ("/$sPath/$file")) {
                        if ($file != "thumbnails") {
                                $dir_array [$count]=
array($file,GetDirCount ("$sPath/$file"));
                                $count++;
                        }
                }
        }
}

if (!isset($file_path)) {
        $file_path=substr($REQUEST_URI,1,(strlen($REQUEST_URI)-2));
}
else {
        //Get real path (strip out the ../ crap - realpath returns with
curdir prepended,so strip too)
        $file_path = realpath($file_path);
        $file_path=substr($file_path,strlen(getcwd())+1,(strlen($file_path)
- strlen(getcwd())) );
}

$file_array = array();
$dir_array=array();
Get_Image_list("$DOCUMENT_ROOT/" . $file_path);

//Sample code to print $file_array
$count=0;
for ($count=$pos;$count<($pos+$max_per_page);$count++) {
        print "<a
href='/phpcommon/postcard.php3?pic=/".$file_path."/".$file_array[$count][0].
"'><img src='/".$file_path."/thumbnails/".$file_array[$count][0]. "' alt='"
. $file_array[$count][0] .
                 "' width=".$size[0].
                 " height=" .$size[1].
                 " border=0></a><br>" . $file_array[$count][1] .
                 "x". $file_array[$count][2].
                 " - ". round($file_array[$count][3]/1024). "k " .
                 " - " . $file_array[$count][0] .
                 "<br>" . $file_array[$count][4];
}

//Sample code to print $dir_array
$exitstr="";
$max2=sizeof($dir_array);
for ($count2=0;$count2<$max2;$count2++){
        $exitstr=$exitstr. "<a
href=\"$PHPSELF?file_path=$file_path/".$dir_array[$count2][0]."\">[".str_rep
lace("_"," ", $dir_array[$count2][0] )." (".$dir_array[$count2][1].")
]</a><br><br>";
}
$exitstr=$exitstr. "<br><br><a href=\"$PHPSELF?file_path=pics\">[up to
picture directory]</a><br>";

if (($pos+$count) < $max){
        ParseFile ($content,"{SIDEBAR}","$exitstr<br><A
href=\"$PHPSELF?pos=".($pos+$count)."&amp;file_path=$file_path\">Next</a>");
}
else {
        ParseFile ($content,"{SIDEBAR}",$exitstr);
}

?>
-----Original Message-----
From: Theodore Jones [mailto:theoj <email protected>]
Sent: September 26, 2000 09:59 AM
To: php-general <email protected>
Subject: [PHP] ### Image Display Problem ###

Hello,

I'm new to this list, so forigive a probably basic problem I present
here:

I'm trying to just iterate through a series of sub-directories of a main
directory. In each subdirectory there are just a set of image files.
All I would like to do is open each sub-directory, read the first image
name from that directory, and display that image, then go to the next
directory. For some reason I cannot get past a certain stage of just
throwing out all image-names per directory. Here is what I'm working
with so far:

<?
  $path = '/home/sites/site10/web/members/gallery/thumbs_new';
  $dir_handle =  <email protected>($path) or die("Unable to open $path");
  while ($file = readdir($dir_handle)) {
   if ($file == "." or $file == "..") {
    // do nothing
   } else {
    $thumb_dir = '/home/sites/site10/web/ft_members/video/thumbs_new/' .
$file;
    $directory =  <email protected>($thumb_dir) or die("Unable to open
$thumb_dir");
    while ($file = readdir($directory)) {
     if ($file == "." or $file == "..") {
      // do nothing
     } else{
      echo "$file <BR>";
     }
    }
    closedir($directory);
    echo "<P>";
   }
  }
  closedir($dir_handle);
  ?>

I think I need to create an array of the images as I iterate through
each sub-directory so that I can just display one image per directory,
but I'm at a loss as to where to nest this in and how?

Any comments?

~ Theo

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: php-general-unsubscribe <email protected>
For additional commands, e-mail: php-general-help <email protected>
To contact the list administrators, e-mail: php-list-admin <email protected>

-- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: php-general-unsubscribe <email protected> For additional commands, e-mail: php-general-help <email protected> To contact the list administrators, e-mail: php-list-admin <email protected>