1 | <?php |
---|
2 | /*************************************************************************** |
---|
3 | * thumbnail.php * |
---|
4 | * ------------------- * |
---|
5 | * application : PhpWebGallery 1.3 <http://phpwebgallery.net> * |
---|
6 | * author : Pierrick LE GALL <pierrick@z0rglub.com> * |
---|
7 | * * |
---|
8 | * $Id: thumbnail.php 57 2003-08-24 07:40:56Z z0rglub $ |
---|
9 | * * |
---|
10 | ***************************************************************************/ |
---|
11 | |
---|
12 | /*************************************************************************** |
---|
13 | * * |
---|
14 | * This program is free software; you can redistribute it and/or modify * |
---|
15 | * it under the terms of the GNU General Public License as published by * |
---|
16 | * the Free Software Foundation; * |
---|
17 | * * |
---|
18 | ***************************************************************************/ |
---|
19 | include_once( './include/isadmin.inc.php' ); |
---|
20 | //------------------------------------------------------------------- functions |
---|
21 | // get_subdirs returns an array containing all sub directory names, |
---|
22 | // excepting : '.', '..' and 'thumbnail'. |
---|
23 | function get_subdirs( $dir ) |
---|
24 | { |
---|
25 | $sub_dirs = array(); |
---|
26 | if ( $opendir = opendir( $dir ) ) |
---|
27 | { |
---|
28 | while ( $file = readdir( $opendir ) ) |
---|
29 | { |
---|
30 | if ( $file != 'thumbnail' and $file != '.' |
---|
31 | and $file != '..' and is_dir( $dir.'/'.$file ) ) |
---|
32 | { |
---|
33 | array_push( $sub_dirs, $file ); |
---|
34 | } |
---|
35 | } |
---|
36 | } |
---|
37 | return $sub_dirs; |
---|
38 | } |
---|
39 | |
---|
40 | // get_images_without_thumbnail returns an array with all the picture names |
---|
41 | // that don't have associated thumbnail in the directory. Each picture name |
---|
42 | // is associated with the width, heigh and filesize of the picture. |
---|
43 | function get_images_without_thumbnail( $dir ) |
---|
44 | { |
---|
45 | $images = array(); |
---|
46 | if ( $opendir = opendir( $dir ) ) |
---|
47 | { |
---|
48 | while ( $file = readdir( $opendir ) ) |
---|
49 | { |
---|
50 | $path = $dir.'/'.$file; |
---|
51 | if ( is_image( $path, true ) ) |
---|
52 | { |
---|
53 | if ( !TN_exists( $dir, $file ) ) |
---|
54 | { |
---|
55 | $image_infos = getimagesize( $path ); |
---|
56 | $size = floor( filesize( $path ) / 1024 ). ' KB'; |
---|
57 | array_push( $images, array( 'name' => $file, |
---|
58 | 'width' => $image_infos[0], |
---|
59 | 'height' => $image_infos[1], |
---|
60 | 'size' => $size ) ); |
---|
61 | } |
---|
62 | } |
---|
63 | } |
---|
64 | } |
---|
65 | return $images; |
---|
66 | } |
---|
67 | |
---|
68 | // scandir scans a dir to find pictures without thumbnails. Once found, |
---|
69 | // creation of the thumbnails (RatioResizeImg). Only the first $_POST['n'] |
---|
70 | // pictures without thumbnails are treated. |
---|
71 | // scandir returns an array with the generation time of each thumbnail (for |
---|
72 | // statistics purpose) |
---|
73 | function scandir( $dir, $width, $height ) |
---|
74 | { |
---|
75 | global $conf; |
---|
76 | $stats = array(); |
---|
77 | if ( $opendir = opendir( $dir ) ) |
---|
78 | { |
---|
79 | while ( $file = readdir ( $opendir ) ) |
---|
80 | { |
---|
81 | $path = $dir.'/'.$file; |
---|
82 | if ( is_image( $path, true ) ) |
---|
83 | { |
---|
84 | if ( count( $stats ) < $_POST['n'] and !TN_exists( $dir, $file ) ) |
---|
85 | { |
---|
86 | $starttime = get_moment(); |
---|
87 | $info = RatioResizeImg( $file, $width, $height, $dir.'/', 'jpg' ); |
---|
88 | $endtime = get_moment(); |
---|
89 | $info['time'] = ( $endtime - $starttime ) * 1000; |
---|
90 | array_push( $stats, $info ); |
---|
91 | } |
---|
92 | } |
---|
93 | } |
---|
94 | } |
---|
95 | return $stats; |
---|
96 | } |
---|
97 | |
---|
98 | // RatioResizeImg creates a new picture (a thumbnail since it is supposed to |
---|
99 | // be smaller than original picture !) in the sub directory named |
---|
100 | // "thumbnail". |
---|
101 | function RatioResizeImg( $filename, $newWidth, $newHeight, $path, $tn_ext ) |
---|
102 | { |
---|
103 | global $conf, $lang; |
---|
104 | // full path to picture |
---|
105 | $filepath = $path.$filename; |
---|
106 | // extension of the picture filename |
---|
107 | $extension = get_extension( $filepath ); |
---|
108 | switch( $extension ) |
---|
109 | { |
---|
110 | case 'jpg': $srcImage = @imagecreatefromjpeg( $filepath ); break; |
---|
111 | case 'JPG': $srcImage = @imagecreatefromjpeg( $filepath ); break; |
---|
112 | case 'png': $srcImage = @imagecreatefrompng( $filepath ); break; |
---|
113 | case 'PNG': $srcImage = @imagecreatefrompng( $filepath ); break; |
---|
114 | default : unset( $extension ); break; |
---|
115 | } |
---|
116 | |
---|
117 | if ( isset( $srcImage ) ) |
---|
118 | { |
---|
119 | // width/height |
---|
120 | $srcWidth = imagesx( $srcImage ); |
---|
121 | $srcHeight = imagesy( $srcImage ); |
---|
122 | $ratioWidth = $srcWidth/$newWidth; |
---|
123 | $ratioHeight = $srcHeight/$newHeight; |
---|
124 | |
---|
125 | // maximal size exceeded ? |
---|
126 | if ( ( $ratioWidth > 1 ) or ( $ratioHeight > 1 ) ) |
---|
127 | { |
---|
128 | if ( $ratioWidth < $ratioHeight) |
---|
129 | { |
---|
130 | $destWidth = $srcWidth/$ratioHeight; |
---|
131 | $destHeight = $newHeight; |
---|
132 | } |
---|
133 | else |
---|
134 | { |
---|
135 | $destWidth = $newWidth; |
---|
136 | $destHeight = $srcHeight/$ratioWidth; |
---|
137 | } |
---|
138 | } |
---|
139 | else |
---|
140 | { |
---|
141 | $destWidth = $srcWidth; |
---|
142 | $destHeight = $srcHeight; |
---|
143 | } |
---|
144 | // according to the GD version installed on the server |
---|
145 | if ( $_POST['gd'] == 2 ) |
---|
146 | { |
---|
147 | // GD 2.0 or more recent -> good results (but slower) |
---|
148 | $destImage = imagecreatetruecolor( $destWidth, $destHeight); |
---|
149 | imagecopyresampled( $destImage, $srcImage, 0, 0, 0, 0, |
---|
150 | $destWidth,$destHeight,$srcWidth,$srcHeight ); |
---|
151 | } |
---|
152 | else |
---|
153 | { |
---|
154 | // GD prior to version 2 -> pretty bad results :-/ (but fast) |
---|
155 | $destImage = imagecreate( $destWidth, $destHeight); |
---|
156 | imagecopyresized( $destImage, $srcImage, 0, 0, 0, 0, |
---|
157 | $destWidth,$destHeight,$srcWidth,$srcHeight ); |
---|
158 | } |
---|
159 | |
---|
160 | |
---|
161 | if( !is_dir( $path.'thumbnail' ) ) |
---|
162 | { |
---|
163 | umask( 0000 ); |
---|
164 | mkdir( $path.'thumbnail', 0777 ); |
---|
165 | } |
---|
166 | $dest_file = $path.'thumbnail/'.$conf['prefix_thumbnail']; |
---|
167 | $dest_file.= get_filename_wo_extension( $filename ); |
---|
168 | $dest_file.= '.'.$tn_ext; |
---|
169 | |
---|
170 | // creation and backup of final picture |
---|
171 | imagejpeg( $destImage, $dest_file ); |
---|
172 | // freeing memory ressources |
---|
173 | imagedestroy( $srcImage ); |
---|
174 | imagedestroy( $destImage ); |
---|
175 | |
---|
176 | list( $width,$height ) = getimagesize( $filepath ); |
---|
177 | $size = floor( filesize( $filepath ) / 1024 ).' KB'; |
---|
178 | list( $tn_width,$tn_height ) = getimagesize( $dest_file ); |
---|
179 | $tn_size = floor( filesize( $dest_file ) / 1024 ).' KB'; |
---|
180 | $info = array( 'file' => $filename, |
---|
181 | 'width' => $width, |
---|
182 | 'height' => $height, |
---|
183 | 'size' => $size, |
---|
184 | 'tn_file' => $dest_file, |
---|
185 | 'tn_width' => $tn_width, |
---|
186 | 'tn_height' => $tn_height, |
---|
187 | 'tn_size' => $tn_size ); |
---|
188 | return $info; |
---|
189 | } |
---|
190 | // error |
---|
191 | else |
---|
192 | { |
---|
193 | echo $lang['tn_no_support']." "; |
---|
194 | if ( isset( $extenstion ) ) |
---|
195 | { |
---|
196 | echo $lang['tn_format'].' '.$extension; |
---|
197 | } |
---|
198 | else |
---|
199 | { |
---|
200 | echo $lang['tn_thisformat']; |
---|
201 | } |
---|
202 | exit(); |
---|
203 | } |
---|
204 | } |
---|
205 | |
---|
206 | // array_max returns the highest value of the given array |
---|
207 | function array_max( $array ) |
---|
208 | { |
---|
209 | sort( $array, SORT_NUMERIC ); |
---|
210 | return array_pop( $array ); |
---|
211 | } |
---|
212 | |
---|
213 | // array_min returns the lowest value of the given array |
---|
214 | function array_min( $array ) |
---|
215 | { |
---|
216 | sort( $array, SORT_NUMERIC ); |
---|
217 | return array_shift( $array ); |
---|
218 | } |
---|
219 | |
---|
220 | // array_avg returns the average value of the array |
---|
221 | function array_avg( $array ) |
---|
222 | { |
---|
223 | return array_sum( $array ) / sizeof( $array ); |
---|
224 | } |
---|
225 | |
---|
226 | // get_displayed_dirs builds the tree of dirs under "galleries". If a |
---|
227 | // directory contains pictures without thumbnails, the become linked to the |
---|
228 | // page of thumbnails creation. |
---|
229 | function get_displayed_dirs( $dir, $indent ) |
---|
230 | { |
---|
231 | global $conf,$lang,$vtp,$sub; |
---|
232 | |
---|
233 | $sub_dirs = get_subdirs( $dir ); |
---|
234 | // write of the dirs |
---|
235 | foreach ( $sub_dirs as $sub_dir ) { |
---|
236 | $pictures = get_images_without_thumbnail( $dir.'/'.$sub_dir ); |
---|
237 | $vtp->addSession( $sub, 'dir' ); |
---|
238 | $vtp->setVar( $sub, 'dir.indent', $indent ); |
---|
239 | if ( count( $pictures ) > 0 ) |
---|
240 | { |
---|
241 | $vtp->addSession( $sub, 'linked' ); |
---|
242 | $url = './admin.php?page=thumbnail&dir='.$dir."/".$sub_dir; |
---|
243 | $vtp->setVar( $sub, 'linked.url', add_session_id( $url ) ); |
---|
244 | $vtp->setVar( $sub, 'linked.name', $sub_dir ); |
---|
245 | $vtp->setVar( $sub, 'linked.nb_pic', count( $pictures ) ); |
---|
246 | $vtp->closeSession( $sub, 'linked' ); |
---|
247 | } |
---|
248 | else |
---|
249 | { |
---|
250 | $vtp->addSession( $sub, 'unlinked' ); |
---|
251 | $vtp->setVar( $sub, 'unlinked.name', $sub_dir ); |
---|
252 | $vtp->closeSession( $sub, 'unlinked' ); |
---|
253 | } |
---|
254 | $vtp->closeSession( $sub, 'dir' ); |
---|
255 | // recursive call |
---|
256 | $dirs.= get_displayed_dirs( $dir.'/'.$sub_dir, |
---|
257 | $indent+30 ); |
---|
258 | |
---|
259 | } |
---|
260 | } |
---|
261 | //----------------------------------------------------- template initialization |
---|
262 | $sub = $vtp->Open( '../template/'.$user['template'].'/admin/thumbnail.vtp' ); |
---|
263 | $tpl = array( |
---|
264 | 'tn_dirs_title','tn_dirs_alone','tn_params_title','tn_params_GD', |
---|
265 | 'tn_params_GD_info','tn_width','tn_params_width_info','tn_height', |
---|
266 | 'tn_params_height_info','tn_params_create','tn_params_create_info', |
---|
267 | 'tn_params_format','tn_params_format_info','submit','tn_alone_title', |
---|
268 | 'filesize','tn_picture','tn_results_title','thumbnail', |
---|
269 | 'tn_results_gen_time','tn_stats','tn_stats_nb','tn_stats_total', |
---|
270 | 'tn_stats_max','tn_stats_min','tn_stats_mean' ); |
---|
271 | templatize_array( $tpl, 'lang', $sub ); |
---|
272 | $vtp->setGlobalVar( $sub, 'user_template', $user['template'] ); |
---|
273 | //----------------------------------------------------- miniaturization results |
---|
274 | if ( isset( $_GET['dir'] ) ) |
---|
275 | { |
---|
276 | $pictures = get_images_without_thumbnail( $_GET['dir'] ); |
---|
277 | if ( count( $pictures ) == 0 ) |
---|
278 | { |
---|
279 | $vtp->addSession( $sub, 'warning' ); |
---|
280 | $vtp->closeSession( $sub, 'warning' ); |
---|
281 | } |
---|
282 | elseif ( isset( $_POST['submit'] ) ) |
---|
283 | { |
---|
284 | // checking criteria |
---|
285 | $errors = array(); |
---|
286 | if ( !ereg( "^[0-9]{2,3}$", $_POST['width'] ) or $_POST['width'] < 10 ) |
---|
287 | { |
---|
288 | array_push( $errors, $lang['tn_err_width'].' 10' ); |
---|
289 | } |
---|
290 | if ( !ereg( "^[0-9]{2,3}$", $_POST['height'] ) or $_POST['height'] < 10 ) |
---|
291 | { |
---|
292 | array_push( $errors, $lang['tn_err_height'].' 10' ); |
---|
293 | } |
---|
294 | // picture miniaturization |
---|
295 | if ( count( $errors ) == 0 ) |
---|
296 | { |
---|
297 | $vtp->addSession( $sub, 'results' ); |
---|
298 | $stats = scandir( $_GET['dir'], $_POST['width'], $_POST['height'] ); |
---|
299 | $times = array(); |
---|
300 | foreach ( $stats as $stat ) { |
---|
301 | array_push( $times, $stat['time'] ); |
---|
302 | } |
---|
303 | $max = array_max( $times ); |
---|
304 | $min = array_min( $times ); |
---|
305 | foreach ( $stats as $i => $stat ) { |
---|
306 | $vtp->addSession( $sub, 'picture' ); |
---|
307 | if ( $i % 2 == 1 ) |
---|
308 | { |
---|
309 | $vtp->setVar( $sub, 'picture.class', 'row2' ); |
---|
310 | } |
---|
311 | $vtp->setVar( $sub, 'picture.num', ($i+1) ); |
---|
312 | $vtp->setVar( $sub, 'picture.file', $stat['file'] ); |
---|
313 | $vtp->setVar( $sub, 'picture.filesize', $stat['size'] ); |
---|
314 | $vtp->setVar( $sub, 'picture.width', $stat['width'] ); |
---|
315 | $vtp->setVar( $sub, 'picture.height', $stat['height'] ); |
---|
316 | $vtp->setVar( $sub, 'picture.thumb_file', $stat['tn_file'] ); |
---|
317 | $vtp->setVar( $sub, 'picture.thumb_filesize', $stat['tn_size'] ); |
---|
318 | $vtp->setVar( $sub, 'picture.thumb_width', $stat['tn_width'] ); |
---|
319 | $vtp->setVar( $sub, 'picture.thumb_height', $stat['tn_height'] ); |
---|
320 | $vtp->setVar( $sub, 'picture.time', |
---|
321 | number_format( $stat['time'], 2, '.', ' ').' ms' ); |
---|
322 | if ( $stat['time'] == $max ) |
---|
323 | { |
---|
324 | $vtp->setVar( $sub, 'picture.color', 'red' ); |
---|
325 | } |
---|
326 | else if ( $stat['time'] == $min ) |
---|
327 | { |
---|
328 | $vtp->setVar( $sub, 'picture.color', 'green' ); |
---|
329 | } |
---|
330 | $vtp->closeSession( $sub, 'picture' ); |
---|
331 | } |
---|
332 | // general statistics |
---|
333 | $vtp->setVar( $sub, 'results.stats_nb', count( $stats ) ); |
---|
334 | $vtp->setVar( $sub, 'results.stats_total', |
---|
335 | number_format( array_sum( $times ), 2, '.', ' ').' ms' ); |
---|
336 | $vtp->setVar( $sub, 'results.stats_max', |
---|
337 | number_format( $max, 2, '.', ' ').' ms' ); |
---|
338 | $vtp->setVar( $sub, 'results.stats_min', |
---|
339 | number_format( $min, 2, '.', ' ').' ms' ); |
---|
340 | $vtp->setVar( $sub, 'results.stats_mean', |
---|
341 | number_format( array_avg( $times ), 2, '.', ' ').' ms' ); |
---|
342 | $vtp->closeSession( $sub, 'results' ); |
---|
343 | } |
---|
344 | else |
---|
345 | { |
---|
346 | $vtp->addSession( $sub, 'errors' ); |
---|
347 | foreach ( $errors as $error ) { |
---|
348 | $vtp->addSession( $sub, 'li' ); |
---|
349 | $vtp->setVar( $sub, 'li.li', $error ); |
---|
350 | $vtp->closeSession( $sub, 'li' ); |
---|
351 | } |
---|
352 | $vtp->closeSession( $sub, 'errors' ); |
---|
353 | } |
---|
354 | } |
---|
355 | //-------------------------------------------------- miniaturization parameters |
---|
356 | if ( sizeof( $pictures ) != 0 ) |
---|
357 | { |
---|
358 | $vtp->addSession( $sub, 'params' ); |
---|
359 | $url = './admin.php?page=thumbnail&dir='.$_GET['dir']; |
---|
360 | $vtp->setVar( $sub, 'params.action', add_session_id( $url ) ); |
---|
361 | // GD version selected... |
---|
362 | if ( $_POST['gd'] == 1 ) |
---|
363 | { |
---|
364 | $vtp->setVar( $sub, 'params.gd1_checked', ' checked="checked"' ); |
---|
365 | } |
---|
366 | else |
---|
367 | { |
---|
368 | $vtp->setVar( $sub, 'params.gd2_checked', ' checked="checked"' ); |
---|
369 | } |
---|
370 | // width values |
---|
371 | if ( isset( $_POST['width'] ) ) |
---|
372 | { |
---|
373 | $vtp->setVar( $sub, 'params.width_value', $_POST['width'] ); |
---|
374 | } |
---|
375 | else |
---|
376 | { |
---|
377 | $vtp->setVar( $sub, 'params.width_value', '128' ); |
---|
378 | } |
---|
379 | // height value |
---|
380 | if ( isset( $_POST['height'] ) ) |
---|
381 | { |
---|
382 | $vtp->setVar( $sub, 'params.height_value', $_POST['height'] ); |
---|
383 | } |
---|
384 | else |
---|
385 | { |
---|
386 | $vtp->setVar( $sub, 'params.height_value', '96' ); |
---|
387 | } |
---|
388 | // options for the number of picture to miniaturize : "n" |
---|
389 | $options = array( 5,10,20,40 ); |
---|
390 | foreach ( $options as $option ) { |
---|
391 | $vtp->addSession( $sub, 'n_option' ); |
---|
392 | $vtp->setVar( $sub, 'n_option.option', $option ); |
---|
393 | if ( $option == $_POST['n'] ) |
---|
394 | { |
---|
395 | $vtp->setVar( $sub, 'n_option.selected', ' selected="selected"' ); |
---|
396 | } |
---|
397 | $vtp->closeSession( $sub, 'n_option' ); |
---|
398 | } |
---|
399 | $vtp->closeSession( $sub, 'params' ); |
---|
400 | //---------------------------------------------------------- remaining pictures |
---|
401 | $vtp->addSession( $sub, 'remainings' ); |
---|
402 | $pictures = get_images_without_thumbnail( $_GET['dir'] ); |
---|
403 | $vtp->setVar( $sub, 'remainings.total', count( $pictures ) ); |
---|
404 | foreach ( $pictures as $i => $picture ) { |
---|
405 | $vtp->addSession( $sub, 'remaining' ); |
---|
406 | if ( $i % 2 == 1 ) |
---|
407 | { |
---|
408 | $vtp->setVar( $sub, 'remaining.class', 'row2' ); |
---|
409 | } |
---|
410 | $vtp->setVar( $sub, 'remaining.num', ($i+1) ); |
---|
411 | $vtp->setVar( $sub, 'remaining.file', $picture['name'] ); |
---|
412 | $vtp->setVar( $sub, 'remaining.filesize', $picture['size'] ); |
---|
413 | $vtp->setVar( $sub, 'remaining.width', $picture['width'] ); |
---|
414 | $vtp->setVar( $sub, 'remaining.height', $picture['height'] ); |
---|
415 | $vtp->closeSession( $sub, 'remaining' ); |
---|
416 | } |
---|
417 | $vtp->closeSession( $sub, 'remainings' ); |
---|
418 | } |
---|
419 | } |
---|
420 | //-------------------------------------------------------------- directory list |
---|
421 | else |
---|
422 | { |
---|
423 | $vtp->addSession( $sub, 'directory_list' ); |
---|
424 | get_displayed_dirs( '../galleries', 60 ); |
---|
425 | $vtp->closeSession( $sub, 'directory_list' ); |
---|
426 | } |
---|
427 | //----------------------------------------------------------- sending html code |
---|
428 | $vtp->Parse( $handle , 'sub', $sub ); |
---|
429 | ?> |
---|