Changeset 284


Ignore:
Timestamp:
Jan 17, 2004, 7:13:49 PM (20 years ago)
Author:
z0rglub
Message:

Using a one shot function to retrieve all directories : faster

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/release-1_3/admin/create_listing_file.php

    r161 r284  
    1010 ***************************************************************************/
    1111
    12 $prefix_thumbnail = 'TN-';
    13        
     12$conf['prefix_thumbnail'] = 'TN-';
    1413$conf['picture_ext'] = array ( 'jpg', 'gif', 'png', 'JPG', 'GIF', 'PNG' );
    1514
     
    2120
    2221$listing.= '<url>'.$url.'</url>';
    23        
     22
     23/**
     24 * returns an array with all picture files according to $conf['picture_ext']
     25 *
     26 * @param string $dir
     27 * @return array
     28 */
     29function get_picture_files( $dir )
     30{
     31  global $conf;
     32
     33  $pictures = array();
     34  if ( $opendir = opendir( $dir ) )
     35  {
     36    while ( $file = readdir( $opendir ) )
     37    {
     38      if ( in_array( get_extension( $file ), $conf['picture_ext'] ) )
     39      {
     40        array_push( $pictures, $file );
     41      }
     42    }
     43  }
     44  return $pictures;
     45}
     46
     47/**
     48 * returns an array with all thumbnails according to $conf['picture_ext']
     49 * and $conf['prefix_thumbnail']
     50 *
     51 * @param string $dir
     52 * @return array
     53 */
     54function get_thumb_files( $dir )
     55{
     56  global $conf;
     57
     58  $prefix_length = strlen( $conf['prefix_thumbnail'] );
     59 
     60  $thumbnails = array();
     61  if ( $opendir = @opendir( $dir ) )
     62  {
     63    while ( $file = readdir( $opendir ) )
     64    {
     65      if ( in_array( get_extension( $file ), $conf['picture_ext'] )
     66           and substr($file,0,$prefix_length) == $conf['prefix_thumbnail'] )
     67      {
     68        array_push( $thumbnails, $file );
     69      }
     70    }
     71  }
     72  return $thumbnails;
     73}
     74
    2475// get_dirs retourne un tableau contenant tous les sous-répertoires d'un
    2576// répertoire
    26 function get_dirs( $rep, $indent, $level )
     77function get_dirs( $basedir, $indent, $level )
    2778{
    28   $sub_rep = array();
    29   $i = 0;
     79  $fs_dirs = array();
    3080  $dirs = "";
    31   if ( $opendir = opendir ( $rep ) )
     81
     82  if ( $opendir = opendir( $basedir ) )
    3283  {
    33     while ( $file = readdir ( $opendir ) )
     84    while ( $file = readdir( $opendir ) )
    3485    {
    35       if ( $file != "."
    36            and $file != ".."
    37            and is_dir ( $rep."/".$file )
    38            and $file != "thumbnail" )
     86      if ( $file != '.'
     87           and $file != '..'
     88           and is_dir ( $basedir.'/'.$file )
     89           and $file != 'thumbnail' )
    3990      {
    40         $sub_rep[$i++] = $file;
     91        array_push( $fs_dirs, $file );
    4192        if ( !preg_match( '/^[a-zA-Z0-9-_.]+$/', $file ) )
    4293        {
     
    50101  }
    51102  // write of the dirs
    52   for ( $i = 0; $i < sizeof( $sub_rep ); $i++ )
    53   {
    54     $dirs.= "\n".$indent.'<dir'.$level.' name="'.$sub_rep[$i].'">';
    55     $dirs.= get_pictures( $rep.'/'.$sub_rep[$i], $indent.'  ' );
    56     $dirs.= get_dirs( $rep.'/'.$sub_rep[$i], $indent.'  ', $level + 1 );
     103  foreach ( $fs_dirs as $fs_dir ) {
     104    $dirs.= "\n".$indent.'<dir'.$level.' name="'.$fs_dir.'">';
     105    $dirs.= get_pictures( $basedir.'/'.$fs_dir, $indent.'  ' );
     106    $dirs.= get_dirs( $basedir.'/'.$fs_dir, $indent.'  ', $level + 1 );
    57107    $dirs.= "\n".$indent.'</dir'.$level.'>';
    58108  }
     
    74124}
    75125
    76 function is_image( $filename )
     126function get_pictures( $dir, $indent )
    77127{
    78128  global $conf;
     129 
     130  // fs means filesystem : $fs_pictures contains pictures in the filesystem
     131  // found in $dir, $fs_thumbnails contains thumbnails...
     132  $fs_pictures   = get_picture_files( $dir );
     133  $fs_thumbnails = get_thumb_files( $dir.'/thumbnail' );
    79134
    80   if ( !is_dir( $filename )
    81        and in_array( get_extension( $filename ), $conf['picture_ext'] ) )
    82   {
    83     return true;
    84   }
    85   return false;
    86 }
     135  $root = "\n".$indent.'<root>';
    87136
    88 function TN_exists( $dir, $file )
    89 {
    90   global $conf, $prefix_thumbnail;
     137  foreach ( $fs_pictures as $fs_picture ) {
     138    $file_wo_ext = get_filename_wo_extension( $fs_picture );
     139    $tn_ext = '';
     140    foreach ( $conf['picture_ext'] as $ext ) {
     141      $test = $conf['prefix_thumbnail'].$file_wo_ext.'.'.$ext;
     142      if ( !in_array( $test, $fs_thumbnails ) ) continue;
     143      else { $tn_ext = $ext; break; }
     144    }
     145    // if we found a thumnbnail corresponding to our picture...
     146    if ( $tn_ext != '' )
     147    {
     148      list( $width,$height ) = @getimagesize( $dir.'/'.$fs_picture );
    91149
    92   $titre = get_filename_wo_extension( $file );
    93 
    94   for ( $i = 0; $i < sizeof ( $conf['picture_ext'] ); $i++ )
    95   {
    96     $base_tn_name = $dir.'/thumbnail/'.$prefix_thumbnail.$titre.'.';
    97     $ext = $conf['picture_ext'][$i];
    98     if ( is_file( $base_tn_name.$ext ) )
     150      $root.= "\n".$indent.'  ';
     151      $root.= '<picture';
     152      $root.= ' file="'.    $fs_picture.'"';
     153      $root.= ' tn_ext="'.  $tn_ext.'"';
     154      $root.= ' filesize="'.floor(filesize($dir.'/'.$fs_picture)/1024).'"';
     155      $root.= ' width="'.   $width.'"';
     156      $root.= ' height="'.  $height.'"';
     157      $root.= ' />';
     158     
     159      if ( !preg_match( '/^[a-zA-Z0-9-_.]+$/', $fs_picture ) )
     160      {
     161        echo '<span style="color:red;">"'.$fs_picture.'" : ';
     162        echo 'The name of the picture should be composed of ';
     163        echo 'letters, figures, "-", "_" or "." ONLY';
     164        echo '</span><br />';
     165      }
     166    }
     167    else
    99168    {
    100       return $ext;
     169      echo 'The thumbnail is missing for '.$dir.'/'.$fs_picture;
     170      echo '-> '.$dir.'/thumbnail/';
     171      echo $conf['prefix_thumbnail'].$file_wo_ext.'.xxx';
     172      echo ' ("xxx" can be : ';
     173      echo implode( ', ', $conf['picture_ext'] );
     174      echo ')<br />';
    101175    }
    102176  }
    103   echo 'The thumbnail is missing for '.$dir.'/'.$file;
    104   echo '-> '.$dir.'/thumbnail/'.$prefix_thumbnail.$titre.'.xxx';
    105   echo ' ("xxx" can be : ';
    106   for ( $i = 0; $i < sizeof ( $conf['picture_ext'] ); $i++ )
    107   {
    108     if ( $i > 0 )
    109     {
    110       echo ', ';
    111     }
    112     echo '"'.$conf['picture_ext'][$i].'"';
    113   }
    114   echo ')<br />';
    115   return false;
    116 }
    117177
    118 function get_pictures( $rep, $indent )
    119 {
    120   $pictures = array();         
     178  $root.= "\n".$indent.'</root>';
    121179
    122   $tn_ext = '';
    123   $root = '';
    124   if ( $opendir = opendir ( $rep ) )
    125   {
    126     while ( $file = readdir ( $opendir ) )
    127     {
    128       if ( is_image( $file ) and $tn_ext = TN_exists( $rep, $file ) )
    129       {
    130         $picture = array();
    131 
    132         $picture['file']     = $file;
    133         $picture['tn_ext']   = $tn_ext;
    134         $picture['date']     = date('Y-m-d',filemtime( $rep.'/'.$file ) );
    135         $picture['filesize'] = floor( filesize( $rep."/".$file ) / 1024 );
    136         $image_size = @getimagesize( $rep."/".$file );
    137         $picture['width']    = $image_size[0];
    138         $picture['height']   = $image_size[1];
    139 
    140         array_push( $pictures, $picture );
    141 
    142         if ( !preg_match( '/^[a-zA-Z0-9-_.]+$/', $file ) )
    143         {
    144           echo '<span style="color:red;">"'.$file.'" : ';
    145           echo 'The name of the picture should be composed of ';
    146           echo 'letters, figures, "-", "_" or "." ONLY';
    147           echo '</span><br />';
    148         }
    149       }
    150     }
    151   }
    152   // write of the node <root> with all the pictures at the root of the
    153   // directory
    154   $root.= "\n".$indent."<root>";
    155   if ( sizeof( $pictures ) > 0 )
    156   {
    157     for( $i = 0; $i < sizeof( $pictures ); $i++ )
    158     {
    159       $root.= "\n".$indent.'  ';
    160       $root.= '<picture';
    161       $root.= ' file="'.     $pictures[$i]['file'].     '"';
    162       $root.= ' tn_ext="'.   $pictures[$i]['tn_ext'].   '"';
    163       $root.= ' date="'.     $pictures[$i]['date'].     '"';
    164       $root.= ' filesize="'. $pictures[$i]['filesize']. '"';
    165       $root.= ' width="'.    $pictures[$i]['width'].    '"';
    166       $root.= ' height="'.   $pictures[$i]['height'].   '"';
    167       $root.= ' />';
    168     }
    169   }
    170   $root.= "\n".$indent.'</root>';
    171180  return $root;
    172181}
     
    178187  fwrite( $fp, $listing );
    179188  fclose( $fp );
     189  echo "listing.xml created";
    180190}
    181191else
     
    183193  echo "I can't write the file listing.xml";
    184194}
    185 
    186 echo "listing.xml created";
    187195?>
Note: See TracChangeset for help on using the changeset viewer.