source: trunk/admin/thumbnail.php @ 3049

Last change on this file since 3049 was 3049, checked in by plg, 15 years ago

Administration: happy new year 2009, all PHP headers updated.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 11.0 KB
RevLine 
[2]1<?php
[362]2// +-----------------------------------------------------------------------+
[2297]3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
[3049]5// | Copyright(C) 2008-2009 Piwigo Team                  http://piwigo.org |
[2297]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// +-----------------------------------------------------------------------+
[1072]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
[26]31//------------------------------------------------------------------- functions
[695]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)
[2]36{
[792]37  global $conf, $lang, $page;
[2]38
[1379]39  if (!function_exists('gd_info'))
40  {
41    return;
42  }
43
[695]44  $filename = basename($path);
45  $dirname = dirname($path);
46 
47  // extension of the picture filename
48  $extension = get_extension($filename);
49
[1635]50  if (in_array($extension, array('jpg', 'JPG', 'jpeg', 'JPEG')))
[2]51  {
[695]52    $srcImage = @imagecreatefromjpeg($path);
[2]53  }
[695]54  else if ($extension == 'png' or $extension == 'PNG')
[2]55  {
[695]56    $srcImage = @imagecreatefrompng($path);
[2]57  }
[695]58  else
[2]59  {
[695]60    unset($extension);
[2]61  }
[1631]62
[26]63  if ( isset( $srcImage ) )
[2]64  {
[26]65    // width/height
66    $srcWidth    = imagesx( $srcImage ); 
67    $srcHeight   = imagesy( $srcImage ); 
68    $ratioWidth  = $srcWidth/$newWidth;
[2]69    $ratioHeight = $srcHeight/$newHeight;
[26]70
71    // maximal size exceeded ?
72    if ( ( $ratioWidth > 1 ) or ( $ratioHeight > 1 ) )
[2]73    {
[26]74      if ( $ratioWidth < $ratioHeight)
[2]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    }
[26]90    // according to the GD version installed on the server
91    if ( $_POST['gd'] == 2 )
[2]92    {
[26]93      // GD 2.0 or more recent -> good results (but slower)
[2]94      $destImage = imagecreatetruecolor( $destWidth, $destHeight); 
[26]95      imagecopyresampled( $destImage, $srcImage, 0, 0, 0, 0,
96                          $destWidth,$destHeight,$srcWidth,$srcHeight );
[2]97    }
98    else
99    {
[26]100      // GD prior to version  2 -> pretty bad results :-/ (but fast)
[2]101      $destImage = imagecreate( $destWidth, $destHeight);
[26]102      imagecopyresized( $destImage, $srcImage, 0, 0, 0, 0,
103                        $destWidth,$destHeight,$srcWidth,$srcHeight );
[2]104    }
[1631]105
106    if (($tndir = mkget_thumbnail_dir($dirname, $page['errors'])) == false)
[2]107    {
[1631]108      return false;
[2]109    }
[1631]110
[695]111    $dest_file = $tndir.'/'.$conf['prefix_thumbnail'];
112    $dest_file.= get_filename_wo_extension($filename);
[26]113    $dest_file.= '.'.$tn_ext;
[695]114   
[26]115    // creation and backup of final picture
[695]116    if (!is_writable($tndir))
117    {
[2201]118      array_push($page['errors'], '['.$tndir.'] : '.l10n('no_write_access'));
[695]119      return false;
120    }
121    imagejpeg($destImage, $dest_file);
[26]122    // freeing memory ressources
[2]123    imagedestroy( $srcImage );
124    imagedestroy( $destImage );
[695]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,
[26]130                   'tn_file'   => $dest_file,
131                   'tn_width'  => $tn_width,
132                   'tn_height' => $tn_height,
133                   'tn_size'   => $tn_size );
[2]134    return $info;
135  }
[26]136  // error
[2]137  else
138  {
[2201]139    echo l10n('tn_no_support')." ";
[26]140    if ( isset( $extenstion ) )
[2]141    {
[2201]142      echo l10n('tn_format').' '.$extension;
[2]143    }
144    else
145    {
[2201]146      echo l10n('tn_thisformat');
[2]147    }
148    exit();
149  }
150}
[26]151
[393]152$pictures = array();
153$stats = array();
[2428]154
155if (!function_exists('gd_info'))
156{
157  array_push($page['errors'], l10n('GD library is missing'));
158}
159
[695]160// +-----------------------------------------------------------------------+
161// |                       template initialization                         |
162// +-----------------------------------------------------------------------+
[2530]163$template->set_filenames( array('thumbnail'=>'thumbnail.tpl') );
[393]164
[2250]165$template->assign(array(
[1246]166  'U_HELP' => PHPWG_ROOT_PATH.'popuphelp.php?page=thumbnail',
[393]167  ));
[695]168// +-----------------------------------------------------------------------+
169// |                   search pictures without thumbnails                  |
170// +-----------------------------------------------------------------------+
171$wo_thumbnails = array();
172$thumbnalized = array();
[393]173
[1814]174
[695]175// what is the directory to search in ?
176$query = '
[1814]177SELECT galleries_url FROM '.SITES_TABLE.'
178  WHERE galleries_url NOT LIKE "http://%"
[695]179;';
[1814]180$result = pwg_query($query);
181while ( $row=mysql_fetch_assoc($result) )
182{
183  $basedir = preg_replace('#/*$#', '', $row['galleries_url']);
184  $fs = get_fs($basedir);
[695]185
[1814]186  // because isset is one hundred time faster than in_array
187  $fs['thumbnails'] = array_flip($fs['thumbnails']);
[695]188
[1814]189  foreach ($fs['elements'] as $path)
[2]190  {
[1814]191    // only pictures need thumbnails
192    if (in_array(get_extension($path), $conf['picture_ext']))
[703]193    {
[1814]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))
[695]200      {
[1814]201        continue;
[695]202      }
[1814]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      }
[695]222    }
[1814]223  } // next element
224} // next site id
[695]225// +-----------------------------------------------------------------------+
226// |                         thumbnails creation                           |
227// +-----------------------------------------------------------------------+
228if (isset($_POST['submit']))
[393]229{
230  $times = array();
[695]231  $infos = array();
232 
233  // checking criteria
234  if (!ereg('^[0-9]{2,3}$', $_POST['width']) or $_POST['width'] < 10)
[665]235  {
[2201]236    array_push($page['errors'], l10n('tn_err_width').' 10');
[393]237  }
[695]238  if (!ereg('^[0-9]{2,3}$', $_POST['height']) or $_POST['height'] < 10)
[2]239  {
[2201]240    array_push($page['errors'], l10n('tn_err_height').' 10');
[393]241  }
[665]242 
[695]243  // picture miniaturization
[792]244  if (count($page['errors']) == 0)
[393]245  {
[695]246    $num = 1;
247    foreach ($wo_thumbnails as $path)
[665]248    {
[695]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      }
[2]268    }
[695]269
270    if (count($infos) > 0)
[665]271    {
[695]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     
[2250]285      $tpl_var = 
[695]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',
[2250]291          'TN_AVERAGE'=>number_format($average, 2, '.', ' ').' ms',
292          'elements' => array()
293          );
[695]294     
295      foreach ($infos as $i => $info)
296      {
[2250]297        $tpl_var['elements'][] =
[695]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',
[2250]305            );
[695]306      }
[2250]307      $template->assign('results', $tpl_var);
[665]308    }
[2]309  }
[665]310}
[695]311// +-----------------------------------------------------------------------+
312// |             form & pictures without thumbnails display                |
313// +-----------------------------------------------------------------------+
314$remainings = array_diff($wo_thumbnails, $thumbnalized);
315
316if (count($remainings) > 0)
317{
[2250]318  $form_url = get_root_url().'admin.php?page=thumbnail';
[695]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 
[2250]324  $template->assign(
[665]325    'params',
326    array(
[2250]327      'F_ACTION'=> $form_url,
328      'GD_SELECTED' => $gd,
329      'N_SELECTED' => $n,
[665]330      'WIDTH_TN'=>$width,
331      'HEIGHT_TN'=>$height
332      ));
[695]333
[2250]334  $template->assign(
335    'TOTAL_NB_REMAINING',
336    count($remainings));
[393]337
[695]338  foreach ($remainings as $path)
[2]339  {
[695]340    list($width, $height) = getimagesize($path);
341    $size = floor(filesize($path) / 1024).' KB';
342
[2250]343    $template->append(
344      'remainings',
[665]345      array(
[695]346        'PATH'=>$path,
347        'FILESIZE_IMG'=>$size,
348        'WIDTH_IMG'=>$width,
349        'HEIGHT_IMG'=>$height,
[665]350        ));
351  }
[2]352}
[2250]353
[695]354// +-----------------------------------------------------------------------+
355// |                           return to admin                             |
356// +-----------------------------------------------------------------------+
[393]357$template->assign_var_from_handle('ADMIN_CONTENT', 'thumbnail');
[362]358?>
Note: See TracBrowser for help on using the repository browser.