source: trunk/admin/thumbnail.php @ 2250

Last change on this file since 2250 was 2250, checked in by rvelices, 16 years ago

forgot commit thumbnail.php

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