source: trunk/admin/thumbnail.php @ 586

Last change on this file since 586 was 586, checked in by z0rglub, 20 years ago

pwg_representative and pwg_high directories are reserved directories for
PhpWebGallery

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.4 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-10-25 20:42:13 +0000 (Mon, 25 Oct 2004) $
10// | last modifier : $Author: z0rglub $
11// | revision      : $Revision: 586 $
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'
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.
55function 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)
85function 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
110// RatioResizeImg creates a new picture (a thumbnail since it is supposed to
111// be smaller than original picture !) in the sub directory named
112// "thumbnail".
113function RatioResizeImg( $filename, $newWidth, $newHeight, $path, $tn_ext )
114{
115  global $conf, $lang;
116  // full path to picture
117  $filepath = $path.$filename;
118  // 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;
127  }
128               
129  if ( isset( $srcImage ) )
130  {
131    // width/height
132    $srcWidth    = imagesx( $srcImage ); 
133    $srcHeight   = imagesy( $srcImage ); 
134    $ratioWidth  = $srcWidth/$newWidth;
135    $ratioHeight = $srcHeight/$newHeight;
136
137    // maximal size exceeded ?
138    if ( ( $ratioWidth > 1 ) or ( $ratioHeight > 1 ) )
139    {
140      if ( $ratioWidth < $ratioHeight)
141      { 
142        $destWidth = $srcWidth/$ratioHeight;
143        $destHeight = $newHeight; 
144      }
145      else
146      { 
147        $destWidth = $newWidth; 
148        $destHeight = $srcHeight/$ratioWidth;
149      }
150    }
151    else
152    {
153      $destWidth = $srcWidth;
154      $destHeight = $srcHeight;
155    }
156    // according to the GD version installed on the server
157    if ( $_POST['gd'] == 2 )
158    {
159      // GD 2.0 or more recent -> good results (but slower)
160      $destImage = imagecreatetruecolor( $destWidth, $destHeight); 
161      imagecopyresampled( $destImage, $srcImage, 0, 0, 0, 0,
162                          $destWidth,$destHeight,$srcWidth,$srcHeight );
163    }
164    else
165    {
166      // GD prior to version  2 -> pretty bad results :-/ (but fast)
167      $destImage = imagecreate( $destWidth, $destHeight);
168      imagecopyresized( $destImage, $srcImage, 0, 0, 0, 0,
169                        $destWidth,$destHeight,$srcWidth,$srcHeight );
170    }
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 );
180    $dest_file.= '.'.$tn_ext;
181                       
182    // creation and backup of final picture
183    imagejpeg( $destImage, $dest_file );
184    // freeing memory ressources
185    imagedestroy( $srcImage );
186    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,
196                   'tn_file'   => $dest_file,
197                   'tn_width'  => $tn_width,
198                   'tn_height' => $tn_height,
199                   'tn_size'   => $tn_size );
200    return $info;
201  }
202  // error
203  else
204  {
205    echo $lang['tn_no_support']." ";
206    if ( isset( $extenstion ) )
207    {
208      echo $lang['tn_format'].' '.$extension;
209    }
210    else
211    {
212      echo $lang['tn_thisformat'];
213    }
214    exit();
215  }
216}
217
218// get_displayed_dirs builds the tree of dirs under "galleries". If a
219// directory contains pictures without thumbnails, the become linked to the
220// page of thumbnails creation.
221function get_displayed_dirs( $dir, $indent )
222{
223  global $lang;
224               
225  $sub_dirs = get_subdirs( $dir );
226  $output = '';
227  if (!empty($sub_dirs))
228  {
229  $output.='<ul class="menu">';
230  // write of the dirs
231  foreach ( $sub_dirs as $sub_dir ) {
232    $output.='<li>';
233    $pictures = get_images_without_thumbnail( $dir.'/'.$sub_dir );
234    if ( count( $pictures ) > 0 )
235    {
236          $url = add_session_id(PHPWG_ROOT_PATH.'admin.php?page=thumbnail&amp;dir='.$dir.'/'.$sub_dir);
237      $output.='<a class="adminMenu" href="'.$url.'">'.$sub_dir.'</a> [ '.count( $pictures ).' ';
238          $output.=$lang['thumbnail'].' ]';
239    }
240    else
241    {
242      $output.=$sub_dir;
243    }
244    // recursive call
245    $output.=get_displayed_dirs( $dir.'/'.$sub_dir,
246                                $indent+30 ); 
247        $output.='</li>';   
248  }
249  $output.='</ul>';
250  }
251  return $output;
252}
253
254$errors = array();
255$pictures = array();
256$stats = array();
257
258if ( isset( $_GET['dir'] ) &&  isset( $_POST['submit'] ))
259{
260  $pictures = get_images_without_thumbnail( $_GET['dir'] );
261  // checking criteria
262  if ( !ereg( "^[0-9]{2,3}$", $_POST['width'] ) or $_POST['width'] < 10 )
263  {
264    array_push( $errors, $lang['tn_err_width'].' 10' );
265  }
266  if ( !ereg( "^[0-9]{2,3}$", $_POST['height'] ) or $_POST['height'] < 10 )
267  {
268    array_push( $errors, $lang['tn_err_height'].' 10' );
269  }
270 
271  // picture miniaturization
272  if ( count( $errors ) == 0 )
273  {
274    $stats = phpwg_scandir( $_GET['dir'], $_POST['width'], $_POST['height'] );
275  }
276}
277               
278//----------------------------------------------------- template initialization
279$template->set_filenames( array('thumbnail'=>'admin/thumbnail.tpl') );
280
281$template->assign_vars(array(
282  'L_THUMBNAIL_TITLE'=>$lang['tn_dirs_title'],
283  'L_UNLINK'=>$lang['tn_dirs_alone'],
284  'L_RESULTS'=>$lang['tn_results_title'],
285  'L_TN_PICTURE'=>$lang['tn_picture'],
286  'L_FILESIZE'=>$lang['filesize'],
287  'L_WIDTH'=>$lang['tn_width'],
288  'L_HEIGHT'=>$lang['tn_height'],
289  'L_GENERATED'=>$lang['tn_results_gen_time'],
290  'L_THUMBNAIL'=>$lang['thumbnail'],
291  'L_PARAMS'=>$lang['tn_params_title'],
292  'L_GD'=>$lang['tn_params_GD'],
293  'L_GD_INFO'=>$lang['tn_params_GD_info'],
294  'L_WIDTH_INFO'=>$lang['tn_params_width_info'],
295  'L_HEIGHT_INFO'=>$lang['tn_params_height_info'],
296  'L_CREATE'=>$lang['tn_params_create'],
297  'L_CREATE_INFO'=>$lang['tn_params_create_info'],
298  'L_FORMAT'=>$lang['tn_params_format'],
299  'L_FORMAT_INFO'=>$lang['tn_params_format_info'],
300  'L_SUBMIT'=>$lang['submit'],
301  'L_REMAINING'=>$lang['tn_alone_title'],
302  'L_TN_STATS'=>$lang['tn_stats'],
303  'L_TN_NB_STATS'=>$lang['tn_stats_nb'],
304  'L_TN_TOTAL'=>$lang['tn_stats_total'],
305  'L_TN_MAX'=>$lang['tn_stats_max'],
306  'L_TN_MIN'=>$lang['tn_stats_min'],
307  'L_TN_AVERAGE'=>$lang['tn_stats_mean'],
308 
309  'T_STYLE'=>$user['template']
310  ));
311
312//----------------------------------------------------- miniaturization results
313if ( sizeof( $errors ) != 0 )
314{
315  $template->assign_block_vars('errors',array());
316  for ( $i = 0; $i < sizeof( $errors ); $i++ )
317  {
318    $template->assign_block_vars('errors.error',array('ERROR'=>$errors[$i]));
319  }
320}
321else if ( isset( $_GET['dir'] ) &&  isset( $_POST['submit'] ) && !empty($stats))
322{
323  $times = array();
324  foreach ( $stats as $stat ) {
325        array_push( $times, $stat['time'] );
326  }
327  $sum=array_sum( $times );
328  $average = $sum/sizeof($times);
329  sort( $times, SORT_NUMERIC );
330  $max = array_pop($times);
331  $min = array_shift( $times);
332 
333  $template->assign_block_vars('results',array(
334    'TN_NB'=>count( $stats ),
335        'TN_TOTAL'=>number_format( $sum, 2, '.', ' ').' ms',
336        'TN_MAX'=>number_format( $max, 2, '.', ' ').' ms',
337        'TN_MIN'=>number_format( $min, 2, '.', ' ').' ms',
338        'TN_AVERAGE'=>number_format( $average, 2, '.', ' ').' ms'
339        ));
340  if ( !count( $pictures ) )
341  {
342    $template->assign_block_vars('warning',array());
343  }
344
345  foreach ( $stats as $i => $stat ) 
346  {
347        $class = ($i % 2)? 'row1':'row2';
348    $color='';
349        if ($stat['time']==$max) $color = 'red';
350        elseif ($stat['time']==$min) $color = '#33FF00';
351        $template->assign_block_vars('results.picture',array(
352          'NB_IMG'=>($i+1),
353          'FILE_IMG'=>$stat['file'],
354          'FILESIZE_IMG'=>$stat['size'],
355          'WIDTH_IMG'=>$stat['width'],
356          'HEIGHT_IMG'=>$stat['height'],
357          'TN_FILE_IMG'=>$stat['tn_file'],
358          'TN_FILESIZE_IMG'=>$stat['tn_size'],
359          'TN_WIDTH_IMG'=>$stat['tn_width'],
360          'TN_HEIGHT_IMG'=>$stat['tn_height'],
361          'GEN_TIME'=>number_format( $stat['time'], 2, '.', ' ').' ms',
362         
363          'T_COLOR'=>$color,
364          'T_CLASS'=>$class
365          ));
366    }
367  }
368//-------------------------------------------------- miniaturization parameters
369if ( isset( $_GET['dir'] ) && !sizeof( $pictures ))
370{
371    $form_url = PHPWG_ROOT_PATH.'admin.php?page=thumbnail&amp;dir='.$_GET['dir'];
372    $gd = !empty( $_POST['gd'] )?$_POST['gd']:2;
373        $width = !empty( $_POST['width'] )?$_POST['width']:128;
374        $height = !empty( $_POST['height'] )?$_POST['height']:96;
375        $gdlabel = 'GD'.$gd.'_CHECKED';
376
377    $template->assign_block_vars('params',array(
378          'F_ACTION'=>add_session_id($form_url),
379          $gdlabel=>'checked="checked"',
380          'WIDTH_TN'=>$width,
381          'HEIGHT_TN'=>$height
382          ));
383
384//---------------------------------------------------------- remaining pictures
385  $pictures = get_images_without_thumbnail( $_GET['dir'] );
386  $template->assign_block_vars('remainings',array('TOTAL_IMG'=>count( $pictures )));
387
388  foreach ( $pictures as $i => $picture ) 
389  {
390    $class = ($i % 2)? 'row1':'row2';
391    $template->assign_block_vars('remainings.remaining',array(
392          'NB_IMG'=>($i+1),
393          'FILE_TN'=>$picture['name'],
394          'FILESIZE_IMG'=>$picture['size'],
395          'WIDTH_IMG'=>$picture['width'],
396          'HEIGHT_IMG'=>$picture['height'],
397         
398          'T_CLASS'=>$class
399          ));
400    }
401}
402//-------------------------------------------------------------- directory list
403else
404{
405  $categories = get_displayed_dirs( './galleries', 60 );
406  $template->assign_block_vars('directory_list',array('CATEGORY_LIST'=>$categories));
407}
408
409$template->assign_var_from_handle('ADMIN_CONTENT', 'thumbnail');
410?>
Note: See TracBrowser for help on using the repository browser.