source: trunk/admin/thumbnail.php @ 1072

Last change on this file since 1072 was 1072, checked in by rub, 18 years ago

Step 2 improvement issue 0000301:

o Add and use Functions Check of status
o Restricted Access for user generic

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