source: branches/branch-1_5/admin/thumbnail.php @ 1005

Last change on this file since 1005 was 1005, checked in by nikrou, 18 years ago

Revert to revision 1002

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.2 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-01-15 13:49:29 +0000 (Sun, 15 Jan 2006) $
10// | last modifier : $Author: nikrou $
11// | revision      : $Revision: 1005 $
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, $page;
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($page['errors'],
104                   '['.$dirname.'] : '.$lang['no_write_access']);
105        return false;
106      }
107      umask(0000);
108      mkdir($tndir, 0777);
109    }
110   
111    $dest_file = $tndir.'/'.$conf['prefix_thumbnail'];
112    $dest_file.= get_filename_wo_extension($filename);
113    $dest_file.= '.'.$tn_ext;
114   
115    // creation and backup of final picture
116    if (!is_writable($tndir))
117    {
118      array_push($page['errors'], '['.$tndir.'] : '.$lang['no_write_access']);
119      return false;
120    }
121    imagejpeg($destImage, $dest_file);
122    // freeing memory ressources
123    imagedestroy( $srcImage );
124    imagedestroy( $destImage );
125   
126    list($tn_width, $tn_height) = getimagesize($dest_file);
127    $tn_size = floor(filesize($dest_file) / 1024).' KB';
128   
129    $info = array( 'path'      => $path,
130                   'tn_file'   => $dest_file,
131                   'tn_width'  => $tn_width,
132                   'tn_height' => $tn_height,
133                   'tn_size'   => $tn_size );
134    return $info;
135  }
136  // error
137  else
138  {
139    echo $lang['tn_no_support']." ";
140    if ( isset( $extenstion ) )
141    {
142      echo $lang['tn_format'].' '.$extension;
143    }
144    else
145    {
146      echo $lang['tn_thisformat'];
147    }
148    exit();
149  }
150}
151
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_CREATE'=>$lang['tn_params_create'],
173  'L_SUBMIT'=>$lang['submit'],
174  'L_REMAINING'=>$lang['tn_alone_title'],
175  'L_TN_STATS'=>$lang['tn_stats'],
176  'L_TN_NB_STATS'=>$lang['tn_stats_nb'],
177  'L_TN_TOTAL'=>$lang['tn_stats_total'],
178  'L_TN_MAX'=>$lang['tn_stats_max'],
179  'L_TN_MIN'=>$lang['tn_stats_min'],
180  'L_TN_AVERAGE'=>$lang['tn_stats_mean'],
181  'L_ALL'=>$lang['tn_all'],
182
183  'U_HELP' => PHPWG_ROOT_PATH.'/popuphelp.php?page=thumbnail',
184 
185  'T_STYLE'=>$user['template']
186  ));
187// +-----------------------------------------------------------------------+
188// |                   search pictures without thumbnails                  |
189// +-----------------------------------------------------------------------+
190$wo_thumbnails = array();
191$thumbnalized = array();
192
193// what is the directory to search in ?
194$query = '
195SELECT galleries_url
196  FROM '.SITES_TABLE.'
197  WHERE id = 1
198;';
199list($galleries_url) = mysql_fetch_array(pwg_query($query));
200$basedir = preg_replace('#/*$#', '', $galleries_url);
201
202$fs = get_fs($basedir);
203// because isset is one hundred time faster than in_array
204$fs['thumbnails'] = array_flip($fs['thumbnails']);
205
206foreach ($fs['elements'] as $path)
207{
208  // only pictures need thumbnails
209  if (in_array(get_extension($path), $conf['picture_ext']))
210  {
211    $dirname = dirname($path);
212    $filename = basename($path);
213
214    // only files matching the authorized filename pattern can be considered
215    // as "without thumbnail"
216    if (!preg_match('/^[a-zA-Z0-9-_.]+$/', $filename))
217    {
218      continue;
219    }
220   
221    // searching the element
222    $filename_wo_ext = get_filename_wo_extension($filename);
223    $tn_ext = '';
224    $base_test = $dirname.'/thumbnail/';
225    $base_test.= $conf['prefix_thumbnail'].$filename_wo_ext.'.';
226    foreach ($conf['picture_ext'] as $ext)
227    {
228      if (isset($fs['thumbnails'][$base_test.$ext]))
229      {
230        $tn_ext = $ext;
231        break;
232      }
233    }
234   
235    if (empty($tn_ext))
236    {
237      array_push($wo_thumbnails, $path);
238    }
239  }
240}
241// +-----------------------------------------------------------------------+
242// |                         thumbnails creation                           |
243// +-----------------------------------------------------------------------+
244if (isset($_POST['submit']))
245{
246  $times = array();
247  $infos = array();
248 
249  // checking criteria
250  if (!ereg('^[0-9]{2,3}$', $_POST['width']) or $_POST['width'] < 10)
251  {
252    array_push($page['errors'], $lang['tn_err_width'].' 10');
253  }
254  if (!ereg('^[0-9]{2,3}$', $_POST['height']) or $_POST['height'] < 10)
255  {
256    array_push($page['errors'], $lang['tn_err_height'].' 10');
257  }
258 
259  // picture miniaturization
260  if (count($page['errors']) == 0)
261  {
262    $num = 1;
263    foreach ($wo_thumbnails as $path)
264    {
265      if (is_numeric($_POST['n']) and $num > $_POST['n'])
266      {
267        break;
268      }
269     
270      $starttime = get_moment();
271      if ($info = RatioResizeImg($path,$_POST['width'],$_POST['height'],'jpg'))
272      {
273        $endtime = get_moment();
274        $info['time'] = ($endtime - $starttime) * 1000;
275        array_push($infos, $info);
276        array_push($times, $info['time']);
277        array_push($thumbnalized, $path);
278        $num++;
279      }
280      else
281      {
282        break;
283      }
284    }
285
286    if (count($infos) > 0)
287    {
288      $sum = array_sum($times);
289      $average = $sum / count($times);
290      sort($times, SORT_NUMERIC);
291      $max = array_pop($times);
292      if (count($thumbnalized) == 1)
293      {
294        $min = $max;
295      }
296      else
297      {
298        $min = array_shift($times);
299      }
300     
301      $template->assign_block_vars(
302        'results',
303        array(
304          'TN_NB'=>count($infos),
305          'TN_TOTAL'=>number_format($sum, 2, '.', ' ').' ms',
306          'TN_MAX'=>number_format($max, 2, '.', ' ').' ms',
307          'TN_MIN'=>number_format($min, 2, '.', ' ').' ms',
308          'TN_AVERAGE'=>number_format($average, 2, '.', ' ').' ms'
309          ));
310     
311      foreach ($infos as $i => $info)
312      {
313        if ($info['time'] == $max)
314        {
315          $class = 'worst_gen_time';
316        }
317        else if ($info['time'] == $min)
318        {
319          $class = 'best_gen_time';
320        }
321        else
322        {
323          $class = '';
324        }
325       
326        $template->assign_block_vars(
327          'results.picture',
328          array(
329            'PATH'=>$info['path'],
330            'TN_FILE_IMG'=>$info['tn_file'],
331            'TN_FILESIZE_IMG'=>$info['tn_size'],
332            'TN_WIDTH_IMG'=>$info['tn_width'],
333            'TN_HEIGHT_IMG'=>$info['tn_height'],
334            'GEN_TIME'=>number_format($info['time'], 2, '.', ' ').' ms',
335           
336            'T_CLASS'=>$class
337            ));
338      }
339    }
340  }
341}
342// +-----------------------------------------------------------------------+
343// |             form & pictures without thumbnails display                |
344// +-----------------------------------------------------------------------+
345$remainings = array_diff($wo_thumbnails, $thumbnalized);
346
347if (count($remainings) > 0)
348{
349  $form_url = PHPWG_ROOT_PATH.'admin.php?page=thumbnail';
350  $gd = !empty($_POST['gd']) ? $_POST['gd'] : 2;
351  $width = !empty($_POST['width']) ? $_POST['width'] : $conf['tn_width'];
352  $height = !empty($_POST['height']) ? $_POST['height'] : $conf['tn_height'];
353  $n = !empty($_POST['n']) ? $_POST['n'] : 5;
354 
355  $gdlabel = 'GD'.$gd.'_CHECKED';
356  $nlabel = 'n_'.$n.'_CHECKED';
357 
358  $template->assign_block_vars(
359    'params',
360    array(
361      'F_ACTION'=>add_session_id($form_url),
362      $gdlabel=>'checked="checked"',
363      $nlabel=>'checked="checked"',
364      'WIDTH_TN'=>$width,
365      'HEIGHT_TN'=>$height
366      ));
367
368  $template->assign_block_vars(
369    'remainings',
370    array('TOTAL_IMG'=>count($remainings)));
371
372  $num = 1;
373  foreach ($remainings as $path)
374  {
375    $class = ($num % 2) ? 'row1' : 'row2';
376    list($width, $height) = getimagesize($path);
377    $size = floor(filesize($path) / 1024).' KB';
378
379    $template->assign_block_vars(
380      'remainings.remaining',
381      array(
382        'NB_IMG'=>($num),
383        'PATH'=>$path,
384        'FILESIZE_IMG'=>$size,
385        'WIDTH_IMG'=>$width,
386        'HEIGHT_IMG'=>$height,
387       
388        'T_CLASS'=>$class
389        ));
390
391    $num++;
392  }
393}
394else
395{
396  $template->assign_block_vars('warning', array());
397}
398// +-----------------------------------------------------------------------+
399// |                           return to admin                             |
400// +-----------------------------------------------------------------------+
401$template->assign_var_from_handle('ADMIN_CONTENT', 'thumbnail');
402?>
Note: See TracBrowser for help on using the repository browser.