source: trunk/admin/thumbnail.php @ 345

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

Merge of the 1.3.1 release
Creation of an unique include file (common.php)
Creation of an unique define file (include/constants.php)
Modification of the installation procedure

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