source: trunk/admin/thumbnail.php @ 5920

Last change on this file since 5920 was 5920, checked in by nikrou, 14 years ago

Bug 1617 fixed : help page is displayed in current theme in public or admin pages

  • Property svn:eol-style set to LF
File size: 11.2 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2010 Piwigo Team                  http://piwigo.org |
6// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
7// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
8// +-----------------------------------------------------------------------+
9// | This program is free software; you can redistribute it and/or modify  |
10// | it under the terms of the GNU General Public License as published by  |
11// | the Free Software Foundation                                          |
12// |                                                                       |
13// | This program is distributed in the hope that it will be useful, but   |
14// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
16// | General Public License for more details.                              |
17// |                                                                       |
18// | You should have received a copy of the GNU General Public License     |
19// | along with this program; if not, write to the Free Software           |
20// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21// | USA.                                                                  |
22// +-----------------------------------------------------------------------+
23
24include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
25
26// +-----------------------------------------------------------------------+
27// | Check Access and exit when user status is not ok                      |
28// +-----------------------------------------------------------------------+
29check_status(ACCESS_ADMINISTRATOR);
30
31//------------------------------------------------------------------- functions
32// RatioResizeImg creates a new picture (a thumbnail since it is supposed to
33// be smaller than original picture !) in the sub directory named
34// "thumbnail".
35function RatioResizeImg($path, $newWidth, $newHeight, $tn_ext)
36{
37  global $conf, $lang, $page;
38
39  if (!function_exists('gd_info'))
40  {
41    return;
42  }
43
44  $filename = basename($path);
45  $dirname = dirname($path);
46 
47  // extension of the picture filename
48  $extension = get_extension($filename);
49
50  if (in_array($extension, array('jpg', 'JPG', 'jpeg', 'JPEG')))
51  {
52    $srcImage = @imagecreatefromjpeg($path);
53  }
54  else if ($extension == 'png' or $extension == 'PNG')
55  {
56    $srcImage = @imagecreatefrompng($path);
57  }
58  else
59  {
60    unset($extension);
61  }
62
63  if ( isset( $srcImage ) )
64  {
65    // width/height
66    $srcWidth    = imagesx( $srcImage ); 
67    $srcHeight   = imagesy( $srcImage ); 
68    $ratioWidth  = $srcWidth/$newWidth;
69    $ratioHeight = $srcHeight/$newHeight;
70
71    // maximal size exceeded ?
72    if ( ( $ratioWidth > 1 ) or ( $ratioHeight > 1 ) )
73    {
74      if ( $ratioWidth < $ratioHeight)
75      { 
76        $destWidth = $srcWidth/$ratioHeight;
77        $destHeight = $newHeight; 
78      }
79      else
80      { 
81        $destWidth = $newWidth; 
82        $destHeight = $srcHeight/$ratioWidth;
83      }
84    }
85    else
86    {
87      $destWidth = $srcWidth;
88      $destHeight = $srcHeight;
89    }
90    // according to the GD version installed on the server
91    if ( $_POST['gd'] == 2 )
92    {
93      // GD 2.0 or more recent -> good results (but slower)
94      $destImage = imagecreatetruecolor( $destWidth, $destHeight); 
95      imagecopyresampled( $destImage, $srcImage, 0, 0, 0, 0,
96                          $destWidth,$destHeight,$srcWidth,$srcHeight );
97    }
98    else
99    {
100      // GD prior to version  2 -> pretty bad results :-/ (but fast)
101      $destImage = imagecreate( $destWidth, $destHeight);
102      imagecopyresized( $destImage, $srcImage, 0, 0, 0, 0,
103                        $destWidth,$destHeight,$srcWidth,$srcHeight );
104    }
105
106    if (($tndir = mkget_thumbnail_dir($dirname, $page['errors'])) == false)
107    {
108      return false;
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.'] : '.l10n('no write access'));
119      return false;
120    }
121    imagejpeg($destImage, $dest_file, $conf['tn_compression_level']);
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 l10n('Picture unreachable or no support')." ";
140    if ( isset( $extenstion ) )
141    {
142      echo l10n('for the file format').' '.$extension;
143    }
144    else
145    {
146      echo l10n('for this file format');
147    }
148    exit();
149  }
150}
151
152$pictures = array();
153$stats = array();
154
155if (!function_exists('gd_info'))
156{
157  array_push($page['errors'], l10n('GD library is missing'));
158}
159
160// +-----------------------------------------------------------------------+
161// |                       template initialization                         |
162// +-----------------------------------------------------------------------+
163$template->set_filenames( array('thumbnail'=>'thumbnail.tpl') );
164
165$template->assign(
166  array('U_HELP' => get_root_url().'admin/popuphelp.php?page=thumbnail')
167  );
168// +-----------------------------------------------------------------------+
169// |                   search pictures without thumbnails                  |
170// +-----------------------------------------------------------------------+
171$wo_thumbnails = array();
172$thumbnalized = array();
173
174// what is the directory to search in ?
175$query = '
176SELECT galleries_url FROM '.SITES_TABLE.'
177  WHERE galleries_url NOT LIKE "http://%"
178;';
179$result = pwg_query($query);
180while ( $row=pwg_db_fetch_assoc($result) )
181{
182  $basedir = preg_replace('#/*$#', '', $row['galleries_url']);
183  $fs = get_fs($basedir);
184
185  // because isset is one hundred time faster than in_array
186  $fs['thumbnails'] = array_flip($fs['thumbnails']);
187
188  foreach ($fs['elements'] as $path)
189  {
190    // only pictures need thumbnails
191    if (in_array(get_extension($path), $conf['picture_ext']))
192    {
193      $dirname = dirname($path);
194      $filename = basename($path);
195 
196      // only files matching the authorized filename pattern can be considered
197      // as "without thumbnail"
198      if (!preg_match('/^[a-zA-Z0-9-_.]+$/', $filename))
199      {
200        continue;
201      }
202     
203      // searching the element
204      $filename_wo_ext = get_filename_wo_extension($filename);
205      $tn_ext = '';
206      $base_test = $dirname.'/'.$conf['dir_thumbnail'].'/';
207      $base_test.= $conf['prefix_thumbnail'].$filename_wo_ext.'.';
208      foreach ($conf['picture_ext'] as $ext)
209      {
210        if (isset($fs['thumbnails'][$base_test.$ext]))
211        {
212          $tn_ext = $ext;
213          break;
214        }
215      }
216     
217      if (empty($tn_ext))
218      {
219        array_push($wo_thumbnails, $path);
220      }
221    }
222  } // next element
223} // next site id
224// +-----------------------------------------------------------------------+
225// |                         thumbnails creation                           |
226// +-----------------------------------------------------------------------+
227if (isset($_POST['submit']))
228{
229  $times = array();
230  $infos = array();
231 
232  // checking criteria
233  if (!preg_match('/^[0-9]{2,3}$/', $_POST['width']) or $_POST['width'] < 10)
234  {
235    array_push($page['errors'], l10n('width must be a number superior to').' 10');
236  }
237  if (!preg_match('/^[0-9]{2,3}$/', $_POST['height']) or $_POST['height'] < 10)
238  {
239    array_push($page['errors'], l10n('height must be a number superior to').' 10');
240  }
241 
242  // picture miniaturization
243  if (count($page['errors']) == 0)
244  {
245    $num = 1;
246    foreach ($wo_thumbnails as $path)
247    {
248      if (is_numeric($_POST['n']) and $num > $_POST['n'])
249      {
250        break;
251      }
252     
253      $starttime = get_moment();
254      if ($info = RatioResizeImg($path,$_POST['width'],$_POST['height'],'jpg'))
255      {
256        $endtime = get_moment();
257        $info['time'] = ($endtime - $starttime) * 1000;
258        array_push($infos, $info);
259        array_push($times, $info['time']);
260        array_push($thumbnalized, $path);
261        $num++;
262      }
263      else
264      {
265        break;
266      }
267    }
268
269    if (count($infos) > 0)
270    {
271      $sum = array_sum($times);
272      $average = $sum / count($times);
273      sort($times, SORT_NUMERIC);
274      $max = array_pop($times);
275      if (count($thumbnalized) == 1)
276      {
277        $min = $max;
278      }
279      else
280      {
281        $min = array_shift($times);
282      }
283     
284      $tpl_var = 
285        array(
286          'TN_NB'=>count($infos),
287          'TN_TOTAL'=>number_format($sum, 2, '.', ' ').' ms',
288          'TN_MAX'=>number_format($max, 2, '.', ' ').' ms',
289          'TN_MIN'=>number_format($min, 2, '.', ' ').' ms',
290          'TN_AVERAGE'=>number_format($average, 2, '.', ' ').' ms',
291          'elements' => array()
292          );
293     
294      foreach ($infos as $i => $info)
295      {
296        $tpl_var['elements'][] =
297          array(
298            'PATH'=>$info['path'],
299            'TN_FILE_IMG'=>$info['tn_file'],
300            'TN_FILESIZE_IMG'=>$info['tn_size'],
301            'TN_WIDTH_IMG'=>$info['tn_width'],
302            'TN_HEIGHT_IMG'=>$info['tn_height'],
303            'GEN_TIME'=>number_format($info['time'], 2, '.', ' ').' ms',
304            );
305      }
306      $template->assign('results', $tpl_var);
307    }
308  }
309}
310// +-----------------------------------------------------------------------+
311// |             form & pictures without thumbnails display                |
312// +-----------------------------------------------------------------------+
313$remainings = array_diff($wo_thumbnails, $thumbnalized);
314
315if (count($remainings) > 0)
316{
317  $form_url = get_root_url().'admin.php?page=thumbnail';
318  $gd = !empty($_POST['gd']) ? $_POST['gd'] : 2;
319  $width = !empty($_POST['width']) ? $_POST['width'] : $conf['tn_width'];
320  $height = !empty($_POST['height']) ? $_POST['height'] : $conf['tn_height'];
321  $n = !empty($_POST['n']) ? $_POST['n'] : 5;
322 
323  $template->assign(
324    'params',
325    array(
326      'F_ACTION'=> $form_url,
327      'GD_SELECTED' => $gd,
328      'N_SELECTED' => $n,
329      'WIDTH_TN'=>$width,
330      'HEIGHT_TN'=>$height
331      ));
332
333  $template->assign(
334    'TOTAL_NB_REMAINING',
335    count($remainings));
336
337  foreach ($remainings as $path)
338  {
339    list($width, $height) = getimagesize($path);
340    $size = floor(filesize($path) / 1024).' KB';
341
342    $template->append(
343      'remainings',
344      array(
345        'PATH'=>$path,
346        'FILESIZE_IMG'=>$size,
347        'WIDTH_IMG'=>$width,
348        'HEIGHT_IMG'=>$height,
349        ));
350  }
351}
352
353// +-----------------------------------------------------------------------+
354// |                           return to admin                             |
355// +-----------------------------------------------------------------------+
356$template->assign_var_from_handle('ADMIN_CONTENT', 'thumbnail');
357?>
Note: See TracBrowser for help on using the repository browser.