source: trunk/admin/thumbnail.php @ 26

Last change on this file since 26 was 26, checked in by z0rglub, 21 years ago

* empty log message *

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.6 KB
Line 
1<?php
2/***************************************************************************
3 *                              thumbnail.php                              *
4 *                            -------------------                          *
5 *   application          : PhpWebGallery 1.3                              *
6 *   author               : Pierrick LE GALL <pierrick@z0rglub.com>        *
7 *                                                                         *
8 ***************************************************************************/
9
10/***************************************************************************
11 *                                                                         *
12 *   This program is free software; you can redistribute it and/or modify  *
13 *   it under the terms of the GNU General Public License as published by  *
14 *   the Free Software Foundation;                                         *
15 *                                                                         *
16 ***************************************************************************/
17include_once( './include/isadmin.inc.php' );
18//------------------------------------------------------------------- functions
19// get_subdirs returns an array containing all sub directory names,
20// excepting : '.', '..' and 'thumbnail'.
21function get_subdirs( $dir )
22{
23  $sub_dirs = array();
24  if ( $opendir = opendir( $dir ) )
25  {
26    while ( $file = readdir( $opendir ) )
27    {
28      if ( $file != 'thumbnail' and $file != '.'
29           and $file != '..' and is_dir( $dir.'/'.$file ) )
30      {
31        array_push( $sub_dirs, $file );
32      }
33    }
34  }
35  return $sub_dirs;
36}
37
38// get_images_without_thumbnail returns an array with all the picture names
39// that don't have associated thumbnail in the directory. Each picture name
40// is associated with the width, heigh and filesize of the picture.
41function get_images_without_thumbnail( $dir )
42{
43  $images = array();
44  if ( $opendir = opendir( $dir ) )
45  {
46    while ( $file = readdir( $opendir ) )
47    {
48      $path = $dir.'/'.$file;
49      if ( is_image( $path, true ) )
50      {
51        if ( !TN_exists( $dir, $file ) )
52        {
53          $image_infos = getimagesize( $path );
54          $size = floor( filesize( $path ) / 1024 ). ' KB';
55          array_push( $images, array( 'name' => $file,
56                                      'width' => $image_infos[0],
57                                      'height' => $image_infos[1],
58                                      'size' => $size ) );
59        }
60      }
61    }
62  }
63  return $images;
64}
65
66// scandir scans a dir to find pictures without thumbnails. Once found,
67// creation of the thumbnails (RatioResizeImg). Only the first $_POST['n']
68// pictures without thumbnails are treated.
69// scandir returns an array with the generation time of each thumbnail (for
70// statistics purpose)
71function scandir( $dir, $width, $height )
72{
73  global $conf;
74  $stats = array();
75  if ( $opendir = opendir( $dir ) )
76  {
77    while ( $file = readdir ( $opendir ) )
78    {
79      $path = $dir.'/'.$file;
80      if ( is_image( $path, true ) )
81      {
82        if ( count( $stats ) < $_POST['n'] and !TN_exists( $dir, $file ) )
83        {
84          $starttime = get_moment();
85          $info = RatioResizeImg( $file, $width, $height, $dir.'/', 'jpg' );
86          $endtime = get_moment();
87          $info['time'] = ( $endtime - $starttime ) * 1000;
88          array_push( $stats, $info );
89        }
90      }
91    }
92  }
93  return $stats;
94}
95
96// RatioResizeImg creates a new picture (a thumbnail since it is supposed to
97// be smaller than original picture !) in the sub directory named
98// "thumbnail".
99function RatioResizeImg( $filename, $newWidth, $newHeight, $path, $tn_ext )
100{
101  global $conf, $lang;
102  // full path to picture
103  $filepath = $path.$filename;
104  // extension of the picture filename
105  $extension = get_extension( $filepath );
106  switch( $extension )
107  {
108  case 'jpg': $srcImage = @imagecreatefromjpeg( $filepath ); break; 
109  case 'JPG': $srcImage = @imagecreatefromjpeg( $filepath ); break; 
110  case 'png': $srcImage = @imagecreatefrompng(  $filepath ); break; 
111  case 'PNG': $srcImage = @imagecreatefrompng(  $filepath ); break; 
112  default : unset( $extension ); break;
113  }
114               
115  if ( isset( $srcImage ) )
116  {
117    // width/height
118    $srcWidth    = imagesx( $srcImage ); 
119    $srcHeight   = imagesy( $srcImage ); 
120    $ratioWidth  = $srcWidth/$newWidth;
121    $ratioHeight = $srcHeight/$newHeight;
122
123    // maximal size exceeded ?
124    if ( ( $ratioWidth > 1 ) or ( $ratioHeight > 1 ) )
125    {
126      if ( $ratioWidth < $ratioHeight)
127      { 
128        $destWidth = $srcWidth/$ratioHeight;
129        $destHeight = $newHeight; 
130      }
131      else
132      { 
133        $destWidth = $newWidth; 
134        $destHeight = $srcHeight/$ratioWidth;
135      }
136    }
137    else
138    {
139      $destWidth = $srcWidth;
140      $destHeight = $srcHeight;
141    }
142    // according to the GD version installed on the server
143    if ( $_POST['gd'] == 2 )
144    {
145      // GD 2.0 or more recent -> good results (but slower)
146      $destImage = imagecreatetruecolor( $destWidth, $destHeight); 
147      imagecopyresampled( $destImage, $srcImage, 0, 0, 0, 0,
148                          $destWidth,$destHeight,$srcWidth,$srcHeight );
149    }
150    else
151    {
152      // GD prior to version  2 -> pretty bad results :-/ (but fast)
153      $destImage = imagecreate( $destWidth, $destHeight);
154      imagecopyresized( $destImage, $srcImage, 0, 0, 0, 0,
155                        $destWidth,$destHeight,$srcWidth,$srcHeight );
156    }
157                       
158                       
159    if( !is_dir( $path.'thumbnail' ) )
160    {
161      umask( 0000 );
162      mkdir( $path.'thumbnail', 0777 );
163    }
164    $dest_file = $path.'thumbnail/'.$conf['prefix_thumbnail'];
165    $dest_file.= get_filename_wo_extension( $filename );
166    $dest_file.= '.'.$tn_ext;
167                       
168    // creation and backup of final picture
169    imagejpeg( $destImage, $dest_file );
170    // freeing memory ressources
171    imagedestroy( $srcImage );
172    imagedestroy( $destImage );
173                       
174    list( $width,$height ) = getimagesize( $filepath );
175    $size = floor( filesize( $filepath ) / 1024 ).' KB';
176    list( $tn_width,$tn_height ) = getimagesize( $dest_file );
177    $tn_size = floor( filesize( $dest_file ) / 1024 ).' KB';
178    $info = array( 'file'      => $filename,
179                   'width'     => $width,
180                   'height'    => $height,
181                   'size'      => $size,
182                   'tn_file'   => $dest_file,
183                   'tn_width'  => $tn_width,
184                   'tn_height' => $tn_height,
185                   'tn_size'   => $tn_size );
186    return $info;
187  }
188  // error
189  else
190  {
191    echo $lang['tn_no_support']." ";
192    if ( isset( $extenstion ) )
193    {
194      echo $lang['tn_format'].' '.$extension;
195    }
196    else
197    {
198      echo $lang['tn_thisformat'];
199    }
200    exit();
201  }
202}
203
204// array_max returns the highest value of the given array
205function array_max( $array )
206{
207  sort( $array, SORT_NUMERIC );
208  return array_pop( $array );
209}
210
211// array_min returns the lowest value of the given array
212function array_min( $array )
213{
214  sort( $array, SORT_NUMERIC );
215  return array_shift( $array );
216}
217
218// array_avg returns the average value of the array
219function array_avg( $array )
220{
221  return array_sum( $array ) / sizeof( $array );
222}
223       
224// get_displayed_dirs builds the tree of dirs under "galleries". If a
225// directory contains pictures without thumbnails, the become linked to the
226// page of thumbnails creation.
227function get_displayed_dirs( $dir, $indent )
228{
229  global $conf,$lang,$vtp,$sub;
230               
231  $sub_dirs = get_subdirs( $dir );
232  // write of the dirs
233  foreach ( $sub_dirs as $sub_dir ) {
234    $pictures = get_images_without_thumbnail( $dir.'/'.$sub_dir );
235    $vtp->addSession( $sub, 'dir' );
236    $vtp->setVar( $sub, 'dir.indent', $indent );
237    if ( count( $pictures ) > 0 )
238    {
239      $vtp->addSession( $sub, 'linked' );
240      $url = './admin.php?page=thumbnail&amp;dir='.$dir."/".$sub_dir;
241      $vtp->setVar( $sub, 'linked.url', add_session_id( $url ) );
242      $vtp->setVar( $sub, 'linked.name', $sub_dir );
243      $vtp->setVar( $sub, 'linked.nb_pic', count( $pictures ) );
244      $vtp->closeSession( $sub, 'linked' );
245    }
246    else
247    {
248      $vtp->addSession( $sub, 'unlinked' );
249      $vtp->setVar( $sub, 'unlinked.name', $sub_dir );
250      $vtp->closeSession( $sub, 'unlinked' );
251    }
252    $vtp->closeSession( $sub, 'dir' );
253    // recursive call
254    $dirs.= get_displayed_dirs( $dir.'/'.$sub_dir,
255                                $indent+30 );
256   
257  }
258}
259//----------------------------------------------------- template initialization
260$sub = $vtp->Open( '../template/'.$user['template'].'/admin/thumbnail.vtp' );
261$tpl = array(
262  'tn_dirs_title','tn_dirs_alone','tn_params_title','tn_params_GD',
263  'tn_params_GD_info','tn_width','tn_params_width_info','tn_height',
264  'tn_params_height_info','tn_params_create','tn_params_create_info',
265  'tn_params_format','tn_params_format_info','submit','tn_alone_title',
266  'filesize','tn_picture','tn_results_title','thumbnail',
267  'tn_results_gen_time','tn_stats','tn_stats_nb','tn_stats_total',
268  'tn_stats_max','tn_stats_min','tn_stats_mean' );
269templatize_array( $tpl, 'lang', $sub );
270//----------------------------------------------------- miniaturization results
271if ( isset( $_GET['dir'] ) )
272{
273  $pictures = get_images_without_thumbnail( $_GET['dir'] );
274  if ( count( $pictures ) == 0 )
275  {
276    $vtp->addSession( $sub, 'warning' );
277    $vtp->closeSession( $sub, 'warning' );
278  }
279  elseif ( isset( $_POST['submit'] ) )
280  {
281    // checking criteria
282    $errors = array();
283    if ( !ereg( "^[0-9]{2,3}$", $_POST['width'] ) or $_POST['width'] < 10 )
284    {
285      array_push( $errors, $lang['tn_err_width'].' 10' );
286    }
287    if ( !ereg( "^[0-9]{2,3}$", $_POST['height'] ) or $_POST['height'] < 10 )
288    {
289      array_push( $errors, $lang['tn_err_height'].' 10' );
290    }
291    // picture miniaturization
292    if ( count( $errors ) == 0 )
293    {
294      $vtp->addSession( $sub, 'results' );
295      $stats = scandir( $_GET['dir'], $_POST['width'], $_POST['height'] );
296      $times = array();
297      foreach ( $stats as $stat ) {
298        array_push( $times, $stat['time'] );
299      }
300      $max = array_max( $times );
301      $min = array_min( $times );
302      foreach ( $stats as $i => $stat ) {
303        $vtp->addSession( $sub, 'picture' );
304        if ( $i % 2 == 1 )
305        {
306          $vtp->setVar( $sub, 'picture.class', 'row2' );
307        }
308        $vtp->setVar( $sub, 'picture.num',            ($i+1) );
309        $vtp->setVar( $sub, 'picture.file',           $stat['file'] );
310        $vtp->setVar( $sub, 'picture.filesize',       $stat['size'] );
311        $vtp->setVar( $sub, 'picture.width',          $stat['width'] );
312        $vtp->setVar( $sub, 'picture.height',         $stat['height'] );
313        $vtp->setVar( $sub, 'picture.thumb_file',     $stat['tn_file'] );
314        $vtp->setVar( $sub, 'picture.thumb_filesize', $stat['tn_size'] );
315        $vtp->setVar( $sub, 'picture.thumb_width',    $stat['tn_width'] );
316        $vtp->setVar( $sub, 'picture.thumb_height',   $stat['tn_height'] );
317        $vtp->setVar( $sub, 'picture.time',
318                      number_format( $stat['time'], 2, '.', ' ').' ms' );
319        if ( $stat['time'] == $max )
320        {
321          $vtp->setVar( $sub, 'picture.color', 'red' );
322        }
323        else if ( $stat['time'] == $min )
324        {
325          $vtp->setVar( $sub, 'picture.color', 'green' );
326        }
327        $vtp->closeSession( $sub, 'picture' );
328      }
329      // general statistics
330      $vtp->setVar( $sub, 'results.stats_nb', count( $stats ) );
331      $vtp->setVar( $sub, 'results.stats_total',
332                    number_format( array_sum( $times ), 2, '.', ' ').' ms' );
333      $vtp->setVar( $sub, 'results.stats_max',
334                    number_format( $max, 2, '.', ' ').' ms' );
335      $vtp->setVar( $sub, 'results.stats_min',
336                    number_format( $min, 2, '.', ' ').' ms' );
337      $vtp->setVar( $sub, 'results.stats_mean',
338                    number_format( array_avg( $times ), 2, '.', ' ').' ms' );
339      $vtp->closeSession( $sub, 'results' );
340    }
341    else
342    {
343      $vtp->addSession( $sub, 'errors' );
344      foreach ( $errors as $error ) {
345        $vtp->addSession( $sub, 'li' );
346        $vtp->setVar( $sub, 'li.li', $error );
347        $vtp->closeSession( $sub, 'li' );
348      }
349      $vtp->closeSession( $sub, 'errors' );
350    }
351  }
352//-------------------------------------------------- miniaturization parameters
353  if ( sizeof( $pictures ) != 0 )
354  {
355    $vtp->addSession( $sub, 'params' );
356    $url = './admin.php?page=thumbnail&amp;dir='.$_GET['dir'];
357    $vtp->setVar( $sub, 'params.action', add_session_id( $url ) );
358    // GD version selected...
359    if ( $_POST['gd'] == 1 )
360    {
361      $vtp->setVar( $sub, 'params.gd1_checked', ' checked="checked"' );
362    }
363    else
364    {
365      $vtp->setVar( $sub, 'params.gd2_checked', ' checked="checked"' );
366    }
367    // width values
368    if ( isset( $_POST['width'] ) )
369    {
370      $vtp->setVar( $sub, 'params.width_value', $_POST['width'] );
371    }
372    else
373    {
374      $vtp->setVar( $sub, 'params.width_value', '128' );
375    }
376    // height value
377    if ( isset( $_POST['height'] ) )
378    {
379      $vtp->setVar( $sub, 'params.height_value', $_POST['height'] );
380    }
381    else
382    {
383      $vtp->setVar( $sub, 'params.height_value', '96' );
384    }
385    // options for the number of picture to miniaturize : "n"
386    $options = array( 5,10,20,40 );
387    foreach ( $options as $option ) {
388      $vtp->addSession( $sub, 'n_option' );
389      $vtp->setVar( $sub, 'n_option.option', $option );
390      if ( $option == $_POST['n'] )
391      {
392        $vtp->setVar( $sub, 'n_option.selected', ' selected="selected"' );
393      }
394      $vtp->closeSession( $sub, 'n_option' );
395    }
396    $vtp->closeSession( $sub, 'params' );
397//---------------------------------------------------------- remaining pictures
398    $vtp->addSession( $sub, 'remainings' );
399    $pictures = get_images_without_thumbnail( $_GET['dir'] );
400    $vtp->setVar( $sub, 'remainings.total', count( $pictures ) );
401    foreach ( $pictures as $i => $picture ) {
402      $vtp->addSession( $sub, 'remaining' );
403      if ( $i % 2 == 1 )
404      {
405        $vtp->setVar( $sub, 'remaining.class', 'row2' );
406      }
407      $vtp->setVar( $sub, 'remaining.num',      ($i+1) );
408      $vtp->setVar( $sub, 'remaining.file',     $picture['name'] );
409      $vtp->setVar( $sub, 'remaining.filesize', $picture['size'] );
410      $vtp->setVar( $sub, 'remaining.width',    $picture['width'] );
411      $vtp->setVar( $sub, 'remaining.height',   $picture['height'] );
412      $vtp->closeSession( $sub, 'remaining' );
413    }
414    $vtp->closeSession( $sub, 'remainings' );
415  }
416}
417//-------------------------------------------------------------- directory list
418else
419{
420  $vtp->addSession( $sub, 'directory_list' );
421  get_displayed_dirs( '../galleries', 60 );
422  $vtp->closeSession( $sub, 'directory_list' );
423}
424//----------------------------------------------------------- sending html code
425$vtp->Parse( $handle , 'sub', $sub );
426?>
Note: See TracBrowser for help on using the repository browser.