source: branches/1.3/admin/thumbnail.php @ 29641

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