Ignore:
Timestamp:
Jan 16, 2005, 12:13:30 PM (19 years ago)
Author:
plg
Message:
  • big update in thumbnail creation process : form is always shown with the comprehensive list of pictures without thumbnails (no matter the directory) unless there are no thumbnail missing.
  • thumbnail creation process : form parameters are saved between 2 thumbnail generations thanks to new process
  • thumbnail creation process : can ask to create all missing thumbnails
  • thumbnail creation process : default thumbnail width and height are configured in include/config.inc.php
  • thumbnail creation process : errors due to no write access directories are catched
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/admin/thumbnail.php

    r675 r695  
    2727include_once( PHPWG_ROOT_PATH.'admin/include/isadmin.inc.php' );
    2828//------------------------------------------------------------------- functions
    29 // get_subdirs returns an array containing all sub directory names,
    30 // excepting : '.', '..' and 'thumbnail'.
    31 function get_subdirs( $dir )
    32 {
    33   $sub_dirs = array();
    34   if ( $opendir = opendir( $dir ) )
    35   {
    36     while ( $file = readdir( $opendir ) )
    37     {
    38       if ($file != 'thumbnail'
    39           and $file != 'pwg_representative'
    40           and $file != 'pwg_high'
    41           and $file != '.'
    42           and $file != '..'
    43           and is_dir($dir.'/'.$file))
    44       {
    45         array_push( $sub_dirs, $file );
    46       }
    47     }
    48   }
    49   return $sub_dirs;
    50 }
    51 
    52 // get_images_without_thumbnail returns an array with all the picture names
    53 // that don't have associated thumbnail in the directory. Each picture name
    54 // is associated with the width, heigh and filesize of the picture.
    55 function get_images_without_thumbnail( $dir )
    56 {
    57   $images = array();
    58   if ( $opendir = opendir( $dir ) )
    59   {
    60     while ( $file = readdir( $opendir ) )
    61     {
    62       $path = $dir.'/'.$file;
    63       if ( is_image( $path, true ) )
    64       {
    65         if ( !TN_exists( $dir, $file ) )
    66         {
    67           $image_infos = getimagesize( $path );
    68           $size = floor( filesize( $path ) / 1024 ). ' KB';
    69           array_push( $images, array( 'name' => $file,
    70                                       'width' => $image_infos[0],
    71                                       'height' => $image_infos[1],
    72                                       'size' => $size ) );
    73         }
    74       }
    75     }
    76   }
    77   return $images;
    78 }
    79 
    80 // phpwg_scandir scans a dir to find pictures without thumbnails. Once found,
    81 // creation of the thumbnails (RatioResizeImg). Only the first $_POST['n']
    82 // pictures without thumbnails are treated.
    83 // scandir returns an array with the generation time of each thumbnail (for
    84 // statistics purpose)
    85 function phpwg_scandir( $dir, $width, $height )
    86 {
    87   global $conf;
    88   $stats = array();
    89   if ( $opendir = opendir( $dir ) )
    90   {
    91     while ( $file = readdir ( $opendir ) )
    92     {
    93       $path = $dir.'/'.$file;
    94       if ( is_image( $path, true ) )
    95       {
    96         if ( count( $stats ) < $_POST['n'] and !TN_exists( $dir, $file ) )
    97         {
    98           $starttime = get_moment();
    99           $info = RatioResizeImg( $file, $width, $height, $dir.'/', 'jpg' );
    100           $endtime = get_moment();
    101           $info['time'] = ( $endtime - $starttime ) * 1000;
    102           array_push( $stats, $info );
    103         }
    104       }
    105     }
    106   }
    107   return $stats;
    108 }
    109 
    11029// RatioResizeImg creates a new picture (a thumbnail since it is supposed to
    11130// be smaller than original picture !) in the sub directory named
    11231// "thumbnail".
    113 function RatioResizeImg( $filename, $newWidth, $newHeight, $path, $tn_ext )
    114 {
    115   global $conf, $lang;
    116   // full path to picture
    117   $filepath = $path.$filename;
     32function RatioResizeImg($path, $newWidth, $newHeight, $tn_ext)
     33{
     34  global $conf, $lang, $errors;
     35
     36  $filename = basename($path);
     37  $dirname = dirname($path);
     38 
    11839  // extension of the picture filename
    119   $extension = get_extension( $filepath );
    120   switch( $extension )
    121   {
    122   case 'jpg': $srcImage = @imagecreatefromjpeg( $filepath ); break;
    123   case 'JPG': $srcImage = @imagecreatefromjpeg( $filepath ); break;
    124   case 'png': $srcImage = @imagecreatefrompng(  $filepath ); break;
    125   case 'PNG': $srcImage = @imagecreatefrompng(  $filepath ); break;
    126   default : unset( $extension ); break;
     40  $extension = get_extension($filename);
     41
     42  if ($extension == 'jpg' or $extension == 'JPG')
     43  {
     44    $srcImage = @imagecreatefromjpeg($path);
     45  }
     46  else if ($extension == 'png' or $extension == 'PNG')
     47  {
     48    $srcImage = @imagecreatefrompng($path);
     49  }
     50  else
     51  {
     52    unset($extension);
    12753  }
    12854               
     
    16995                        $destWidth,$destHeight,$srcWidth,$srcHeight );
    17096    }
    171                        
    172                        
    173     if( !is_dir( $path.'thumbnail' ) )
    174     {
    175       umask( 0000 );
    176       mkdir( $path.'thumbnail', 0777 );
    177     }
    178     $dest_file = $path.'thumbnail/'.$conf['prefix_thumbnail'];
    179     $dest_file.= get_filename_wo_extension( $filename );
     97   
     98    $tndir = $dirname.'/thumbnail';
     99    if (!is_dir($tndir))
     100    {
     101      if (!is_writable($dirname))
     102      {
     103        array_push($errors, '['.$dirname.'] : '.$lang['no_write_access']);
     104        return false;
     105      }
     106      umask(0000);
     107      mkdir($tndir, 0777);
     108    }
     109   
     110    $dest_file = $tndir.'/'.$conf['prefix_thumbnail'];
     111    $dest_file.= get_filename_wo_extension($filename);
    180112    $dest_file.= '.'.$tn_ext;
    181                        
     113   
    182114    // creation and backup of final picture
    183     imagejpeg( $destImage, $dest_file );
     115    if (!is_writable($tndir))
     116    {
     117      array_push($errors, '['.$tndir.'] : '.$lang['no_write_access']);
     118      return false;
     119    }
     120    imagejpeg($destImage, $dest_file);
    184121    // freeing memory ressources
    185122    imagedestroy( $srcImage );
    186123    imagedestroy( $destImage );
    187                        
    188     list( $width,$height ) = getimagesize( $filepath );
    189     $size = floor( filesize( $filepath ) / 1024 ).' KB';
    190     list( $tn_width,$tn_height ) = getimagesize( $dest_file );
    191     $tn_size = floor( filesize( $dest_file ) / 1024 ).' KB';
    192     $info = array( 'file'      => $filename,
    193                    'width'     => $width,
    194                    'height'    => $height,
    195                    'size'      => $size,
     124   
     125    list($tn_width, $tn_height) = getimagesize($dest_file);
     126    $tn_size = floor(filesize($dest_file) / 1024).' KB';
     127   
     128    $info = array( 'path'      => $path,
    196129                   'tn_file'   => $dest_file,
    197130                   'tn_width'  => $tn_width,
     
    219152$pictures = array();
    220153$stats = array();
    221 
    222 if ( isset( $_GET['dir'] ) &&  isset( $_POST['submit'] ))
    223 {
    224   $pictures = get_images_without_thumbnail( $_GET['dir'] );
    225   // checking criteria
    226   if ( !ereg( "^[0-9]{2,3}$", $_POST['width'] ) or $_POST['width'] < 10 )
    227   {
    228     array_push( $errors, $lang['tn_err_width'].' 10' );
    229   }
    230   if ( !ereg( "^[0-9]{2,3}$", $_POST['height'] ) or $_POST['height'] < 10 )
    231   {
    232     array_push( $errors, $lang['tn_err_height'].' 10' );
    233   }
    234  
    235   // picture miniaturization
    236   if ( count( $errors ) == 0 )
    237   {
    238     $stats = phpwg_scandir( $_GET['dir'], $_POST['width'], $_POST['height'] );
    239   }
    240 }
    241                
    242 //----------------------------------------------------- template initialization
     154// +-----------------------------------------------------------------------+
     155// |                       template initialization                         |
     156// +-----------------------------------------------------------------------+
    243157$template->set_filenames( array('thumbnail'=>'admin/thumbnail.tpl') );
    244158
    245159$template->assign_vars(array(
    246160  'L_THUMBNAIL_TITLE'=>$lang['tn_dirs_title'],
    247   'L_UNLINK'=>$lang['tn_dirs_alone'],
     161  'L_UNLINK'=>$lang['tn_no_missing'],
    248162  'L_MISSING_THUMBNAILS'=>$lang['tn_dirs_alone'],
    249163  'L_RESULTS'=>$lang['tn_results_title'],
    250   'L_TN_PICTURE'=>$lang['tn_picture'],
     164  'L_PATH'=>$lang['path'],
    251165  'L_FILESIZE'=>$lang['filesize'],
    252166  'L_WIDTH'=>$lang['tn_width'],
     
    274188  'T_STYLE'=>$user['template']
    275189  ));
    276 
    277 //----------------------------------------------------- miniaturization results
    278 if ( sizeof( $errors ) != 0 )
     190// +-----------------------------------------------------------------------+
     191// |                   search pictures without thumbnails                  |
     192// +-----------------------------------------------------------------------+
     193$wo_thumbnails = array();
     194$thumbnalized = array();
     195
     196// what is the directory to search in ?
     197$query = '
     198SELECT galleries_url
     199  FROM '.SITES_TABLE.'
     200  WHERE id = 1
     201;';
     202list($galleries_url) = mysql_fetch_array(pwg_query($query));
     203$basedir = preg_replace('#/*$#', '', $galleries_url);
     204
     205$fs = get_fs($basedir);
     206// because isset is one hundred time faster than in_array
     207$fs['thumbnails'] = array_flip($fs['thumbnails']);
     208
     209foreach ($fs['elements'] as $path)
     210{
     211  // only pictures need thumbnails
     212  if (in_array(get_extension($path), $conf['picture_ext']))
     213  {
     214    $dirname = dirname($path);
     215    $filename = basename($path);
     216    // searching the element
     217    $filename_wo_ext = get_filename_wo_extension($filename);
     218    $tn_ext = '';
     219    $base_test = $dirname.'/thumbnail/';
     220    $base_test.= $conf['prefix_thumbnail'].$filename_wo_ext.'.';
     221    foreach ($conf['picture_ext'] as $ext)
     222    {
     223      if (isset($fs['thumbnails'][$base_test.$ext]))
     224      {
     225        $tn_ext = $ext;
     226        break;
     227      }
     228    }
     229   
     230    if (empty($tn_ext))
     231    {
     232      array_push($wo_thumbnails, $path);
     233    }
     234  }
     235}
     236// +-----------------------------------------------------------------------+
     237// |                         thumbnails creation                           |
     238// +-----------------------------------------------------------------------+
     239if (isset($_POST['submit']))
     240{
     241  $errors = array();
     242  $times = array();
     243  $infos = array();
     244 
     245  // checking criteria
     246  if (!ereg('^[0-9]{2,3}$', $_POST['width']) or $_POST['width'] < 10)
     247  {
     248    array_push($errors, $lang['tn_err_width'].' 10');
     249  }
     250  if (!ereg('^[0-9]{2,3}$', $_POST['height']) or $_POST['height'] < 10)
     251  {
     252    array_push($errors, $lang['tn_err_height'].' 10');
     253  }
     254 
     255  // picture miniaturization
     256  if (count($errors) == 0)
     257  {
     258    $num = 1;
     259    foreach ($wo_thumbnails as $path)
     260    {
     261      if (is_numeric($_POST['n']) and $num > $_POST['n'])
     262      {
     263        break;
     264      }
     265     
     266      $starttime = get_moment();
     267      if ($info = RatioResizeImg($path,$_POST['width'],$_POST['height'],'jpg'))
     268      {
     269        $endtime = get_moment();
     270        $info['time'] = ($endtime - $starttime) * 1000;
     271        array_push($infos, $info);
     272        array_push($times, $info['time']);
     273        array_push($thumbnalized, $path);
     274        $num++;
     275      }
     276      else
     277      {
     278        break;
     279      }
     280    }
     281
     282    if (count($infos) > 0)
     283    {
     284      $sum = array_sum($times);
     285      $average = $sum / count($times);
     286      sort($times, SORT_NUMERIC);
     287      $max = array_pop($times);
     288      if (count($thumbnalized) == 1)
     289      {
     290        $min = $max;
     291      }
     292      else
     293      {
     294        $min = array_shift($times);
     295      }
     296     
     297      $template->assign_block_vars(
     298        'results',
     299        array(
     300          'TN_NB'=>count($infos),
     301          'TN_TOTAL'=>number_format($sum, 2, '.', ' ').' ms',
     302          'TN_MAX'=>number_format($max, 2, '.', ' ').' ms',
     303          'TN_MIN'=>number_format($min, 2, '.', ' ').' ms',
     304          'TN_AVERAGE'=>number_format($average, 2, '.', ' ').' ms'
     305          ));
     306     
     307      foreach ($infos as $i => $info)
     308      {
     309        if ($info['time'] == $max)
     310        {
     311          $class = 'worst_gen_time';
     312        }
     313        else if ($info['time'] == $min)
     314        {
     315          $class = 'best_gen_time';
     316        }
     317        else
     318        {
     319          $class = '';
     320        }
     321       
     322        $template->assign_block_vars(
     323          'results.picture',
     324          array(
     325            'PATH'=>$info['path'],
     326            'TN_FILE_IMG'=>$info['tn_file'],
     327            'TN_FILESIZE_IMG'=>$info['tn_size'],
     328            'TN_WIDTH_IMG'=>$info['tn_width'],
     329            'TN_HEIGHT_IMG'=>$info['tn_height'],
     330            'GEN_TIME'=>number_format($info['time'], 2, '.', ' ').' ms',
     331           
     332            'T_CLASS'=>$class
     333            ));
     334      }
     335    }
     336  }
     337}
     338// +-----------------------------------------------------------------------+
     339// |                            errors display                             |
     340// +-----------------------------------------------------------------------+
     341if (count($errors) != 0)
    279342{
    280343  $template->assign_block_vars('errors',array());
    281   for ( $i = 0; $i < sizeof( $errors ); $i++ )
    282   {
    283     $template->assign_block_vars('errors.error',array('ERROR'=>$errors[$i]));
    284   }
    285 }
    286 else if (isset($_GET['dir']) and isset($_POST['submit']) and !empty($stats))
    287 {
    288   $times = array();
    289   foreach ($stats as $stat)
    290   {
    291     array_push( $times, $stat['time'] );
    292   }
    293   $sum=array_sum( $times );
    294   $average = $sum/sizeof($times);
    295   sort( $times, SORT_NUMERIC );
    296   $max = array_pop($times);
    297   $min = array_shift( $times);
    298  
    299   $template->assign_block_vars(
    300     'results',
    301     array(
    302       'TN_NB'=>count( $stats ),
    303       'TN_TOTAL'=>number_format( $sum, 2, '.', ' ').' ms',
    304       'TN_MAX'=>number_format( $max, 2, '.', ' ').' ms',
    305       'TN_MIN'=>number_format( $min, 2, '.', ' ').' ms',
    306       'TN_AVERAGE'=>number_format( $average, 2, '.', ' ').' ms'
    307       ));
    308   if (!count($pictures))
    309   {
    310     $template->assign_block_vars('warning',array());
    311   }
    312  
    313   foreach ($stats as $i => $stat)
    314   {
    315     $class = ($i % 2)? 'row1':'row2';
    316     $color='';
    317     if ($stat['time'] == $max)
    318     {
    319       $color = 'red';
    320     }
    321     else if ($stat['time'] == $min)
    322     {
    323       $color = '#33FF00';
    324     }
    325    
    326     $template->assign_block_vars(
    327       'results.picture',
    328       array(
    329         'NB_IMG'=>($i+1),
    330         'FILE_IMG'=>$stat['file'],
    331         'FILESIZE_IMG'=>$stat['size'],
    332         'WIDTH_IMG'=>$stat['width'],
    333         'HEIGHT_IMG'=>$stat['height'],
    334         'TN_FILE_IMG'=>$stat['tn_file'],
    335         'TN_FILESIZE_IMG'=>$stat['tn_size'],
    336         'TN_WIDTH_IMG'=>$stat['tn_width'],
    337         'TN_HEIGHT_IMG'=>$stat['tn_height'],
    338         'GEN_TIME'=>number_format( $stat['time'], 2, '.', ' ').' ms',
    339        
    340         'T_COLOR'=>$color,
    341         'T_CLASS'=>$class
    342         ));
    343   }
    344 }
    345 //-------------------------------------------------- miniaturization parameters
    346 if (isset($_GET['dir']) and !sizeof($pictures))
    347 {
    348   $form_url = PHPWG_ROOT_PATH.'admin.php?page=thumbnail&amp;dir='.$_GET['dir'];
    349   $gd = !empty( $_POST['gd'] )?$_POST['gd']:2;
    350   $width = !empty( $_POST['width'] )?$_POST['width']:128;
    351   $height = !empty( $_POST['height'] )?$_POST['height']:96;
     344  foreach ($errors as $error)
     345  {
     346    $template->assign_block_vars('errors.error',array('ERROR'=>$error));
     347  }
     348}
     349// +-----------------------------------------------------------------------+
     350// |             form & pictures without thumbnails display                |
     351// +-----------------------------------------------------------------------+
     352$remainings = array_diff($wo_thumbnails, $thumbnalized);
     353
     354if (count($remainings) > 0)
     355{
     356  $form_url = PHPWG_ROOT_PATH.'admin.php?page=thumbnail';
     357  $gd = !empty($_POST['gd']) ? $_POST['gd'] : 2;
     358  $width = !empty($_POST['width']) ? $_POST['width'] : $conf['tn_width'];
     359  $height = !empty($_POST['height']) ? $_POST['height'] : $conf['tn_height'];
     360  $n = !empty($_POST['n']) ? $_POST['n'] : 5;
     361 
    352362  $gdlabel = 'GD'.$gd.'_CHECKED';
     363  $nlabel = 'n_'.$n.'_CHECKED';
    353364 
    354365  $template->assign_block_vars(
     
    357368      'F_ACTION'=>add_session_id($form_url),
    358369      $gdlabel=>'checked="checked"',
     370      $nlabel=>'checked="checked"',
    359371      'WIDTH_TN'=>$width,
    360372      'HEIGHT_TN'=>$height
    361373      ));
    362 //---------------------------------------------------------- remaining pictures
    363   $pictures = get_images_without_thumbnail( $_GET['dir'] );
     374
    364375  $template->assign_block_vars(
    365376    'remainings',
    366     array('TOTAL_IMG'=>count($pictures)));
    367 
    368   foreach ($pictures as $i => $picture)
    369   {
    370     $class = ($i % 2)? 'row1':'row2';
     377    array('TOTAL_IMG'=>count($remainings)));
     378
     379  $num = 1;
     380  foreach ($remainings as $path)
     381  {
     382    $class = ($num % 2) ? 'row1' : 'row2';
     383    list($width, $height) = getimagesize($path);
     384    $size = floor(filesize($path) / 1024).' KB';
     385
    371386    $template->assign_block_vars(
    372387      'remainings.remaining',
    373388      array(
    374         'NB_IMG'=>($i+1),
    375         'FILE_TN'=>$picture['name'],
    376         'FILESIZE_IMG'=>$picture['size'],
    377         'WIDTH_IMG'=>$picture['width'],
    378         'HEIGHT_IMG'=>$picture['height'],
    379          
     389        'NB_IMG'=>($num),
     390        'PATH'=>$path,
     391        'FILESIZE_IMG'=>$size,
     392        'WIDTH_IMG'=>$width,
     393        'HEIGHT_IMG'=>$height,
     394       
    380395        'T_CLASS'=>$class
    381396        ));
    382   }
    383 }
    384 //-------------------------------------------------------------- directory list
     397
     398    $num++;
     399  }
     400}
    385401else
    386402{
    387   $wo_thumbnails = array();
    388  
    389   // what is the directory to search in ?
    390   $query = '
    391 SELECT galleries_url
    392   FROM '.SITES_TABLE.'
    393   WHERE id = 1
    394 ;';
    395   list($galleries_url) = mysql_fetch_array(pwg_query($query));
    396   $basedir = preg_replace('#/*$#', '', $galleries_url);
    397 
    398   $fs = get_fs($basedir);
    399   // because isset is one hundred time faster than in_array
    400   $fs['thumbnails'] = array_flip($fs['thumbnails']);
    401 
    402   foreach ($fs['elements'] as $path)
    403   {
    404     // only pictures need thumbnails
    405     if (in_array(get_extension($path), $conf['picture_ext']))
    406     {
    407       $dirname = dirname($path);
    408       $filename = basename($path);
    409       // searching the element
    410       $filename_wo_ext = get_filename_wo_extension($filename);
    411       $tn_ext = '';
    412       $base_test = $dirname.'/thumbnail/';
    413       $base_test.= $conf['prefix_thumbnail'].$filename_wo_ext.'.';
    414       foreach ($conf['picture_ext'] as $ext)
    415       {
    416         if (isset($fs['thumbnails'][$base_test.$ext]))
    417         {
    418           $tn_ext = $ext;
    419           break;
    420         }
    421       }
    422      
    423       if (empty($tn_ext))
    424       {
    425         if (!isset($wo_thumbnails[$dirname]))
    426         {
    427           $wo_thumbnails[$dirname] = 1;
    428         }
    429         else
    430         {
    431           $wo_thumbnails[$dirname]++;
    432         }
    433       }
    434     }
    435   }
    436 
    437   if (count($wo_thumbnails) > 0)
    438   {
    439     $template->assign_block_vars('directory_list', array());
    440     foreach ($wo_thumbnails as $directory => $nb_missing)
    441     {
    442       $url = PHPWG_ROOT_PATH.'admin.php?page=thumbnail&amp;dir='.$directory;
    443      
    444       $template->assign_block_vars(
    445         'directory_list.directory',
    446         array(
    447           'DIRECTORY'=>$directory,
    448           'URL'=>add_session_id($url),
    449           'NB_MISSING'=>$nb_missing));
    450     }
    451   }
    452 }
     403  $template->assign_block_vars('warning', array());
     404}
     405// +-----------------------------------------------------------------------+
     406// |                           return to admin                             |
     407// +-----------------------------------------------------------------------+
    453408$template->assign_var_from_handle('ADMIN_CONTENT', 'thumbnail');
    454409?>
Note: See TracChangeset for help on using the changeset viewer.