1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | Piwigo - a PHP based picture gallery | |
---|
4 | // +-----------------------------------------------------------------------+ |
---|
5 | // | Copyright(C) 2008-2010 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 | |
---|
24 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
25 | |
---|
26 | // +-----------------------------------------------------------------------+ |
---|
27 | // | Check Access and exit when user status is not ok | |
---|
28 | // +-----------------------------------------------------------------------+ |
---|
29 | check_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". |
---|
35 | function RatioResizeImg($info, $path, $newWidth, $newHeight, $tn_ext) |
---|
36 | { |
---|
37 | global $conf, $lang, $page; |
---|
38 | |
---|
39 | if ($info !== false) |
---|
40 | { |
---|
41 | //someone hooked us - so we skip |
---|
42 | return $info; |
---|
43 | } |
---|
44 | |
---|
45 | if (!function_exists('gd_info')) |
---|
46 | { |
---|
47 | return; |
---|
48 | } |
---|
49 | |
---|
50 | $filename = basename($path); |
---|
51 | $dirname = dirname($path); |
---|
52 | |
---|
53 | // extension of the picture filename |
---|
54 | $extension = get_extension($filename); |
---|
55 | |
---|
56 | if (in_array($extension, array('jpg', 'JPG', 'jpeg', 'JPEG'))) |
---|
57 | { |
---|
58 | $srcImage = @imagecreatefromjpeg($path); |
---|
59 | } |
---|
60 | else if ($extension == 'png' or $extension == 'PNG') |
---|
61 | { |
---|
62 | $srcImage = @imagecreatefrompng($path); |
---|
63 | } |
---|
64 | else |
---|
65 | { |
---|
66 | unset($extension); |
---|
67 | } |
---|
68 | |
---|
69 | if ( isset( $srcImage ) ) |
---|
70 | { |
---|
71 | // width/height |
---|
72 | $srcWidth = imagesx( $srcImage ); |
---|
73 | $srcHeight = imagesy( $srcImage ); |
---|
74 | $ratioWidth = $srcWidth/$newWidth; |
---|
75 | $ratioHeight = $srcHeight/$newHeight; |
---|
76 | |
---|
77 | // maximal size exceeded ? |
---|
78 | if ( ( $ratioWidth > 1 ) or ( $ratioHeight > 1 ) ) |
---|
79 | { |
---|
80 | if ( $ratioWidth < $ratioHeight) |
---|
81 | { |
---|
82 | $destWidth = $srcWidth/$ratioHeight; |
---|
83 | $destHeight = $newHeight; |
---|
84 | } |
---|
85 | else |
---|
86 | { |
---|
87 | $destWidth = $newWidth; |
---|
88 | $destHeight = $srcHeight/$ratioWidth; |
---|
89 | } |
---|
90 | } |
---|
91 | else |
---|
92 | { |
---|
93 | $destWidth = $srcWidth; |
---|
94 | $destHeight = $srcHeight; |
---|
95 | } |
---|
96 | // according to the GD version installed on the server |
---|
97 | if ( $_POST['gd'] == 2 ) |
---|
98 | { |
---|
99 | // GD 2.0 or more recent -> good results (but slower) |
---|
100 | $destImage = imagecreatetruecolor( $destWidth, $destHeight); |
---|
101 | imagecopyresampled( $destImage, $srcImage, 0, 0, 0, 0, |
---|
102 | $destWidth,$destHeight,$srcWidth,$srcHeight ); |
---|
103 | } |
---|
104 | else |
---|
105 | { |
---|
106 | // GD prior to version 2 -> pretty bad results :-/ (but fast) |
---|
107 | $destImage = imagecreate( $destWidth, $destHeight); |
---|
108 | imagecopyresized( $destImage, $srcImage, 0, 0, 0, 0, |
---|
109 | $destWidth,$destHeight,$srcWidth,$srcHeight ); |
---|
110 | } |
---|
111 | |
---|
112 | if (($tndir = mkget_thumbnail_dir($dirname, $page['errors'])) == false) |
---|
113 | { |
---|
114 | return false; |
---|
115 | } |
---|
116 | |
---|
117 | $dest_file = $tndir.'/'.$conf['prefix_thumbnail']; |
---|
118 | $dest_file.= get_filename_wo_extension($filename); |
---|
119 | $dest_file.= '.'.$tn_ext; |
---|
120 | |
---|
121 | // creation and backup of final picture |
---|
122 | if (!is_writable($tndir)) |
---|
123 | { |
---|
124 | array_push($page['errors'], '['.$tndir.'] : '.l10n('no write access')); |
---|
125 | return false; |
---|
126 | } |
---|
127 | imagejpeg($destImage, $dest_file, $conf['tn_compression_level']); |
---|
128 | // freeing memory ressources |
---|
129 | imagedestroy( $srcImage ); |
---|
130 | imagedestroy( $destImage ); |
---|
131 | |
---|
132 | list($tn_width, $tn_height) = getimagesize($dest_file); |
---|
133 | $tn_size = floor(filesize($dest_file) / 1024).' KB'; |
---|
134 | |
---|
135 | $info = array( 'path' => $path, |
---|
136 | 'tn_file' => $dest_file, |
---|
137 | 'tn_width' => $tn_width, |
---|
138 | 'tn_height' => $tn_height, |
---|
139 | 'tn_size' => $tn_size ); |
---|
140 | return $info; |
---|
141 | } |
---|
142 | // error |
---|
143 | else |
---|
144 | { |
---|
145 | echo l10n('Picture unreachable or no support')." "; |
---|
146 | if ( isset( $extenstion ) ) |
---|
147 | { |
---|
148 | echo l10n('for the file format').' '.$extension; |
---|
149 | } |
---|
150 | else |
---|
151 | { |
---|
152 | echo l10n('for this file format'); |
---|
153 | } |
---|
154 | exit(); |
---|
155 | } |
---|
156 | } |
---|
157 | |
---|
158 | $pictures = array(); |
---|
159 | $stats = array(); |
---|
160 | |
---|
161 | if (!function_exists('gd_info')) |
---|
162 | { |
---|
163 | array_push($page['errors'], l10n('GD library is missing')); |
---|
164 | } |
---|
165 | |
---|
166 | // add default event handler for thumbnail resize |
---|
167 | add_event_handler('thumbnail_resize', 'RatioResizeImg', EVENT_HANDLER_PRIORITY_NEUTRAL, 5); |
---|
168 | |
---|
169 | // +-----------------------------------------------------------------------+ |
---|
170 | // | template initialization | |
---|
171 | // +-----------------------------------------------------------------------+ |
---|
172 | $template->set_filenames( array('thumbnail'=>'thumbnail.tpl') ); |
---|
173 | |
---|
174 | $template->assign( |
---|
175 | array('U_HELP' => get_root_url().'admin/popuphelp.php?page=thumbnail') |
---|
176 | ); |
---|
177 | // +-----------------------------------------------------------------------+ |
---|
178 | // | search pictures without thumbnails | |
---|
179 | // +-----------------------------------------------------------------------+ |
---|
180 | $wo_thumbnails = array(); |
---|
181 | $thumbnalized = array(); |
---|
182 | |
---|
183 | // what is the directory to search in ? |
---|
184 | $query = ' |
---|
185 | SELECT galleries_url FROM '.SITES_TABLE.' |
---|
186 | WHERE galleries_url NOT LIKE "http://%" |
---|
187 | ;'; |
---|
188 | $result = pwg_query($query); |
---|
189 | while ( $row=pwg_db_fetch_assoc($result) ) |
---|
190 | { |
---|
191 | $basedir = preg_replace('#/*$#', '', $row['galleries_url']); |
---|
192 | $fs = get_fs($basedir); |
---|
193 | |
---|
194 | // because isset is one hundred time faster than in_array |
---|
195 | $fs['thumbnails'] = array_flip($fs['thumbnails']); |
---|
196 | |
---|
197 | foreach ($fs['elements'] as $path) |
---|
198 | { |
---|
199 | // only pictures need thumbnails |
---|
200 | if (in_array(get_extension($path), $conf['picture_ext'])) |
---|
201 | { |
---|
202 | $dirname = dirname($path); |
---|
203 | $filename = basename($path); |
---|
204 | |
---|
205 | // only files matching the authorized filename pattern can be considered |
---|
206 | // as "without thumbnail" |
---|
207 | if (!preg_match('/^[a-zA-Z0-9-_.]+$/', $filename)) |
---|
208 | { |
---|
209 | continue; |
---|
210 | } |
---|
211 | |
---|
212 | // searching the element |
---|
213 | $filename_wo_ext = get_filename_wo_extension($filename); |
---|
214 | $tn_ext = ''; |
---|
215 | $base_test = $dirname.'/'.$conf['dir_thumbnail'].'/'; |
---|
216 | $base_test.= $conf['prefix_thumbnail'].$filename_wo_ext.'.'; |
---|
217 | foreach ($conf['picture_ext'] as $ext) |
---|
218 | { |
---|
219 | if (isset($fs['thumbnails'][$base_test.$ext])) |
---|
220 | { |
---|
221 | $tn_ext = $ext; |
---|
222 | break; |
---|
223 | } |
---|
224 | } |
---|
225 | |
---|
226 | if (empty($tn_ext)) |
---|
227 | { |
---|
228 | array_push($wo_thumbnails, $path); |
---|
229 | } |
---|
230 | } |
---|
231 | } // next element |
---|
232 | } // next site id |
---|
233 | // +-----------------------------------------------------------------------+ |
---|
234 | // | thumbnails creation | |
---|
235 | // +-----------------------------------------------------------------------+ |
---|
236 | if (isset($_POST['submit'])) |
---|
237 | { |
---|
238 | $times = array(); |
---|
239 | $infos = array(); |
---|
240 | |
---|
241 | // checking criteria |
---|
242 | if (!preg_match('/^[0-9]{2,3}$/', $_POST['width']) or $_POST['width'] < 10) |
---|
243 | { |
---|
244 | array_push($page['errors'], l10n('width must be a number superior to').' 10'); |
---|
245 | } |
---|
246 | if (!preg_match('/^[0-9]{2,3}$/', $_POST['height']) or $_POST['height'] < 10) |
---|
247 | { |
---|
248 | array_push($page['errors'], l10n('height must be a number superior to').' 10'); |
---|
249 | } |
---|
250 | |
---|
251 | // picture miniaturization |
---|
252 | if (count($page['errors']) == 0) |
---|
253 | { |
---|
254 | $num = 1; |
---|
255 | foreach ($wo_thumbnails as $path) |
---|
256 | { |
---|
257 | if (is_numeric($_POST['n']) and $num > $_POST['n']) |
---|
258 | { |
---|
259 | break; |
---|
260 | } |
---|
261 | |
---|
262 | $starttime = get_moment(); |
---|
263 | if ($info = trigger_event('thumbnail_resize', |
---|
264 | false, |
---|
265 | $path, |
---|
266 | $_POST['width'], |
---|
267 | $_POST['height'], |
---|
268 | 'jpg' |
---|
269 | ) |
---|
270 | ) |
---|
271 | { |
---|
272 | $endtime = get_moment(); |
---|
273 | $info['time'] = ($endtime - $starttime) * 1000; |
---|
274 | array_push($infos, $info); |
---|
275 | array_push($times, $info['time']); |
---|
276 | array_push($thumbnalized, $path); |
---|
277 | $num++; |
---|
278 | } |
---|
279 | else |
---|
280 | { |
---|
281 | break; |
---|
282 | } |
---|
283 | } |
---|
284 | |
---|
285 | if (count($infos) > 0) |
---|
286 | { |
---|
287 | $sum = array_sum($times); |
---|
288 | $average = $sum / count($times); |
---|
289 | sort($times, SORT_NUMERIC); |
---|
290 | $max = array_pop($times); |
---|
291 | if (count($thumbnalized) == 1) |
---|
292 | { |
---|
293 | $min = $max; |
---|
294 | } |
---|
295 | else |
---|
296 | { |
---|
297 | $min = array_shift($times); |
---|
298 | } |
---|
299 | |
---|
300 | $tpl_var = |
---|
301 | array( |
---|
302 | 'TN_NB'=>count($infos), |
---|
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 | 'elements' => array() |
---|
308 | ); |
---|
309 | |
---|
310 | foreach ($infos as $i => $info) |
---|
311 | { |
---|
312 | $tpl_var['elements'][] = |
---|
313 | array( |
---|
314 | 'PATH'=>$info['path'], |
---|
315 | 'TN_FILE_IMG'=>$info['tn_file'], |
---|
316 | 'TN_FILESIZE_IMG'=>$info['tn_size'], |
---|
317 | 'TN_WIDTH_IMG'=>$info['tn_width'], |
---|
318 | 'TN_HEIGHT_IMG'=>$info['tn_height'], |
---|
319 | 'GEN_TIME'=>number_format($info['time'], 2, '.', ' ').' ms', |
---|
320 | ); |
---|
321 | } |
---|
322 | $template->assign('results', $tpl_var); |
---|
323 | } |
---|
324 | } |
---|
325 | } |
---|
326 | // +-----------------------------------------------------------------------+ |
---|
327 | // | form & pictures without thumbnails display | |
---|
328 | // +-----------------------------------------------------------------------+ |
---|
329 | $remainings = array_diff($wo_thumbnails, $thumbnalized); |
---|
330 | |
---|
331 | if (count($remainings) > 0) |
---|
332 | { |
---|
333 | $form_url = get_root_url().'admin.php?page=thumbnail'; |
---|
334 | $gd = !empty($_POST['gd']) ? $_POST['gd'] : 2; |
---|
335 | $width = !empty($_POST['width']) ? $_POST['width'] : $conf['tn_width']; |
---|
336 | $height = !empty($_POST['height']) ? $_POST['height'] : $conf['tn_height']; |
---|
337 | $n = !empty($_POST['n']) ? $_POST['n'] : 5; |
---|
338 | |
---|
339 | $template->assign( |
---|
340 | 'params', |
---|
341 | array( |
---|
342 | 'F_ACTION'=> $form_url, |
---|
343 | 'GD_SELECTED' => $gd, |
---|
344 | 'N_SELECTED' => $n, |
---|
345 | 'WIDTH_TN'=>$width, |
---|
346 | 'HEIGHT_TN'=>$height |
---|
347 | )); |
---|
348 | |
---|
349 | $template->assign( |
---|
350 | 'TOTAL_NB_REMAINING', |
---|
351 | count($remainings)); |
---|
352 | |
---|
353 | foreach ($remainings as $path) |
---|
354 | { |
---|
355 | list($width, $height) = getimagesize($path); |
---|
356 | $size = floor(filesize($path) / 1024).' KB'; |
---|
357 | |
---|
358 | $template->append( |
---|
359 | 'remainings', |
---|
360 | array( |
---|
361 | 'PATH'=>$path, |
---|
362 | 'FILESIZE_IMG'=>$size, |
---|
363 | 'WIDTH_IMG'=>$width, |
---|
364 | 'HEIGHT_IMG'=>$height, |
---|
365 | )); |
---|
366 | } |
---|
367 | } |
---|
368 | |
---|
369 | // +-----------------------------------------------------------------------+ |
---|
370 | // | return to admin | |
---|
371 | // +-----------------------------------------------------------------------+ |
---|
372 | $template->assign_var_from_handle('ADMIN_CONTENT', 'thumbnail'); |
---|
373 | ?> |
---|