source: branches/2.0/admin/thumbnail.php @ 6348

Last change on this file since 6348 was 4035, checked in by patdenice, 14 years ago

Replace some ereg function (depreciated) by preg_match.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 11.1 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2009 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('tn_no_support')." ";
140    if ( isset( $extenstion ) )
141    {
142      echo l10n('tn_format').' '.$extension;
143    }
144    else
145    {
146      echo l10n('tn_thisformat');
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(array(
166  'U_HELP' => PHPWG_ROOT_PATH.'popuphelp.php?page=thumbnail',
167  ));
168// +-----------------------------------------------------------------------+
169// |                   search pictures without thumbnails                  |
170// +-----------------------------------------------------------------------+
171$wo_thumbnails = array();
172$thumbnalized = array();
173
174
175// what is the directory to search in ?
176$query = '
177SELECT galleries_url FROM '.SITES_TABLE.'
178  WHERE galleries_url NOT LIKE "http://%"
179;';
180$result = pwg_query($query);
181while ( $row=mysql_fetch_assoc($result) )
182{
183  $basedir = preg_replace('#/*$#', '', $row['galleries_url']);
184  $fs = get_fs($basedir);
185
186  // because isset is one hundred time faster than in_array
187  $fs['thumbnails'] = array_flip($fs['thumbnails']);
188
189  foreach ($fs['elements'] as $path)
190  {
191    // only pictures need thumbnails
192    if (in_array(get_extension($path), $conf['picture_ext']))
193    {
194      $dirname = dirname($path);
195      $filename = basename($path);
196 
197      // only files matching the authorized filename pattern can be considered
198      // as "without thumbnail"
199      if (!preg_match('/^[a-zA-Z0-9-_.]+$/', $filename))
200      {
201        continue;
202      }
203     
204      // searching the element
205      $filename_wo_ext = get_filename_wo_extension($filename);
206      $tn_ext = '';
207      $base_test = $dirname.'/thumbnail/';
208      $base_test.= $conf['prefix_thumbnail'].$filename_wo_ext.'.';
209      foreach ($conf['picture_ext'] as $ext)
210      {
211        if (isset($fs['thumbnails'][$base_test.$ext]))
212        {
213          $tn_ext = $ext;
214          break;
215        }
216      }
217     
218      if (empty($tn_ext))
219      {
220        array_push($wo_thumbnails, $path);
221      }
222    }
223  } // next element
224} // next site id
225// +-----------------------------------------------------------------------+
226// |                         thumbnails creation                           |
227// +-----------------------------------------------------------------------+
228if (isset($_POST['submit']))
229{
230  $times = array();
231  $infos = array();
232 
233  // checking criteria
234  if (!preg_match('/^[0-9]{2,3}$/', $_POST['width']) or $_POST['width'] < 10)
235  {
236    array_push($page['errors'], l10n('tn_err_width').' 10');
237  }
238  if (!preg_match('/^[0-9]{2,3}$/', $_POST['height']) or $_POST['height'] < 10)
239  {
240    array_push($page['errors'], l10n('tn_err_height').' 10');
241  }
242 
243  // picture miniaturization
244  if (count($page['errors']) == 0)
245  {
246    $num = 1;
247    foreach ($wo_thumbnails as $path)
248    {
249      if (is_numeric($_POST['n']) and $num > $_POST['n'])
250      {
251        break;
252      }
253     
254      $starttime = get_moment();
255      if ($info = RatioResizeImg($path,$_POST['width'],$_POST['height'],'jpg'))
256      {
257        $endtime = get_moment();
258        $info['time'] = ($endtime - $starttime) * 1000;
259        array_push($infos, $info);
260        array_push($times, $info['time']);
261        array_push($thumbnalized, $path);
262        $num++;
263      }
264      else
265      {
266        break;
267      }
268    }
269
270    if (count($infos) > 0)
271    {
272      $sum = array_sum($times);
273      $average = $sum / count($times);
274      sort($times, SORT_NUMERIC);
275      $max = array_pop($times);
276      if (count($thumbnalized) == 1)
277      {
278        $min = $max;
279      }
280      else
281      {
282        $min = array_shift($times);
283      }
284     
285      $tpl_var = 
286        array(
287          'TN_NB'=>count($infos),
288          'TN_TOTAL'=>number_format($sum, 2, '.', ' ').' ms',
289          'TN_MAX'=>number_format($max, 2, '.', ' ').' ms',
290          'TN_MIN'=>number_format($min, 2, '.', ' ').' ms',
291          'TN_AVERAGE'=>number_format($average, 2, '.', ' ').' ms',
292          'elements' => array()
293          );
294     
295      foreach ($infos as $i => $info)
296      {
297        $tpl_var['elements'][] =
298          array(
299            'PATH'=>$info['path'],
300            'TN_FILE_IMG'=>$info['tn_file'],
301            'TN_FILESIZE_IMG'=>$info['tn_size'],
302            'TN_WIDTH_IMG'=>$info['tn_width'],
303            'TN_HEIGHT_IMG'=>$info['tn_height'],
304            'GEN_TIME'=>number_format($info['time'], 2, '.', ' ').' ms',
305            );
306      }
307      $template->assign('results', $tpl_var);
308    }
309  }
310}
311// +-----------------------------------------------------------------------+
312// |             form & pictures without thumbnails display                |
313// +-----------------------------------------------------------------------+
314$remainings = array_diff($wo_thumbnails, $thumbnalized);
315
316if (count($remainings) > 0)
317{
318  $form_url = get_root_url().'admin.php?page=thumbnail';
319  $gd = !empty($_POST['gd']) ? $_POST['gd'] : 2;
320  $width = !empty($_POST['width']) ? $_POST['width'] : $conf['tn_width'];
321  $height = !empty($_POST['height']) ? $_POST['height'] : $conf['tn_height'];
322  $n = !empty($_POST['n']) ? $_POST['n'] : 5;
323 
324  $template->assign(
325    'params',
326    array(
327      'F_ACTION'=> $form_url,
328      'GD_SELECTED' => $gd,
329      'N_SELECTED' => $n,
330      'WIDTH_TN'=>$width,
331      'HEIGHT_TN'=>$height
332      ));
333
334  $template->assign(
335    'TOTAL_NB_REMAINING',
336    count($remainings));
337
338  foreach ($remainings as $path)
339  {
340    list($width, $height) = getimagesize($path);
341    $size = floor(filesize($path) / 1024).' KB';
342
343    $template->append(
344      'remainings',
345      array(
346        'PATH'=>$path,
347        'FILESIZE_IMG'=>$size,
348        'WIDTH_IMG'=>$width,
349        'HEIGHT_IMG'=>$height,
350        ));
351  }
352}
353
354// +-----------------------------------------------------------------------+
355// |                           return to admin                             |
356// +-----------------------------------------------------------------------+
357$template->assign_var_from_handle('ADMIN_CONTENT', 'thumbnail');
358?>
Note: See TracBrowser for help on using the repository browser.