source: trunk/admin/thumbnail.php @ 703

Last change on this file since 703 was 703, checked in by plg, 19 years ago
  • bug fixed : in thumbnail creation process, only files with filename authorized by PhpWebGallery are listed
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.8 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | PhpWebGallery - a PHP based picture gallery                           |
4// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
5// | Copyright (C) 2003-2005 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | branch        : BSF (Best So Far)
8// | file          : $RCSfile$
9// | last update   : $Date: 2005-01-17 19:11:50 +0000 (Mon, 17 Jan 2005) $
10// | last modifier : $Author: plg $
11// | revision      : $Revision: 703 $
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// RatioResizeImg creates a new picture (a thumbnail since it is supposed to
30// be smaller than original picture !) in the sub directory named
31// "thumbnail".
32function RatioResizeImg($path, $newWidth, $newHeight, $tn_ext)
33{
34  global $conf, $lang, $errors;
35
36  $filename = basename($path);
37  $dirname = dirname($path);
38 
39  // extension of the picture filename
40  $extension = get_extension($filename);
41
42  if ($extension == 'jpg' or $extension == 'JPG')
43  {
44    $srcImage = @imagecreatefromjpeg($path);
45  }
46  else if ($extension == 'png' or $extension == 'PNG')
47  {
48    $srcImage = @imagecreatefrompng($path);
49  }
50  else
51  {
52    unset($extension);
53  }
54               
55  if ( isset( $srcImage ) )
56  {
57    // width/height
58    $srcWidth    = imagesx( $srcImage ); 
59    $srcHeight   = imagesy( $srcImage ); 
60    $ratioWidth  = $srcWidth/$newWidth;
61    $ratioHeight = $srcHeight/$newHeight;
62
63    // maximal size exceeded ?
64    if ( ( $ratioWidth > 1 ) or ( $ratioHeight > 1 ) )
65    {
66      if ( $ratioWidth < $ratioHeight)
67      { 
68        $destWidth = $srcWidth/$ratioHeight;
69        $destHeight = $newHeight; 
70      }
71      else
72      { 
73        $destWidth = $newWidth; 
74        $destHeight = $srcHeight/$ratioWidth;
75      }
76    }
77    else
78    {
79      $destWidth = $srcWidth;
80      $destHeight = $srcHeight;
81    }
82    // according to the GD version installed on the server
83    if ( $_POST['gd'] == 2 )
84    {
85      // GD 2.0 or more recent -> good results (but slower)
86      $destImage = imagecreatetruecolor( $destWidth, $destHeight); 
87      imagecopyresampled( $destImage, $srcImage, 0, 0, 0, 0,
88                          $destWidth,$destHeight,$srcWidth,$srcHeight );
89    }
90    else
91    {
92      // GD prior to version  2 -> pretty bad results :-/ (but fast)
93      $destImage = imagecreate( $destWidth, $destHeight);
94      imagecopyresized( $destImage, $srcImage, 0, 0, 0, 0,
95                        $destWidth,$destHeight,$srcWidth,$srcHeight );
96    }
97   
98    $tndir = $dirname.'/thumbnail';
99    if (!is_dir($tndir))
100    {
101      if (!is_writable($dirname))
102      {
103        array_push($errors, '['.$dirname.'] : '.$lang['no_write_access']);
104        return false;
105      }
106      umask(0000);
107      mkdir($tndir, 0777);
108    }
109   
110    $dest_file = $tndir.'/'.$conf['prefix_thumbnail'];
111    $dest_file.= get_filename_wo_extension($filename);
112    $dest_file.= '.'.$tn_ext;
113   
114    // creation and backup of final picture
115    if (!is_writable($tndir))
116    {
117      array_push($errors, '['.$tndir.'] : '.$lang['no_write_access']);
118      return false;
119    }
120    imagejpeg($destImage, $dest_file);
121    // freeing memory ressources
122    imagedestroy( $srcImage );
123    imagedestroy( $destImage );
124   
125    list($tn_width, $tn_height) = getimagesize($dest_file);
126    $tn_size = floor(filesize($dest_file) / 1024).' KB';
127   
128    $info = array( 'path'      => $path,
129                   'tn_file'   => $dest_file,
130                   'tn_width'  => $tn_width,
131                   'tn_height' => $tn_height,
132                   'tn_size'   => $tn_size );
133    return $info;
134  }
135  // error
136  else
137  {
138    echo $lang['tn_no_support']." ";
139    if ( isset( $extenstion ) )
140    {
141      echo $lang['tn_format'].' '.$extension;
142    }
143    else
144    {
145      echo $lang['tn_thisformat'];
146    }
147    exit();
148  }
149}
150
151$errors = array();
152$pictures = array();
153$stats = array();
154// +-----------------------------------------------------------------------+
155// |                       template initialization                         |
156// +-----------------------------------------------------------------------+
157$template->set_filenames( array('thumbnail'=>'admin/thumbnail.tpl') );
158
159$template->assign_vars(array(
160  'L_THUMBNAIL_TITLE'=>$lang['tn_dirs_title'],
161  'L_UNLINK'=>$lang['tn_no_missing'],
162  'L_MISSING_THUMBNAILS'=>$lang['tn_dirs_alone'],
163  'L_RESULTS'=>$lang['tn_results_title'],
164  'L_PATH'=>$lang['path'],
165  'L_FILESIZE'=>$lang['filesize'],
166  'L_WIDTH'=>$lang['tn_width'],
167  'L_HEIGHT'=>$lang['tn_height'],
168  'L_GENERATED'=>$lang['tn_results_gen_time'],
169  'L_THUMBNAIL'=>$lang['thumbnail'],
170  'L_PARAMS'=>$lang['tn_params_title'],
171  'L_GD'=>$lang['tn_params_GD'],
172  'L_GD_INFO'=>$lang['tn_params_GD_info'],
173  'L_WIDTH_INFO'=>$lang['tn_params_width_info'],
174  'L_HEIGHT_INFO'=>$lang['tn_params_height_info'],
175  'L_CREATE'=>$lang['tn_params_create'],
176  'L_CREATE_INFO'=>$lang['tn_params_create_info'],
177  'L_FORMAT'=>$lang['tn_params_format'],
178  'L_FORMAT_INFO'=>$lang['tn_params_format_info'],
179  'L_SUBMIT'=>$lang['submit'],
180  'L_REMAINING'=>$lang['tn_alone_title'],
181  'L_TN_STATS'=>$lang['tn_stats'],
182  'L_TN_NB_STATS'=>$lang['tn_stats_nb'],
183  'L_TN_TOTAL'=>$lang['tn_stats_total'],
184  'L_TN_MAX'=>$lang['tn_stats_max'],
185  'L_TN_MIN'=>$lang['tn_stats_min'],
186  'L_TN_AVERAGE'=>$lang['tn_stats_mean'],
187 
188  'T_STYLE'=>$user['template']
189  ));
190// +-----------------------------------------------------------------------+
191// |                   search pictures without thumbnails                  |
192// +-----------------------------------------------------------------------+
193$wo_thumbnails = array();
194$thumbnalized = array();
195
196// what is the directory to search in ?
197$query = '
198SELECT galleries_url
199  FROM '.SITES_TABLE.'
200  WHERE id = 1
201;';
202list($galleries_url) = mysql_fetch_array(pwg_query($query));
203$basedir = preg_replace('#/*$#', '', $galleries_url);
204
205$fs = get_fs($basedir);
206// because isset is one hundred time faster than in_array
207$fs['thumbnails'] = array_flip($fs['thumbnails']);
208
209foreach ($fs['elements'] as $path)
210{
211  // only pictures need thumbnails
212  if (in_array(get_extension($path), $conf['picture_ext']))
213  {
214    $dirname = dirname($path);
215    $filename = basename($path);
216
217    // only files matching the authorized filename pattern can be considered
218    // as "without thumbnail"
219    if (!preg_match('/^[a-zA-Z0-9-_.]+$/', $filename))
220    {
221      continue;
222    }
223   
224    // searching the element
225    $filename_wo_ext = get_filename_wo_extension($filename);
226    $tn_ext = '';
227    $base_test = $dirname.'/thumbnail/';
228    $base_test.= $conf['prefix_thumbnail'].$filename_wo_ext.'.';
229    foreach ($conf['picture_ext'] as $ext)
230    {
231      if (isset($fs['thumbnails'][$base_test.$ext]))
232      {
233        $tn_ext = $ext;
234        break;
235      }
236    }
237   
238    if (empty($tn_ext))
239    {
240      array_push($wo_thumbnails, $path);
241    }
242  }
243}
244// +-----------------------------------------------------------------------+
245// |                         thumbnails creation                           |
246// +-----------------------------------------------------------------------+
247if (isset($_POST['submit']))
248{
249  $errors = array();
250  $times = array();
251  $infos = array();
252 
253  // checking criteria
254  if (!ereg('^[0-9]{2,3}$', $_POST['width']) or $_POST['width'] < 10)
255  {
256    array_push($errors, $lang['tn_err_width'].' 10');
257  }
258  if (!ereg('^[0-9]{2,3}$', $_POST['height']) or $_POST['height'] < 10)
259  {
260    array_push($errors, $lang['tn_err_height'].' 10');
261  }
262 
263  // picture miniaturization
264  if (count($errors) == 0)
265  {
266    $num = 1;
267    foreach ($wo_thumbnails as $path)
268    {
269      if (is_numeric($_POST['n']) and $num > $_POST['n'])
270      {
271        break;
272      }
273     
274      $starttime = get_moment();
275      if ($info = RatioResizeImg($path,$_POST['width'],$_POST['height'],'jpg'))
276      {
277        $endtime = get_moment();
278        $info['time'] = ($endtime - $starttime) * 1000;
279        array_push($infos, $info);
280        array_push($times, $info['time']);
281        array_push($thumbnalized, $path);
282        $num++;
283      }
284      else
285      {
286        break;
287      }
288    }
289
290    if (count($infos) > 0)
291    {
292      $sum = array_sum($times);
293      $average = $sum / count($times);
294      sort($times, SORT_NUMERIC);
295      $max = array_pop($times);
296      if (count($thumbnalized) == 1)
297      {
298        $min = $max;
299      }
300      else
301      {
302        $min = array_shift($times);
303      }
304     
305      $template->assign_block_vars(
306        'results',
307        array(
308          'TN_NB'=>count($infos),
309          'TN_TOTAL'=>number_format($sum, 2, '.', ' ').' ms',
310          'TN_MAX'=>number_format($max, 2, '.', ' ').' ms',
311          'TN_MIN'=>number_format($min, 2, '.', ' ').' ms',
312          'TN_AVERAGE'=>number_format($average, 2, '.', ' ').' ms'
313          ));
314     
315      foreach ($infos as $i => $info)
316      {
317        if ($info['time'] == $max)
318        {
319          $class = 'worst_gen_time';
320        }
321        else if ($info['time'] == $min)
322        {
323          $class = 'best_gen_time';
324        }
325        else
326        {
327          $class = '';
328        }
329       
330        $template->assign_block_vars(
331          'results.picture',
332          array(
333            'PATH'=>$info['path'],
334            'TN_FILE_IMG'=>$info['tn_file'],
335            'TN_FILESIZE_IMG'=>$info['tn_size'],
336            'TN_WIDTH_IMG'=>$info['tn_width'],
337            'TN_HEIGHT_IMG'=>$info['tn_height'],
338            'GEN_TIME'=>number_format($info['time'], 2, '.', ' ').' ms',
339           
340            'T_CLASS'=>$class
341            ));
342      }
343    }
344  }
345}
346// +-----------------------------------------------------------------------+
347// |                            errors display                             |
348// +-----------------------------------------------------------------------+
349if (count($errors) != 0)
350{
351  $template->assign_block_vars('errors',array());
352  foreach ($errors as $error)
353  {
354    $template->assign_block_vars('errors.error',array('ERROR'=>$error));
355  }
356}
357// +-----------------------------------------------------------------------+
358// |             form & pictures without thumbnails display                |
359// +-----------------------------------------------------------------------+
360$remainings = array_diff($wo_thumbnails, $thumbnalized);
361
362if (count($remainings) > 0)
363{
364  $form_url = PHPWG_ROOT_PATH.'admin.php?page=thumbnail';
365  $gd = !empty($_POST['gd']) ? $_POST['gd'] : 2;
366  $width = !empty($_POST['width']) ? $_POST['width'] : $conf['tn_width'];
367  $height = !empty($_POST['height']) ? $_POST['height'] : $conf['tn_height'];
368  $n = !empty($_POST['n']) ? $_POST['n'] : 5;
369 
370  $gdlabel = 'GD'.$gd.'_CHECKED';
371  $nlabel = 'n_'.$n.'_CHECKED';
372 
373  $template->assign_block_vars(
374    'params',
375    array(
376      'F_ACTION'=>add_session_id($form_url),
377      $gdlabel=>'checked="checked"',
378      $nlabel=>'checked="checked"',
379      'WIDTH_TN'=>$width,
380      'HEIGHT_TN'=>$height
381      ));
382
383  $template->assign_block_vars(
384    'remainings',
385    array('TOTAL_IMG'=>count($remainings)));
386
387  $num = 1;
388  foreach ($remainings as $path)
389  {
390    $class = ($num % 2) ? 'row1' : 'row2';
391    list($width, $height) = getimagesize($path);
392    $size = floor(filesize($path) / 1024).' KB';
393
394    $template->assign_block_vars(
395      'remainings.remaining',
396      array(
397        'NB_IMG'=>($num),
398        'PATH'=>$path,
399        'FILESIZE_IMG'=>$size,
400        'WIDTH_IMG'=>$width,
401        'HEIGHT_IMG'=>$height,
402       
403        'T_CLASS'=>$class
404        ));
405
406    $num++;
407  }
408}
409else
410{
411  $template->assign_block_vars('warning', array());
412}
413// +-----------------------------------------------------------------------+
414// |                           return to admin                             |
415// +-----------------------------------------------------------------------+
416$template->assign_var_from_handle('ADMIN_CONTENT', 'thumbnail');
417?>
Note: See TracBrowser for help on using the repository browser.