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-2005 PhpWebGallery Team - http://phpwebgallery.net | |
---|
6 | // +-----------------------------------------------------------------------+ |
---|
7 | // | branch : BSF (Best So Far) |
---|
8 | // | file : $RCSfile$ |
---|
9 | // | last update : $Date: 2005-01-07 23:10:51 +0000 (Fri, 07 Jan 2005) $ |
---|
10 | // | last modifier : $Author: plg $ |
---|
11 | // | revision : $Revision: 675 $ |
---|
12 | // +-----------------------------------------------------------------------+ |
---|
13 | // | This program is free software; you can redistribute it and/or modify | |
---|
14 | // | it under the terms of the GNU General Public License as published by | |
---|
15 | // | the Free Software Foundation | |
---|
16 | // | | |
---|
17 | // | This program is distributed in the hope that it will be useful, but | |
---|
18 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
19 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
20 | // | General Public License for more details. | |
---|
21 | // | | |
---|
22 | // | You should have received a copy of the GNU General Public License | |
---|
23 | // | along with this program; if not, write to the Free Software | |
---|
24 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
25 | // | USA. | |
---|
26 | // +-----------------------------------------------------------------------+ |
---|
27 | include_once( PHPWG_ROOT_PATH.'admin/include/isadmin.inc.php' ); |
---|
28 | //------------------------------------------------------------------- functions |
---|
29 | // get_subdirs returns an array containing all sub directory names, |
---|
30 | // excepting : '.', '..' and 'thumbnail'. |
---|
31 | function get_subdirs( $dir ) |
---|
32 | { |
---|
33 | $sub_dirs = array(); |
---|
34 | if ( $opendir = opendir( $dir ) ) |
---|
35 | { |
---|
36 | while ( $file = readdir( $opendir ) ) |
---|
37 | { |
---|
38 | if ($file != 'thumbnail' |
---|
39 | and $file != 'pwg_representative' |
---|
40 | and $file != 'pwg_high' |
---|
41 | and $file != '.' |
---|
42 | and $file != '..' |
---|
43 | and is_dir($dir.'/'.$file)) |
---|
44 | { |
---|
45 | array_push( $sub_dirs, $file ); |
---|
46 | } |
---|
47 | } |
---|
48 | } |
---|
49 | return $sub_dirs; |
---|
50 | } |
---|
51 | |
---|
52 | // get_images_without_thumbnail returns an array with all the picture names |
---|
53 | // that don't have associated thumbnail in the directory. Each picture name |
---|
54 | // is associated with the width, heigh and filesize of the picture. |
---|
55 | function get_images_without_thumbnail( $dir ) |
---|
56 | { |
---|
57 | $images = array(); |
---|
58 | if ( $opendir = opendir( $dir ) ) |
---|
59 | { |
---|
60 | while ( $file = readdir( $opendir ) ) |
---|
61 | { |
---|
62 | $path = $dir.'/'.$file; |
---|
63 | if ( is_image( $path, true ) ) |
---|
64 | { |
---|
65 | if ( !TN_exists( $dir, $file ) ) |
---|
66 | { |
---|
67 | $image_infos = getimagesize( $path ); |
---|
68 | $size = floor( filesize( $path ) / 1024 ). ' KB'; |
---|
69 | array_push( $images, array( 'name' => $file, |
---|
70 | 'width' => $image_infos[0], |
---|
71 | 'height' => $image_infos[1], |
---|
72 | 'size' => $size ) ); |
---|
73 | } |
---|
74 | } |
---|
75 | } |
---|
76 | } |
---|
77 | return $images; |
---|
78 | } |
---|
79 | |
---|
80 | // phpwg_scandir scans a dir to find pictures without thumbnails. Once found, |
---|
81 | // creation of the thumbnails (RatioResizeImg). Only the first $_POST['n'] |
---|
82 | // pictures without thumbnails are treated. |
---|
83 | // scandir returns an array with the generation time of each thumbnail (for |
---|
84 | // statistics purpose) |
---|
85 | function phpwg_scandir( $dir, $width, $height ) |
---|
86 | { |
---|
87 | global $conf; |
---|
88 | $stats = array(); |
---|
89 | if ( $opendir = opendir( $dir ) ) |
---|
90 | { |
---|
91 | while ( $file = readdir ( $opendir ) ) |
---|
92 | { |
---|
93 | $path = $dir.'/'.$file; |
---|
94 | if ( is_image( $path, true ) ) |
---|
95 | { |
---|
96 | if ( count( $stats ) < $_POST['n'] and !TN_exists( $dir, $file ) ) |
---|
97 | { |
---|
98 | $starttime = get_moment(); |
---|
99 | $info = RatioResizeImg( $file, $width, $height, $dir.'/', 'jpg' ); |
---|
100 | $endtime = get_moment(); |
---|
101 | $info['time'] = ( $endtime - $starttime ) * 1000; |
---|
102 | array_push( $stats, $info ); |
---|
103 | } |
---|
104 | } |
---|
105 | } |
---|
106 | } |
---|
107 | return $stats; |
---|
108 | } |
---|
109 | |
---|
110 | // RatioResizeImg creates a new picture (a thumbnail since it is supposed to |
---|
111 | // be smaller than original picture !) in the sub directory named |
---|
112 | // "thumbnail". |
---|
113 | function RatioResizeImg( $filename, $newWidth, $newHeight, $path, $tn_ext ) |
---|
114 | { |
---|
115 | global $conf, $lang; |
---|
116 | // full path to picture |
---|
117 | $filepath = $path.$filename; |
---|
118 | // extension of the picture filename |
---|
119 | $extension = get_extension( $filepath ); |
---|
120 | switch( $extension ) |
---|
121 | { |
---|
122 | case 'jpg': $srcImage = @imagecreatefromjpeg( $filepath ); break; |
---|
123 | case 'JPG': $srcImage = @imagecreatefromjpeg( $filepath ); break; |
---|
124 | case 'png': $srcImage = @imagecreatefrompng( $filepath ); break; |
---|
125 | case 'PNG': $srcImage = @imagecreatefrompng( $filepath ); break; |
---|
126 | default : unset( $extension ); break; |
---|
127 | } |
---|
128 | |
---|
129 | if ( isset( $srcImage ) ) |
---|
130 | { |
---|
131 | // width/height |
---|
132 | $srcWidth = imagesx( $srcImage ); |
---|
133 | $srcHeight = imagesy( $srcImage ); |
---|
134 | $ratioWidth = $srcWidth/$newWidth; |
---|
135 | $ratioHeight = $srcHeight/$newHeight; |
---|
136 | |
---|
137 | // maximal size exceeded ? |
---|
138 | if ( ( $ratioWidth > 1 ) or ( $ratioHeight > 1 ) ) |
---|
139 | { |
---|
140 | if ( $ratioWidth < $ratioHeight) |
---|
141 | { |
---|
142 | $destWidth = $srcWidth/$ratioHeight; |
---|
143 | $destHeight = $newHeight; |
---|
144 | } |
---|
145 | else |
---|
146 | { |
---|
147 | $destWidth = $newWidth; |
---|
148 | $destHeight = $srcHeight/$ratioWidth; |
---|
149 | } |
---|
150 | } |
---|
151 | else |
---|
152 | { |
---|
153 | $destWidth = $srcWidth; |
---|
154 | $destHeight = $srcHeight; |
---|
155 | } |
---|
156 | // according to the GD version installed on the server |
---|
157 | if ( $_POST['gd'] == 2 ) |
---|
158 | { |
---|
159 | // GD 2.0 or more recent -> good results (but slower) |
---|
160 | $destImage = imagecreatetruecolor( $destWidth, $destHeight); |
---|
161 | imagecopyresampled( $destImage, $srcImage, 0, 0, 0, 0, |
---|
162 | $destWidth,$destHeight,$srcWidth,$srcHeight ); |
---|
163 | } |
---|
164 | else |
---|
165 | { |
---|
166 | // GD prior to version 2 -> pretty bad results :-/ (but fast) |
---|
167 | $destImage = imagecreate( $destWidth, $destHeight); |
---|
168 | imagecopyresized( $destImage, $srcImage, 0, 0, 0, 0, |
---|
169 | $destWidth,$destHeight,$srcWidth,$srcHeight ); |
---|
170 | } |
---|
171 | |
---|
172 | |
---|
173 | if( !is_dir( $path.'thumbnail' ) ) |
---|
174 | { |
---|
175 | umask( 0000 ); |
---|
176 | mkdir( $path.'thumbnail', 0777 ); |
---|
177 | } |
---|
178 | $dest_file = $path.'thumbnail/'.$conf['prefix_thumbnail']; |
---|
179 | $dest_file.= get_filename_wo_extension( $filename ); |
---|
180 | $dest_file.= '.'.$tn_ext; |
---|
181 | |
---|
182 | // creation and backup of final picture |
---|
183 | imagejpeg( $destImage, $dest_file ); |
---|
184 | // freeing memory ressources |
---|
185 | imagedestroy( $srcImage ); |
---|
186 | imagedestroy( $destImage ); |
---|
187 | |
---|
188 | list( $width,$height ) = getimagesize( $filepath ); |
---|
189 | $size = floor( filesize( $filepath ) / 1024 ).' KB'; |
---|
190 | list( $tn_width,$tn_height ) = getimagesize( $dest_file ); |
---|
191 | $tn_size = floor( filesize( $dest_file ) / 1024 ).' KB'; |
---|
192 | $info = array( 'file' => $filename, |
---|
193 | 'width' => $width, |
---|
194 | 'height' => $height, |
---|
195 | 'size' => $size, |
---|
196 | 'tn_file' => $dest_file, |
---|
197 | 'tn_width' => $tn_width, |
---|
198 | 'tn_height' => $tn_height, |
---|
199 | 'tn_size' => $tn_size ); |
---|
200 | return $info; |
---|
201 | } |
---|
202 | // error |
---|
203 | else |
---|
204 | { |
---|
205 | echo $lang['tn_no_support']." "; |
---|
206 | if ( isset( $extenstion ) ) |
---|
207 | { |
---|
208 | echo $lang['tn_format'].' '.$extension; |
---|
209 | } |
---|
210 | else |
---|
211 | { |
---|
212 | echo $lang['tn_thisformat']; |
---|
213 | } |
---|
214 | exit(); |
---|
215 | } |
---|
216 | } |
---|
217 | |
---|
218 | $errors = array(); |
---|
219 | $pictures = array(); |
---|
220 | $stats = array(); |
---|
221 | |
---|
222 | if ( isset( $_GET['dir'] ) && isset( $_POST['submit'] )) |
---|
223 | { |
---|
224 | $pictures = get_images_without_thumbnail( $_GET['dir'] ); |
---|
225 | // checking criteria |
---|
226 | if ( !ereg( "^[0-9]{2,3}$", $_POST['width'] ) or $_POST['width'] < 10 ) |
---|
227 | { |
---|
228 | array_push( $errors, $lang['tn_err_width'].' 10' ); |
---|
229 | } |
---|
230 | if ( !ereg( "^[0-9]{2,3}$", $_POST['height'] ) or $_POST['height'] < 10 ) |
---|
231 | { |
---|
232 | array_push( $errors, $lang['tn_err_height'].' 10' ); |
---|
233 | } |
---|
234 | |
---|
235 | // picture miniaturization |
---|
236 | if ( count( $errors ) == 0 ) |
---|
237 | { |
---|
238 | $stats = phpwg_scandir( $_GET['dir'], $_POST['width'], $_POST['height'] ); |
---|
239 | } |
---|
240 | } |
---|
241 | |
---|
242 | //----------------------------------------------------- template initialization |
---|
243 | $template->set_filenames( array('thumbnail'=>'admin/thumbnail.tpl') ); |
---|
244 | |
---|
245 | $template->assign_vars(array( |
---|
246 | 'L_THUMBNAIL_TITLE'=>$lang['tn_dirs_title'], |
---|
247 | 'L_UNLINK'=>$lang['tn_dirs_alone'], |
---|
248 | 'L_MISSING_THUMBNAILS'=>$lang['tn_dirs_alone'], |
---|
249 | 'L_RESULTS'=>$lang['tn_results_title'], |
---|
250 | 'L_TN_PICTURE'=>$lang['tn_picture'], |
---|
251 | 'L_FILESIZE'=>$lang['filesize'], |
---|
252 | 'L_WIDTH'=>$lang['tn_width'], |
---|
253 | 'L_HEIGHT'=>$lang['tn_height'], |
---|
254 | 'L_GENERATED'=>$lang['tn_results_gen_time'], |
---|
255 | 'L_THUMBNAIL'=>$lang['thumbnail'], |
---|
256 | 'L_PARAMS'=>$lang['tn_params_title'], |
---|
257 | 'L_GD'=>$lang['tn_params_GD'], |
---|
258 | 'L_GD_INFO'=>$lang['tn_params_GD_info'], |
---|
259 | 'L_WIDTH_INFO'=>$lang['tn_params_width_info'], |
---|
260 | 'L_HEIGHT_INFO'=>$lang['tn_params_height_info'], |
---|
261 | 'L_CREATE'=>$lang['tn_params_create'], |
---|
262 | 'L_CREATE_INFO'=>$lang['tn_params_create_info'], |
---|
263 | 'L_FORMAT'=>$lang['tn_params_format'], |
---|
264 | 'L_FORMAT_INFO'=>$lang['tn_params_format_info'], |
---|
265 | 'L_SUBMIT'=>$lang['submit'], |
---|
266 | 'L_REMAINING'=>$lang['tn_alone_title'], |
---|
267 | 'L_TN_STATS'=>$lang['tn_stats'], |
---|
268 | 'L_TN_NB_STATS'=>$lang['tn_stats_nb'], |
---|
269 | 'L_TN_TOTAL'=>$lang['tn_stats_total'], |
---|
270 | 'L_TN_MAX'=>$lang['tn_stats_max'], |
---|
271 | 'L_TN_MIN'=>$lang['tn_stats_min'], |
---|
272 | 'L_TN_AVERAGE'=>$lang['tn_stats_mean'], |
---|
273 | |
---|
274 | 'T_STYLE'=>$user['template'] |
---|
275 | )); |
---|
276 | |
---|
277 | //----------------------------------------------------- miniaturization results |
---|
278 | if ( sizeof( $errors ) != 0 ) |
---|
279 | { |
---|
280 | $template->assign_block_vars('errors',array()); |
---|
281 | for ( $i = 0; $i < sizeof( $errors ); $i++ ) |
---|
282 | { |
---|
283 | $template->assign_block_vars('errors.error',array('ERROR'=>$errors[$i])); |
---|
284 | } |
---|
285 | } |
---|
286 | else if (isset($_GET['dir']) and isset($_POST['submit']) and !empty($stats)) |
---|
287 | { |
---|
288 | $times = array(); |
---|
289 | foreach ($stats as $stat) |
---|
290 | { |
---|
291 | array_push( $times, $stat['time'] ); |
---|
292 | } |
---|
293 | $sum=array_sum( $times ); |
---|
294 | $average = $sum/sizeof($times); |
---|
295 | sort( $times, SORT_NUMERIC ); |
---|
296 | $max = array_pop($times); |
---|
297 | $min = array_shift( $times); |
---|
298 | |
---|
299 | $template->assign_block_vars( |
---|
300 | 'results', |
---|
301 | array( |
---|
302 | 'TN_NB'=>count( $stats ), |
---|
303 | 'TN_TOTAL'=>number_format( $sum, 2, '.', ' ').' ms', |
---|
304 | 'TN_MAX'=>number_format( $max, 2, '.', ' ').' ms', |
---|
305 | 'TN_MIN'=>number_format( $min, 2, '.', ' ').' ms', |
---|
306 | 'TN_AVERAGE'=>number_format( $average, 2, '.', ' ').' ms' |
---|
307 | )); |
---|
308 | if (!count($pictures)) |
---|
309 | { |
---|
310 | $template->assign_block_vars('warning',array()); |
---|
311 | } |
---|
312 | |
---|
313 | foreach ($stats as $i => $stat) |
---|
314 | { |
---|
315 | $class = ($i % 2)? 'row1':'row2'; |
---|
316 | $color=''; |
---|
317 | if ($stat['time'] == $max) |
---|
318 | { |
---|
319 | $color = 'red'; |
---|
320 | } |
---|
321 | else if ($stat['time'] == $min) |
---|
322 | { |
---|
323 | $color = '#33FF00'; |
---|
324 | } |
---|
325 | |
---|
326 | $template->assign_block_vars( |
---|
327 | 'results.picture', |
---|
328 | array( |
---|
329 | 'NB_IMG'=>($i+1), |
---|
330 | 'FILE_IMG'=>$stat['file'], |
---|
331 | 'FILESIZE_IMG'=>$stat['size'], |
---|
332 | 'WIDTH_IMG'=>$stat['width'], |
---|
333 | 'HEIGHT_IMG'=>$stat['height'], |
---|
334 | 'TN_FILE_IMG'=>$stat['tn_file'], |
---|
335 | 'TN_FILESIZE_IMG'=>$stat['tn_size'], |
---|
336 | 'TN_WIDTH_IMG'=>$stat['tn_width'], |
---|
337 | 'TN_HEIGHT_IMG'=>$stat['tn_height'], |
---|
338 | 'GEN_TIME'=>number_format( $stat['time'], 2, '.', ' ').' ms', |
---|
339 | |
---|
340 | 'T_COLOR'=>$color, |
---|
341 | 'T_CLASS'=>$class |
---|
342 | )); |
---|
343 | } |
---|
344 | } |
---|
345 | //-------------------------------------------------- miniaturization parameters |
---|
346 | if (isset($_GET['dir']) and !sizeof($pictures)) |
---|
347 | { |
---|
348 | $form_url = PHPWG_ROOT_PATH.'admin.php?page=thumbnail&dir='.$_GET['dir']; |
---|
349 | $gd = !empty( $_POST['gd'] )?$_POST['gd']:2; |
---|
350 | $width = !empty( $_POST['width'] )?$_POST['width']:128; |
---|
351 | $height = !empty( $_POST['height'] )?$_POST['height']:96; |
---|
352 | $gdlabel = 'GD'.$gd.'_CHECKED'; |
---|
353 | |
---|
354 | $template->assign_block_vars( |
---|
355 | 'params', |
---|
356 | array( |
---|
357 | 'F_ACTION'=>add_session_id($form_url), |
---|
358 | $gdlabel=>'checked="checked"', |
---|
359 | 'WIDTH_TN'=>$width, |
---|
360 | 'HEIGHT_TN'=>$height |
---|
361 | )); |
---|
362 | //---------------------------------------------------------- remaining pictures |
---|
363 | $pictures = get_images_without_thumbnail( $_GET['dir'] ); |
---|
364 | $template->assign_block_vars( |
---|
365 | 'remainings', |
---|
366 | array('TOTAL_IMG'=>count($pictures))); |
---|
367 | |
---|
368 | foreach ($pictures as $i => $picture) |
---|
369 | { |
---|
370 | $class = ($i % 2)? 'row1':'row2'; |
---|
371 | $template->assign_block_vars( |
---|
372 | 'remainings.remaining', |
---|
373 | array( |
---|
374 | 'NB_IMG'=>($i+1), |
---|
375 | 'FILE_TN'=>$picture['name'], |
---|
376 | 'FILESIZE_IMG'=>$picture['size'], |
---|
377 | 'WIDTH_IMG'=>$picture['width'], |
---|
378 | 'HEIGHT_IMG'=>$picture['height'], |
---|
379 | |
---|
380 | 'T_CLASS'=>$class |
---|
381 | )); |
---|
382 | } |
---|
383 | } |
---|
384 | //-------------------------------------------------------------- directory list |
---|
385 | else |
---|
386 | { |
---|
387 | $wo_thumbnails = array(); |
---|
388 | |
---|
389 | // what is the directory to search in ? |
---|
390 | $query = ' |
---|
391 | SELECT galleries_url |
---|
392 | FROM '.SITES_TABLE.' |
---|
393 | WHERE id = 1 |
---|
394 | ;'; |
---|
395 | list($galleries_url) = mysql_fetch_array(pwg_query($query)); |
---|
396 | $basedir = preg_replace('#/*$#', '', $galleries_url); |
---|
397 | |
---|
398 | $fs = get_fs($basedir); |
---|
399 | // because isset is one hundred time faster than in_array |
---|
400 | $fs['thumbnails'] = array_flip($fs['thumbnails']); |
---|
401 | |
---|
402 | foreach ($fs['elements'] as $path) |
---|
403 | { |
---|
404 | // only pictures need thumbnails |
---|
405 | if (in_array(get_extension($path), $conf['picture_ext'])) |
---|
406 | { |
---|
407 | $dirname = dirname($path); |
---|
408 | $filename = basename($path); |
---|
409 | // searching the element |
---|
410 | $filename_wo_ext = get_filename_wo_extension($filename); |
---|
411 | $tn_ext = ''; |
---|
412 | $base_test = $dirname.'/thumbnail/'; |
---|
413 | $base_test.= $conf['prefix_thumbnail'].$filename_wo_ext.'.'; |
---|
414 | foreach ($conf['picture_ext'] as $ext) |
---|
415 | { |
---|
416 | if (isset($fs['thumbnails'][$base_test.$ext])) |
---|
417 | { |
---|
418 | $tn_ext = $ext; |
---|
419 | break; |
---|
420 | } |
---|
421 | } |
---|
422 | |
---|
423 | if (empty($tn_ext)) |
---|
424 | { |
---|
425 | if (!isset($wo_thumbnails[$dirname])) |
---|
426 | { |
---|
427 | $wo_thumbnails[$dirname] = 1; |
---|
428 | } |
---|
429 | else |
---|
430 | { |
---|
431 | $wo_thumbnails[$dirname]++; |
---|
432 | } |
---|
433 | } |
---|
434 | } |
---|
435 | } |
---|
436 | |
---|
437 | if (count($wo_thumbnails) > 0) |
---|
438 | { |
---|
439 | $template->assign_block_vars('directory_list', array()); |
---|
440 | foreach ($wo_thumbnails as $directory => $nb_missing) |
---|
441 | { |
---|
442 | $url = PHPWG_ROOT_PATH.'admin.php?page=thumbnail&dir='.$directory; |
---|
443 | |
---|
444 | $template->assign_block_vars( |
---|
445 | 'directory_list.directory', |
---|
446 | array( |
---|
447 | 'DIRECTORY'=>$directory, |
---|
448 | 'URL'=>add_session_id($url), |
---|
449 | 'NB_MISSING'=>$nb_missing)); |
---|
450 | } |
---|
451 | } |
---|
452 | } |
---|
453 | $template->assign_var_from_handle('ADMIN_CONTENT', 'thumbnail'); |
---|
454 | ?> |
---|