source: trunk/admin/thumbnail.php @ 2297

Last change on this file since 2297 was 2297, checked in by plg, 16 years ago

Modification: new header on PHP files, PhpWebGallery renamed Piwigo.

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