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