Zend - The PHP Company




Thumbnails

Add Code


generic thumbnail directory browser  

Type: code fragment
Added by: portasio
Entered: 04/01/2001
Last modified: 01/12/2000
Rating: **** (7 votes)
Views: 14548
browse your image directories with proportional thumbnails. customize thumbnail size and number of columns. click on images for full size view. generates indented html table code. if you modify this code in any way, pls send me a copy of the enhanced code.


<?

# imgdir.php ver 0.09
#
# browse image directory function
#
# generates html table with images and
# links to the actual filenames
#
# jan/2001 - mauricio portasio - portasio@zaz.com.br
# pls keep this header block when using these funcs

# no support is provided, but please send
# suggestions or bug reports.
# tested on: 
# Apache/1.3.14 (Win98 4.1) PHP/4.04
# Apache/1.3.12 (Unix) PHP/4.0.3pl1 

# load html tag definitions
# require ("htmldefs.inc");
# suggestion: move defines below to htmldefs.inc and uncomment line above

define ("LINE_BREAK""<br>");
define ("TABLE_START""<table width=600>n");
define ("TABLE_END""</table>n");
define ("ROW_START""  <tr>n");
define ("ROW_END""  </tr>n");
define ("COL_START""   <td align=center>n      ");
define ("COL_END""n   </td>n");
define ("IMG_START""<img src=");
define ("IMG_END"">");
define ("IMG_WIDTH"" width=");
define ("IMG_HEIGHT"" height=");
define ("A_START"'<a href="');
define ("A_CLOSE"'">');
define ("A_END""</a>");


# dirToArray
#
# directory to array extractor
# $browsedir (string - path name)
# $extentions (array -  extensions to be listed)

function dirToArray ($browsedir$extensions)
{

  
$MAXDIRSIZE 200;

  
$nextensions sizeof ($extensions);
  
$idirectory 0;
  
$directory dir ($browsedir);
  
  while (
$entry $directory->read())
  {
      for (
$i=1$i<=$nextensions$i++)
      {
         
$compare stristr ($entry$extensions[$i]);
         if (
strlen($compare) == strlen($extensions[$i]))
         {
            
$adirectory[++$idirectory] = $entry;
            break;
         }
      }
  }
  
$directory->close();
  return 
$adirectory;
}


# showDir
#
# show table with images and links
# $browsedir (string - path name)
# $adirectory (array -  filenames w/o path)

function showDir ($adirectory)

{

  global 
$browsedir;

  
# change layout options here - should be parameters actually...
  
$maxcols 4;
  
$imagemaxwidth 80;
  
$imagemaxheight 60;
  
  
$imagemaxratio =  $imagemaxwidth $imagemaxheight;
  
  
$ndirectory sizeof ($adirectory);
  echo (
TABLE_START);
  for (
$i=0$i<=$ndirectory;)
  {
     echo (
ROW_START);
     for (
$icols=1$icols<=$maxcols$icols++)
     {
       echo (
COL_START);
       
$imagefilename $adirectory[++$i];
       if (
strlen($imagefilename)>0)
       {
         
$imagepath $browsedir."/".$imagefilename;
         
$imagesize GetImageSize ($imagepath);
         if (
$imagesize)
         {
           
$imagewidth $imagesize[0];
           
$imageheight $imagesize[1];
           
$imageratio $imagewidth $imageheight;
           if (
$imageratio $imagemaxratio)
           {
              
$imageoutputwidth $imagemaxwidth;
              
$imageoutputheight ceil ($imagemaxwidth/$imagewidth*$imageheight);
           }
           else if (
$imageratio $imagemaxratio)
           {
              
$imageoutputheight $imagemaxheight;
              
$imageoutputwidth ceil ($imagemaxheight/$imageheight*$imagewidth);
           } else
           {
              
$imageoutputwidth $imagemaxwidth;
              
$imageoutputheight $imagemaxheight;
           }
           echo (
A_START.$imagepath.A_CLOSE);
           echo (
IMG_START.$imagepath.IMG_WIDTH.$imageoutputwidth.IMG_HEIGHT.$imageoutputheight.IMG_END);
           echo (
LINE_BREAK.$adirectory[$i]);
           echo (
A_END);
         }
         echo (
COL_END);
       }
     }
     echo (
ROW_END);
  }
  echo (
TABLE_END);
}



# main - test

# list allowed extensions here
$extensions[1] = "jpeg";
$extensions[2] = "jpg";
$extensions[3] = "gif";

# directory to be browsed
$browsedir "images";

showDir dirToArray ($browsedir$extensions) );

?>


Usage Example




Rate This Script





Search



This Category All Categories