Zend - The PHP Company




Thumbnails

Add Code


Generic Thumbnail Browser II  

Type: code fragment
Added by: resjr
Entered: 18/02/2001
Last modified: 02/12/2000
Rating: - (fewer than 3 votes)
Views: 11494
This code is used to generate an image directory with proportional thumbnails. The number of images on the page can be set as well as the size of the thumbnails generated. Another feature is an automatically generated menu at the bottom of each page. This code is a spin-off of code originally submitted by mauricio portasio.


<? 

# imgdir.php ver 0.10 

# browse image directory function 

# generates html table with images and 
# links to the actual filenames 

# jan/2001 - mauricio portasio - portasio@zaz.com.br 
# feb/2001 - ralph smith - ralph.smith.1@excite.com
# pls keep this header block when using these funcs 

# HINT: When I set this code up, I used two files: 
# image.inc and images.php. The .inc file contains
# the htmldefs and the main functions that drive the code.
# Because my plan is to integrate this code into a website,
# having the functions and html tid-bits stored in the 
# include file make them less messy to deal with and I can 
# simply call to them in my .php file. The basic contents 
# of the .php look like this:

#include "image.inc";
# main - test 

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

# I hope that helps!.....RS...........................
# >>>>><<<<<<>>>>>>><<<<<<<>>>>>>>><<<<<<<<>>>>>>><<<<
# 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>"); 

<? 

# showDir 

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


function showDir ($adirectory$i



  global 
$browsedir

  
$start $i;

  
# change layout options here - should be parameters actually... 
  
$maxcols 3
  
$maximages 21;
  
$maximages $i + ($maximages 3);
  
$imagemaxwidth 80
  
$imagemaxheight 60
   
  
$imagemaxratio =  $imagemaxwidth $imagemaxheight
   
  
$ndirectory sizeof ($adirectory); 
  echo (
TABLE_START); 
  for (
$i$i<=$maximages;) 
  { 
     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); 

pagemenu ($browsedir$ndirectory$start);



function 
pagemenu ($browsedir$ndirectory$pg) {

print 
"<font face="VerdanaArialHelveticasans-serif" size="1" color="#000033">Page :";

$pg_num 1;

for (
$img_num 0$img_num <= $ndirectory;) {

    if (
$pg == $img_num):
        print 
"<span class="menulink_1"><a href="images_test.php?browsedir=$browsedir&start=$img_num">
        *
$pg_num</a> <span>";

    else:

    print 
"<span class="menulink_2"><a href="images_test.php?browsedir=$browsedir&start=$img_num">
        
$pg_num</a> <span>";

    endif;

    
$pg_num $pg_num 1;
    
$img_num $img_num 21;


}

print 
"</font>n";

}

# dirToArray 

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

function dirToArray ($browsedir$extensions


  
$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


# main - test 

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

showDir dirToArray ($browsedir$extensions), $start); 

?>


Usage Example




Rate This Script





Search



This Category All Categories