source: trunk/admin/thumbnail.php @ 2245

Last change on this file since 2245 was 2201, checked in by rub, 16 years ago

Replace old use of $lang by l10n function.

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