source: tags/release-1_7_2/admin/thumbnail.php @ 25982

Last change on this file since 25982 was 2427, checked in by plg, 16 years ago

bug 802 fixed: if GD library is not available, display an explicit error,
even before trying to generated any thumbnail.

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