source: trunk/admin/thumbnail.php @ 504

Last change on this file since 504 was 504, checked in by gweltas, 20 years ago

PHP5 compatibility

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.3 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// |                             thumbnail.php                             |
4// +-----------------------------------------------------------------------+
5// | application   : PhpWebGallery <http://phpwebgallery.net>              |
6// | branch        : BSF (Best So Far)                                     |
7// +-----------------------------------------------------------------------+
8// | file          : $RCSfile$
9// | last update   : $Date: 2004-08-29 16:56:05 +0000 (Sun, 29 Aug 2004) $
10// | last modifier : $Author: gweltas $
11// | revision      : $Revision: 504 $
12// +-----------------------------------------------------------------------+
13// | This program is free software; you can redistribute it and/or modify  |
14// | it under the terms of the GNU General Public License as published by  |
15// | the Free Software Foundation                                          |
16// |                                                                       |
17// | This program is distributed in the hope that it will be useful, but   |
18// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
19// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
20// | General Public License for more details.                              |
21// |                                                                       |
22// | You should have received a copy of the GNU General Public License     |
23// | along with this program; if not, write to the Free Software           |
24// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
25// | USA.                                                                  |
26// +-----------------------------------------------------------------------+
27include_once( PHPWG_ROOT_PATH.'admin/include/isadmin.inc.php' );
28//------------------------------------------------------------------- functions
29// get_subdirs returns an array containing all sub directory names,
30// excepting : '.', '..' and 'thumbnail'.
31function get_subdirs( $dir )
32{
33  $sub_dirs = array();
34  if ( $opendir = opendir( $dir ) )
35  {
36    while ( $file = readdir( $opendir ) )
37    {
38      if ( $file != 'thumbnail' and $file != '.'
39           and $file != '..' and is_dir( $dir.'/'.$file ) )
40      {
41        array_push( $sub_dirs, $file );
42      }
43    }
44  }
45  return $sub_dirs;
46}
47
48// get_images_without_thumbnail returns an array with all the picture names
49// that don't have associated thumbnail in the directory. Each picture name
50// is associated with the width, heigh and filesize of the picture.
51function get_images_without_thumbnail( $dir )
52{
53  $images = array();
54  if ( $opendir = opendir( $dir ) )
55  {
56    while ( $file = readdir( $opendir ) )
57    {
58      $path = $dir.'/'.$file;
59      if ( is_image( $path, true ) )
60      {
61        if ( !TN_exists( $dir, $file ) )
62        {
63          $image_infos = getimagesize( $path );
64          $size = floor( filesize( $path ) / 1024 ). ' KB';
65          array_push( $images, array( 'name' => $file,
66                                      'width' => $image_infos[0],
67                                      'height' => $image_infos[1],
68                                      'size' => $size ) );
69        }
70      }
71    }
72  }
73  return $images;
74}
75
76// phpwg_scandir scans a dir to find pictures without thumbnails. Once found,
77// creation of the thumbnails (RatioResizeImg). Only the first $_POST['n']
78// pictures without thumbnails are treated.
79// scandir returns an array with the generation time of each thumbnail (for
80// statistics purpose)
81function phpwg_scandir( $dir, $width, $height )
82{
83  global $conf;
84  $stats = array();
85  if ( $opendir = opendir( $dir ) )
86  {
87    while ( $file = readdir ( $opendir ) )
88    {
89      $path = $dir.'/'.$file;
90      if ( is_image( $path, true ) )
91      {
92        if ( count( $stats ) < $_POST['n'] and !TN_exists( $dir, $file ) )
93        {
94          $starttime = get_moment();
95          $info = RatioResizeImg( $file, $width, $height, $dir.'/', 'jpg' );
96          $endtime = get_moment();
97          $info['time'] = ( $endtime - $starttime ) * 1000;
98          array_push( $stats, $info );
99        }
100      }
101    }
102  }
103  return $stats;
104}
105
106// RatioResizeImg creates a new picture (a thumbnail since it is supposed to
107// be smaller than original picture !) in the sub directory named
108// "thumbnail".
109function RatioResizeImg( $filename, $newWidth, $newHeight, $path, $tn_ext )
110{
111  global $conf, $lang;
112  // full path to picture
113  $filepath = $path.$filename;
114  // extension of the picture filename
115  $extension = get_extension( $filepath );
116  switch( $extension )
117  {
118  case 'jpg': $srcImage = @imagecreatefromjpeg( $filepath ); break; 
119  case 'JPG': $srcImage = @imagecreatefromjpeg( $filepath ); break; 
120  case 'png': $srcImage = @imagecreatefrompng(  $filepath ); break; 
121  case 'PNG': $srcImage = @imagecreatefrompng(  $filepath ); break; 
122  default : unset( $extension ); break;
123  }
124               
125  if ( isset( $srcImage ) )
126  {
127    // width/height
128    $srcWidth    = imagesx( $srcImage ); 
129    $srcHeight   = imagesy( $srcImage ); 
130    $ratioWidth  = $srcWidth/$newWidth;
131    $ratioHeight = $srcHeight/$newHeight;
132
133    // maximal size exceeded ?
134    if ( ( $ratioWidth > 1 ) or ( $ratioHeight > 1 ) )
135    {
136      if ( $ratioWidth < $ratioHeight)
137      { 
138        $destWidth = $srcWidth/$ratioHeight;
139        $destHeight = $newHeight; 
140      }
141      else
142      { 
143        $destWidth = $newWidth; 
144        $destHeight = $srcHeight/$ratioWidth;
145      }
146    }
147    else
148    {
149      $destWidth = $srcWidth;
150      $destHeight = $srcHeight;
151    }
152    // according to the GD version installed on the server
153    if ( $_POST['gd'] == 2 )
154    {
155      // GD 2.0 or more recent -> good results (but slower)
156      $destImage = imagecreatetruecolor( $destWidth, $destHeight); 
157      imagecopyresampled( $destImage, $srcImage, 0, 0, 0, 0,
158                          $destWidth,$destHeight,$srcWidth,$srcHeight );
159    }
160    else
161    {
162      // GD prior to version  2 -> pretty bad results :-/ (but fast)
163      $destImage = imagecreate( $destWidth, $destHeight);
164      imagecopyresized( $destImage, $srcImage, 0, 0, 0, 0,
165                        $destWidth,$destHeight,$srcWidth,$srcHeight );
166    }
167                       
168                       
169    if( !is_dir( $path.'thumbnail' ) )
170    {
171      umask( 0000 );
172      mkdir( $path.'thumbnail', 0777 );
173    }
174    $dest_file = $path.'thumbnail/'.$conf['prefix_thumbnail'];
175    $dest_file.= get_filename_wo_extension( $filename );
176    $dest_file.= '.'.$tn_ext;
177                       
178    // creation and backup of final picture
179    imagejpeg( $destImage, $dest_file );
180    // freeing memory ressources
181    imagedestroy( $srcImage );
182    imagedestroy( $destImage );
183                       
184    list( $width,$height ) = getimagesize( $filepath );
185    $size = floor( filesize( $filepath ) / 1024 ).' KB';
186    list( $tn_width,$tn_height ) = getimagesize( $dest_file );
187    $tn_size = floor( filesize( $dest_file ) / 1024 ).' KB';
188    $info = array( 'file'      => $filename,
189                   'width'     => $width,
190                   'height'    => $height,
191                   'size'      => $size,
192                   'tn_file'   => $dest_file,
193                   'tn_width'  => $tn_width,
194                   'tn_height' => $tn_height,
195                   'tn_size'   => $tn_size );
196    return $info;
197  }
198  // error
199  else
200  {
201    echo $lang['tn_no_support']." ";
202    if ( isset( $extenstion ) )
203    {
204      echo $lang['tn_format'].' '.$extension;
205    }
206    else
207    {
208      echo $lang['tn_thisformat'];
209    }
210    exit();
211  }
212}
213
214// get_displayed_dirs builds the tree of dirs under "galleries". If a
215// directory contains pictures without thumbnails, the become linked to the
216// page of thumbnails creation.
217function get_displayed_dirs( $dir, $indent )
218{
219  global $lang;
220               
221  $sub_dirs = get_subdirs( $dir );
222  $output = '';
223  if (!empty($sub_dirs))
224  {
225  $output.='<ul class="menu">';
226  // write of the dirs
227  foreach ( $sub_dirs as $sub_dir ) {
228    $output.='<li>';
229    $pictures = get_images_without_thumbnail( $dir.'/'.$sub_dir );
230    if ( count( $pictures ) > 0 )
231    {
232          $url = add_session_id(PHPWG_ROOT_PATH.'admin.php?page=thumbnail&amp;dir='.$dir.'/'.$sub_dir);
233      $output.='<a class="adminMenu" href="'.$url.'">'.$sub_dir.'</a> [ '.count( $pictures ).' ';
234          $output.=$lang['thumbnail'].' ]';
235    }
236    else
237    {
238      $output.=$sub_dir;
239    }
240    // recursive call
241    $output.=get_displayed_dirs( $dir.'/'.$sub_dir,
242                                $indent+30 ); 
243        $output.='</li>';   
244  }
245  $output.='</ul>';
246  }
247  return $output;
248}
249
250$errors = array();
251$pictures = array();
252$stats = array();
253
254if ( isset( $_GET['dir'] ) &&  isset( $_POST['submit'] ))
255{
256  $pictures = get_images_without_thumbnail( $_GET['dir'] );
257  // checking criteria
258  if ( !ereg( "^[0-9]{2,3}$", $_POST['width'] ) or $_POST['width'] < 10 )
259  {
260    array_push( $errors, $lang['tn_err_width'].' 10' );
261  }
262  if ( !ereg( "^[0-9]{2,3}$", $_POST['height'] ) or $_POST['height'] < 10 )
263  {
264    array_push( $errors, $lang['tn_err_height'].' 10' );
265  }
266 
267  // picture miniaturization
268  if ( count( $errors ) == 0 )
269  {
270    $stats = phpwg_scandir( $_GET['dir'], $_POST['width'], $_POST['height'] );
271  }
272}
273               
274//----------------------------------------------------- template initialization
275$template->set_filenames( array('thumbnail'=>'admin/thumbnail.tpl') );
276
277$template->assign_vars(array(
278  'L_THUMBNAIL_TITLE'=>$lang['tn_dirs_title'],
279  'L_UNLINK'=>$lang['tn_dirs_alone'],
280  'L_RESULTS'=>$lang['tn_results_title'],
281  'L_TN_PICTURE'=>$lang['tn_picture'],
282  'L_FILESIZE'=>$lang['filesize'],
283  'L_WIDTH'=>$lang['tn_width'],
284  'L_HEIGHT'=>$lang['tn_height'],
285  'L_GENERATED'=>$lang['tn_results_gen_time'],
286  'L_THUMBNAIL'=>$lang['thumbnail'],
287  'L_PARAMS'=>$lang['tn_params_title'],
288  'L_GD'=>$lang['tn_params_GD'],
289  'L_GD_INFO'=>$lang['tn_params_GD_info'],
290  'L_WIDTH_INFO'=>$lang['tn_params_width_info'],
291  'L_HEIGHT_INFO'=>$lang['tn_params_height_info'],
292  'L_CREATE'=>$lang['tn_params_create'],
293  'L_CREATE_INFO'=>$lang['tn_params_create_info'],
294  'L_FORMAT'=>$lang['tn_params_format'],
295  'L_FORMAT_INFO'=>$lang['tn_params_format_info'],
296  'L_SUBMIT'=>$lang['submit'],
297  'L_REMAINING'=>$lang['tn_alone_title'],
298  'L_TN_STATS'=>$lang['tn_stats'],
299  'L_TN_NB_STATS'=>$lang['tn_stats_nb'],
300  'L_TN_TOTAL'=>$lang['tn_stats_total'],
301  'L_TN_MAX'=>$lang['tn_stats_max'],
302  'L_TN_MIN'=>$lang['tn_stats_min'],
303  'L_TN_AVERAGE'=>$lang['tn_stats_mean'],
304 
305  'T_STYLE'=>$user['template']
306  ));
307
308//----------------------------------------------------- miniaturization results
309if ( sizeof( $errors ) != 0 )
310{
311  $template->assign_block_vars('errors',array());
312  for ( $i = 0; $i < sizeof( $errors ); $i++ )
313  {
314    $template->assign_block_vars('errors.error',array('ERROR'=>$errors[$i]));
315  }
316}
317else if ( isset( $_GET['dir'] ) &&  isset( $_POST['submit'] ) && !empty($stats))
318{
319  $times = array();
320  foreach ( $stats as $stat ) {
321        array_push( $times, $stat['time'] );
322  }
323  $sum=array_sum( $times );
324  $average = $sum/sizeof($times);
325  sort( $times, SORT_NUMERIC );
326  $max = array_pop($times);
327  $min = array_shift( $times);
328 
329  $template->assign_block_vars('results',array(
330    'TN_NB'=>count( $stats ),
331        'TN_TOTAL'=>number_format( $sum, 2, '.', ' ').' ms',
332        'TN_MAX'=>number_format( $max, 2, '.', ' ').' ms',
333        'TN_MIN'=>number_format( $min, 2, '.', ' ').' ms',
334        'TN_AVERAGE'=>number_format( $average, 2, '.', ' ').' ms'
335        ));
336  if ( !count( $pictures ) )
337  {
338    $template->assign_block_vars('warning',array());
339  }
340
341  foreach ( $stats as $i => $stat ) 
342  {
343        $class = ($i % 2)? 'row1':'row2';
344    $color='';
345        if ($stat['time']==$max) $color = 'red';
346        elseif ($stat['time']==$min) $color = '#33FF00';
347        $template->assign_block_vars('results.picture',array(
348          'NB_IMG'=>($i+1),
349          'FILE_IMG'=>$stat['file'],
350          'FILESIZE_IMG'=>$stat['size'],
351          'WIDTH_IMG'=>$stat['width'],
352          'HEIGHT_IMG'=>$stat['height'],
353          'TN_FILE_IMG'=>$stat['tn_file'],
354          'TN_FILESIZE_IMG'=>$stat['tn_size'],
355          'TN_WIDTH_IMG'=>$stat['tn_width'],
356          'TN_HEIGHT_IMG'=>$stat['tn_height'],
357          'GEN_TIME'=>number_format( $stat['time'], 2, '.', ' ').' ms',
358         
359          'T_COLOR'=>$color,
360          'T_CLASS'=>$class
361          ));
362    }
363  }
364//-------------------------------------------------- miniaturization parameters
365if ( isset( $_GET['dir'] ) && !sizeof( $pictures ))
366{
367    $form_url = PHPWG_ROOT_PATH.'admin.php?page=thumbnail&amp;dir='.$_GET['dir'];
368    $gd = !empty( $_POST['gd'] )?$_POST['gd']:2;
369        $width = !empty( $_POST['width'] )?$_POST['width']:128;
370        $height = !empty( $_POST['height'] )?$_POST['height']:96;
371        $gdlabel = 'GD'.$gd.'_CHECKED';
372
373    $template->assign_block_vars('params',array(
374          'F_ACTION'=>add_session_id($form_url),
375          $gdlabel=>'checked="checked"',
376          'WIDTH_TN'=>$width,
377          'HEIGHT_TN'=>$height
378          ));
379
380//---------------------------------------------------------- remaining pictures
381  $pictures = get_images_without_thumbnail( $_GET['dir'] );
382  $template->assign_block_vars('remainings',array('TOTAL_IMG'=>count( $pictures )));
383
384  foreach ( $pictures as $i => $picture ) 
385  {
386    $class = ($i % 2)? 'row1':'row2';
387    $template->assign_block_vars('remainings.remaining',array(
388          'NB_IMG'=>($i+1),
389          'FILE_TN'=>$picture['name'],
390          'FILESIZE_IMG'=>$picture['size'],
391          'WIDTH_IMG'=>$picture['width'],
392          'HEIGHT_IMG'=>$picture['height'],
393         
394          'T_CLASS'=>$class
395          ));
396    }
397}
398//-------------------------------------------------------------- directory list
399else
400{
401  $categories = get_displayed_dirs( './galleries', 60 );
402  $template->assign_block_vars('directory_list',array('CATEGORY_LIST'=>$categories));
403}
404
405$template->assign_var_from_handle('ADMIN_CONTENT', 'thumbnail');
406?>
Note: See TracBrowser for help on using the repository browser.