source: trunk/admin/thumbnail.php @ 2299

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

Bug fixed: as rvelices notified me by email, my header replacement script was
bugged (r2297 was repeating new and old header).

By the way, I've also removed the replacement keywords. We were using them
because it was a common usage with CVS but it is advised not to use them with
Subversion. Personnaly, it is a problem when I search differences between 2
Piwigo installations outside Subversion.

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