1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | Piwigo - a PHP based photo gallery | |
---|
4 | // +-----------------------------------------------------------------------+ |
---|
5 | // | Copyright(C) 2008-2011 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 | // add default event handler for image and thumbnail resize |
---|
27 | add_event_handler('upload_image_resize', 'pwg_image_resize', EVENT_HANDLER_PRIORITY_NEUTRAL, 7); |
---|
28 | add_event_handler('upload_thumbnail_resize', 'pwg_image_resize', EVENT_HANDLER_PRIORITY_NEUTRAL, 7); |
---|
29 | |
---|
30 | function get_upload_form_config() |
---|
31 | { |
---|
32 | // default configuration for upload |
---|
33 | $upload_form_config = array( |
---|
34 | 'websize_resize' => array( |
---|
35 | 'default' => true, |
---|
36 | 'can_be_null' => false, |
---|
37 | ), |
---|
38 | |
---|
39 | 'websize_maxwidth' => array( |
---|
40 | 'default' => 800, |
---|
41 | 'min' => 100, |
---|
42 | 'max' => 1600, |
---|
43 | 'pattern' => '/^\d+$/', |
---|
44 | 'can_be_null' => true, |
---|
45 | 'error_message' => l10n('The websize maximum width must be a number between %d and %d'), |
---|
46 | ), |
---|
47 | |
---|
48 | 'websize_maxheight' => array( |
---|
49 | 'default' => 600, |
---|
50 | 'min' => 100, |
---|
51 | 'max' => 1200, |
---|
52 | 'pattern' => '/^\d+$/', |
---|
53 | 'can_be_null' => true, |
---|
54 | 'error_message' => l10n('The websize maximum height must be a number between %d and %d'), |
---|
55 | ), |
---|
56 | |
---|
57 | 'websize_quality' => array( |
---|
58 | 'default' => 95, |
---|
59 | 'min' => 50, |
---|
60 | 'max' => 100, |
---|
61 | 'pattern' => '/^\d+$/', |
---|
62 | 'can_be_null' => false, |
---|
63 | 'error_message' => l10n('The websize image quality must be a number between %d and %d'), |
---|
64 | ), |
---|
65 | |
---|
66 | 'thumb_maxwidth' => array( |
---|
67 | 'default' => 128, |
---|
68 | 'min' => 50, |
---|
69 | 'max' => 300, |
---|
70 | 'pattern' => '/^\d+$/', |
---|
71 | 'can_be_null' => false, |
---|
72 | 'error_message' => l10n('The thumbnail maximum width must be a number between %d and %d'), |
---|
73 | ), |
---|
74 | |
---|
75 | 'thumb_maxheight' => array( |
---|
76 | 'default' => 96, |
---|
77 | 'min' => 50, |
---|
78 | 'max' => 300, |
---|
79 | 'pattern' => '/^\d+$/', |
---|
80 | 'can_be_null' => false, |
---|
81 | 'error_message' => l10n('The thumbnail maximum height must be a number between %d and %d'), |
---|
82 | ), |
---|
83 | |
---|
84 | 'thumb_quality' => array( |
---|
85 | 'default' => 95, |
---|
86 | 'min' => 50, |
---|
87 | 'max' => 100, |
---|
88 | 'pattern' => '/^\d+$/', |
---|
89 | 'can_be_null' => false, |
---|
90 | 'error_message' => l10n('The thumbnail image quality must be a number between %d and %d'), |
---|
91 | ), |
---|
92 | |
---|
93 | 'hd_keep' => array( |
---|
94 | 'default' => true, |
---|
95 | 'can_be_null' => false, |
---|
96 | ), |
---|
97 | |
---|
98 | 'hd_resize' => array( |
---|
99 | 'default' => false, |
---|
100 | 'can_be_null' => false, |
---|
101 | ), |
---|
102 | |
---|
103 | 'hd_maxwidth' => array( |
---|
104 | 'default' => 2000, |
---|
105 | 'min' => 500, |
---|
106 | 'max' => 20000, |
---|
107 | 'pattern' => '/^\d+$/', |
---|
108 | 'can_be_null' => false, |
---|
109 | 'error_message' => l10n('The high definition maximum width must be a number between %d and %d'), |
---|
110 | ), |
---|
111 | |
---|
112 | 'hd_maxheight' => array( |
---|
113 | 'default' => 2000, |
---|
114 | 'min' => 500, |
---|
115 | 'max' => 20000, |
---|
116 | 'pattern' => '/^\d+$/', |
---|
117 | 'can_be_null' => false, |
---|
118 | 'error_message' => l10n('The high definition maximum height must be a number between %d and %d'), |
---|
119 | ), |
---|
120 | |
---|
121 | 'hd_quality' => array( |
---|
122 | 'default' => 95, |
---|
123 | 'min' => 50, |
---|
124 | 'max' => 100, |
---|
125 | 'pattern' => '/^\d+$/', |
---|
126 | 'can_be_null' => false, |
---|
127 | 'error_message' => l10n('The high definition image quality must be a number between %d and %d'), |
---|
128 | ), |
---|
129 | ); |
---|
130 | |
---|
131 | return $upload_form_config; |
---|
132 | } |
---|
133 | |
---|
134 | /* |
---|
135 | * automatic fill of configuration parameters |
---|
136 | */ |
---|
137 | function prepare_upload_configuration() |
---|
138 | { |
---|
139 | global $conf; |
---|
140 | |
---|
141 | $inserts = array(); |
---|
142 | |
---|
143 | foreach (get_upload_form_config() as $param_shortname => $param) |
---|
144 | { |
---|
145 | $param_name = 'upload_form_'.$param_shortname; |
---|
146 | |
---|
147 | if (!isset($conf[$param_name])) |
---|
148 | { |
---|
149 | $conf[$param_name] = $param['default']; |
---|
150 | |
---|
151 | array_push( |
---|
152 | $inserts, |
---|
153 | array( |
---|
154 | 'param' => $param_name, |
---|
155 | 'value' => boolean_to_string($param['default']), |
---|
156 | ) |
---|
157 | ); |
---|
158 | } |
---|
159 | } |
---|
160 | |
---|
161 | if (count($inserts) > 0) |
---|
162 | { |
---|
163 | mass_inserts( |
---|
164 | CONFIG_TABLE, |
---|
165 | array_keys($inserts[0]), |
---|
166 | $inserts |
---|
167 | ); |
---|
168 | } |
---|
169 | } |
---|
170 | |
---|
171 | function add_uploaded_file($source_filepath, $original_filename=null, $categories=null, $level=null, $image_id=null) |
---|
172 | { |
---|
173 | // Here is the plan |
---|
174 | // |
---|
175 | // 1) move uploaded file to upload/2010/01/22/20100122003814-449ada00.jpg |
---|
176 | // |
---|
177 | // 2) if taller than max_height or wider than max_width, move to pwg_high |
---|
178 | // + web sized creation |
---|
179 | // |
---|
180 | // 3) thumbnail creation from web sized |
---|
181 | // |
---|
182 | // 4) register in database |
---|
183 | |
---|
184 | // TODO |
---|
185 | // * check md5sum (already exists?) |
---|
186 | |
---|
187 | global $conf, $user; |
---|
188 | |
---|
189 | $md5sum = md5_file($source_filepath); |
---|
190 | $file_path = null; |
---|
191 | |
---|
192 | if (isset($image_id)) |
---|
193 | { |
---|
194 | // we are performing an update |
---|
195 | $query = ' |
---|
196 | SELECT |
---|
197 | path |
---|
198 | FROM '.IMAGES_TABLE.' |
---|
199 | WHERE id = '.$image_id.' |
---|
200 | ;'; |
---|
201 | $result = pwg_query($query); |
---|
202 | while ($row = pwg_db_fetch_assoc($result)) |
---|
203 | { |
---|
204 | $file_path = $row['path']; |
---|
205 | } |
---|
206 | |
---|
207 | if (!isset($file_path)) |
---|
208 | { |
---|
209 | die('['.__FUNCTION__.'] this photo does not exist in the database'); |
---|
210 | } |
---|
211 | |
---|
212 | // delete all physical files related to the photo (thumbnail, web site, HD) |
---|
213 | delete_element_files(array($image_id)); |
---|
214 | } |
---|
215 | else |
---|
216 | { |
---|
217 | // this photo is new |
---|
218 | |
---|
219 | // current date |
---|
220 | list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();')); |
---|
221 | list($year, $month, $day) = preg_split('/[^\d]/', $dbnow, 4); |
---|
222 | |
---|
223 | // upload directory hierarchy |
---|
224 | $upload_dir = sprintf( |
---|
225 | PHPWG_ROOT_PATH.$conf['upload_dir'].'/%s/%s/%s', |
---|
226 | $year, |
---|
227 | $month, |
---|
228 | $day |
---|
229 | ); |
---|
230 | |
---|
231 | // compute file path |
---|
232 | $date_string = preg_replace('/[^\d]/', '', $dbnow); |
---|
233 | $random_string = substr($md5sum, 0, 8); |
---|
234 | $filename_wo_ext = $date_string.'-'.$random_string; |
---|
235 | $file_path = $upload_dir.'/'.$filename_wo_ext.'.'; |
---|
236 | |
---|
237 | list($width, $height, $type) = getimagesize($source_filepath); |
---|
238 | if (IMAGETYPE_PNG == $type) |
---|
239 | { |
---|
240 | $file_path.= 'png'; |
---|
241 | } |
---|
242 | elseif (IMAGETYPE_GIF == $type) |
---|
243 | { |
---|
244 | $file_path.= 'gif'; |
---|
245 | } |
---|
246 | else |
---|
247 | { |
---|
248 | $file_path.= 'jpg'; |
---|
249 | } |
---|
250 | |
---|
251 | prepare_directory($upload_dir); |
---|
252 | } |
---|
253 | |
---|
254 | if (is_uploaded_file($source_filepath)) |
---|
255 | { |
---|
256 | move_uploaded_file($source_filepath, $file_path); |
---|
257 | } |
---|
258 | else |
---|
259 | { |
---|
260 | copy($source_filepath, $file_path); |
---|
261 | } |
---|
262 | |
---|
263 | if ($conf['upload_form_websize_resize'] |
---|
264 | and need_resize($file_path, $conf['upload_form_websize_maxwidth'], $conf['upload_form_websize_maxheight'])) |
---|
265 | { |
---|
266 | $high_path = file_path_for_type($file_path, 'high'); |
---|
267 | $high_dir = dirname($high_path); |
---|
268 | prepare_directory($high_dir); |
---|
269 | |
---|
270 | rename($file_path, $high_path); |
---|
271 | $high_infos = pwg_image_infos($high_path); |
---|
272 | |
---|
273 | trigger_event( |
---|
274 | 'upload_image_resize', |
---|
275 | false, |
---|
276 | $high_path, |
---|
277 | $file_path, |
---|
278 | $conf['upload_form_websize_maxwidth'], |
---|
279 | $conf['upload_form_websize_maxheight'], |
---|
280 | $conf['upload_form_websize_quality'], |
---|
281 | false |
---|
282 | ); |
---|
283 | |
---|
284 | if (is_imagick()) |
---|
285 | { |
---|
286 | if ($conf['upload_form_hd_keep']) |
---|
287 | { |
---|
288 | if ($conf['upload_form_hd_resize']) |
---|
289 | { |
---|
290 | $need_resize = need_resize($high_path, $conf['upload_form_hd_maxwidth'], $conf['upload_form_hd_maxheight']); |
---|
291 | |
---|
292 | if ($need_resize) |
---|
293 | { |
---|
294 | pwg_image_resize( |
---|
295 | false, |
---|
296 | $high_path, |
---|
297 | $high_path, |
---|
298 | $conf['upload_form_hd_maxwidth'], |
---|
299 | $conf['upload_form_hd_maxheight'], |
---|
300 | $conf['upload_form_hd_quality'], |
---|
301 | false |
---|
302 | ); |
---|
303 | $high_infos = pwg_image_infos($high_path); |
---|
304 | } |
---|
305 | } |
---|
306 | } |
---|
307 | else |
---|
308 | { |
---|
309 | unlink($high_path); |
---|
310 | $high_infos = null; |
---|
311 | } |
---|
312 | } |
---|
313 | } |
---|
314 | |
---|
315 | $file_infos = pwg_image_infos($file_path); |
---|
316 | |
---|
317 | $thumb_path = file_path_for_type($file_path, 'thumb'); |
---|
318 | $thumb_dir = dirname($thumb_path); |
---|
319 | prepare_directory($thumb_dir); |
---|
320 | |
---|
321 | trigger_event( |
---|
322 | 'upload_thumbnail_resize', |
---|
323 | false, |
---|
324 | $file_path, |
---|
325 | $thumb_path, |
---|
326 | $conf['upload_form_thumb_maxwidth'], |
---|
327 | $conf['upload_form_thumb_maxheight'], |
---|
328 | $conf['upload_form_thumb_quality'], |
---|
329 | true |
---|
330 | ); |
---|
331 | |
---|
332 | $thumb_infos = pwg_image_infos($thumb_path); |
---|
333 | |
---|
334 | if (isset($image_id)) |
---|
335 | { |
---|
336 | $update = array( |
---|
337 | 'id' => $image_id, |
---|
338 | 'file' => pwg_db_real_escape_string(isset($original_filename) ? $original_filename : basename($file_path)), |
---|
339 | 'filesize' => $file_infos['filesize'], |
---|
340 | 'width' => $file_infos['width'], |
---|
341 | 'height' => $file_infos['height'], |
---|
342 | 'md5sum' => $md5sum, |
---|
343 | 'added_by' => $user['id'], |
---|
344 | ); |
---|
345 | |
---|
346 | if (isset($high_infos)) |
---|
347 | { |
---|
348 | $update['has_high'] = 'true'; |
---|
349 | $update['high_filesize'] = $high_infos['filesize']; |
---|
350 | $update['high_width'] = $high_infos['width']; |
---|
351 | $update['high_height'] = $high_infos['height']; |
---|
352 | } |
---|
353 | else |
---|
354 | { |
---|
355 | $update['has_high'] = 'false'; |
---|
356 | $update['high_filesize'] = null; |
---|
357 | $update['high_width'] = null; |
---|
358 | $update['high_height'] = null; |
---|
359 | } |
---|
360 | |
---|
361 | if (isset($level)) |
---|
362 | { |
---|
363 | $update['level'] = $level; |
---|
364 | } |
---|
365 | |
---|
366 | mass_updates( |
---|
367 | IMAGES_TABLE, |
---|
368 | array( |
---|
369 | 'primary' => array('id'), |
---|
370 | 'update' => array_keys($update) |
---|
371 | ), |
---|
372 | array($update) |
---|
373 | ); |
---|
374 | } |
---|
375 | else |
---|
376 | { |
---|
377 | // database registration |
---|
378 | $insert = array( |
---|
379 | 'file' => pwg_db_real_escape_string(isset($original_filename) ? $original_filename : basename($file_path)), |
---|
380 | 'date_available' => $dbnow, |
---|
381 | 'tn_ext' => 'jpg', |
---|
382 | 'path' => preg_replace('#^'.preg_quote(PHPWG_ROOT_PATH).'#', '', $file_path), |
---|
383 | 'filesize' => $file_infos['filesize'], |
---|
384 | 'width' => $file_infos['width'], |
---|
385 | 'height' => $file_infos['height'], |
---|
386 | 'md5sum' => $md5sum, |
---|
387 | 'added_by' => $user['id'], |
---|
388 | ); |
---|
389 | |
---|
390 | if (isset($high_infos)) |
---|
391 | { |
---|
392 | $insert['has_high'] = 'true'; |
---|
393 | $insert['high_filesize'] = $high_infos['filesize']; |
---|
394 | $insert['high_width'] = $high_infos['width']; |
---|
395 | $insert['high_height'] = $high_infos['height']; |
---|
396 | } |
---|
397 | |
---|
398 | if (isset($level)) |
---|
399 | { |
---|
400 | $insert['level'] = $level; |
---|
401 | } |
---|
402 | |
---|
403 | mass_inserts( |
---|
404 | IMAGES_TABLE, |
---|
405 | array_keys($insert), |
---|
406 | array($insert) |
---|
407 | ); |
---|
408 | |
---|
409 | $image_id = pwg_db_insert_id(IMAGES_TABLE); |
---|
410 | } |
---|
411 | |
---|
412 | if (isset($categories) and count($categories) > 0) |
---|
413 | { |
---|
414 | associate_images_to_categories( |
---|
415 | array($image_id), |
---|
416 | $categories |
---|
417 | ); |
---|
418 | } |
---|
419 | |
---|
420 | // update metadata from the uploaded file (exif/iptc) |
---|
421 | if ($conf['use_exif'] and !function_exists('read_exif_data')) |
---|
422 | { |
---|
423 | $conf['use_exif'] = false; |
---|
424 | } |
---|
425 | update_metadata(array($image_id=>$file_path)); |
---|
426 | |
---|
427 | invalidate_user_cache(); |
---|
428 | |
---|
429 | return $image_id; |
---|
430 | } |
---|
431 | |
---|
432 | function prepare_directory($directory) |
---|
433 | { |
---|
434 | if (!is_dir($directory)) { |
---|
435 | if (substr(PHP_OS, 0, 3) == 'WIN') |
---|
436 | { |
---|
437 | $directory = str_replace('/', DIRECTORY_SEPARATOR, $directory); |
---|
438 | } |
---|
439 | umask(0000); |
---|
440 | $recursive = true; |
---|
441 | if (!@mkdir($directory, 0777, $recursive)) |
---|
442 | { |
---|
443 | die('[prepare_directory] cannot create directory "'.$directory.'"'); |
---|
444 | } |
---|
445 | } |
---|
446 | |
---|
447 | if (!is_writable($directory)) |
---|
448 | { |
---|
449 | // last chance to make the directory writable |
---|
450 | @chmod($directory, 0777); |
---|
451 | |
---|
452 | if (!is_writable($directory)) |
---|
453 | { |
---|
454 | die('[prepare_directory] directory "'.$directory.'" has no write access'); |
---|
455 | } |
---|
456 | } |
---|
457 | |
---|
458 | secure_directory($directory); |
---|
459 | } |
---|
460 | |
---|
461 | function need_resize($image_filepath, $max_width, $max_height) |
---|
462 | { |
---|
463 | // TODO : the resize check should take the orientation into account. If a |
---|
464 | // rotation must be applied to the resized photo, then we should test |
---|
465 | // invert width and height. |
---|
466 | list($width, $height) = getimagesize($image_filepath); |
---|
467 | |
---|
468 | if ($width > $max_width or $height > $max_height) |
---|
469 | { |
---|
470 | return true; |
---|
471 | } |
---|
472 | |
---|
473 | return false; |
---|
474 | } |
---|
475 | |
---|
476 | function get_resize_dimensions($width, $height, $max_width, $max_height, $rotation=null) |
---|
477 | { |
---|
478 | $rotate_for_dimensions = false; |
---|
479 | if (isset($rotation) and in_array(abs($rotation), array(90, 270))) |
---|
480 | { |
---|
481 | $rotate_for_dimensions = true; |
---|
482 | } |
---|
483 | |
---|
484 | if ($rotate_for_dimensions) |
---|
485 | { |
---|
486 | list($width, $height) = array($height, $width); |
---|
487 | } |
---|
488 | |
---|
489 | $ratio_width = $width / $max_width; |
---|
490 | $ratio_height = $height / $max_height; |
---|
491 | $destination_width = $width; |
---|
492 | $destination_height = $height; |
---|
493 | |
---|
494 | // maximal size exceeded ? |
---|
495 | if ($ratio_width > 1 or $ratio_height > 1) |
---|
496 | { |
---|
497 | if ($ratio_width < $ratio_height) |
---|
498 | { |
---|
499 | $destination_width = ceil($width / $ratio_height); |
---|
500 | $destination_height = $max_height; |
---|
501 | } |
---|
502 | else |
---|
503 | { |
---|
504 | $destination_width = $max_width; |
---|
505 | $destination_height = ceil($height / $ratio_width); |
---|
506 | } |
---|
507 | } |
---|
508 | |
---|
509 | if ($rotate_for_dimensions) |
---|
510 | { |
---|
511 | list($destination_width, $destination_height) = array($destination_height, $destination_width); |
---|
512 | } |
---|
513 | |
---|
514 | return array( |
---|
515 | 'width' => $destination_width, |
---|
516 | 'height'=> $destination_height, |
---|
517 | ); |
---|
518 | } |
---|
519 | |
---|
520 | function pwg_image_resize($result, $source_filepath, $destination_filepath, $max_width, $max_height, $quality, $strip_metadata=false) |
---|
521 | { |
---|
522 | if ($result !== false) |
---|
523 | { |
---|
524 | //someone hooked us - so we skip |
---|
525 | return $result; |
---|
526 | } |
---|
527 | |
---|
528 | $extension = strtolower(get_extension($source_filepath)); |
---|
529 | |
---|
530 | if (is_imagick() and $extension != 'gif') |
---|
531 | { |
---|
532 | return pwg_image_resize_im($source_filepath, $destination_filepath, $max_width, $max_height, $quality, $strip_metadata); |
---|
533 | } |
---|
534 | else |
---|
535 | { |
---|
536 | return pwg_image_resize_gd($source_filepath, $destination_filepath, $max_width, $max_height, $quality); |
---|
537 | } |
---|
538 | } |
---|
539 | |
---|
540 | function pwg_image_resize_gd($source_filepath, $destination_filepath, $max_width, $max_height, $quality) |
---|
541 | { |
---|
542 | if (!function_exists('gd_info')) |
---|
543 | { |
---|
544 | return false; |
---|
545 | } |
---|
546 | |
---|
547 | $gd_info = gd_info(); |
---|
548 | |
---|
549 | // extension of the picture filename |
---|
550 | $extension = strtolower(get_extension($source_filepath)); |
---|
551 | |
---|
552 | $source_image = null; |
---|
553 | if (in_array($extension, array('jpg', 'jpeg'))) |
---|
554 | { |
---|
555 | $source_image = imagecreatefromjpeg($source_filepath); |
---|
556 | } |
---|
557 | else if ($extension == 'png') |
---|
558 | { |
---|
559 | $source_image = imagecreatefrompng($source_filepath); |
---|
560 | } |
---|
561 | elseif ($extension == 'gif' and $gd_info['GIF Read Support'] and $gd_info['GIF Create Support']) |
---|
562 | { |
---|
563 | $source_image = imagecreatefromgif($source_filepath); |
---|
564 | } |
---|
565 | else |
---|
566 | { |
---|
567 | die('[GD] unsupported file extension'); |
---|
568 | } |
---|
569 | |
---|
570 | $rotation = null; |
---|
571 | if (function_exists('imagerotate')) |
---|
572 | { |
---|
573 | $rotation = get_rotation_angle($source_filepath); |
---|
574 | } |
---|
575 | |
---|
576 | // width/height |
---|
577 | $source_width = imagesx($source_image); |
---|
578 | $source_height = imagesy($source_image); |
---|
579 | |
---|
580 | $resize_dimensions = get_resize_dimensions($source_width, $source_height, $max_width, $max_height, $rotation); |
---|
581 | |
---|
582 | // testing on height is useless in theory: if width is unchanged, there |
---|
583 | // should be no resize, because width/height ratio is not modified. |
---|
584 | if ($resize_dimensions['width'] == $source_width and $resize_dimensions['height'] == $source_height) |
---|
585 | { |
---|
586 | // the image doesn't need any resize! We just copy it to the destination |
---|
587 | copy($source_filepath, $destination_filepath); |
---|
588 | return true; |
---|
589 | } |
---|
590 | |
---|
591 | $destination_image = imagecreatetruecolor($resize_dimensions['width'], $resize_dimensions['height']); |
---|
592 | |
---|
593 | imagecopyresampled( |
---|
594 | $destination_image, |
---|
595 | $source_image, |
---|
596 | 0, |
---|
597 | 0, |
---|
598 | 0, |
---|
599 | 0, |
---|
600 | $resize_dimensions['width'], |
---|
601 | $resize_dimensions['height'], |
---|
602 | $source_width, |
---|
603 | $source_height |
---|
604 | ); |
---|
605 | |
---|
606 | // rotation occurs only on resized photo to avoid useless memory use |
---|
607 | if (isset($rotation)) |
---|
608 | { |
---|
609 | $destination_image = imagerotate($destination_image, $rotation, 0); |
---|
610 | } |
---|
611 | |
---|
612 | $extension = strtolower(get_extension($destination_filepath)); |
---|
613 | if ($extension == 'png') |
---|
614 | { |
---|
615 | imagepng($destination_image, $destination_filepath); |
---|
616 | } |
---|
617 | elseif ($extension == 'gif') |
---|
618 | { |
---|
619 | imagegif($destination_image, $destination_filepath); |
---|
620 | } |
---|
621 | else |
---|
622 | { |
---|
623 | imagejpeg($destination_image, $destination_filepath, $quality); |
---|
624 | } |
---|
625 | // freeing memory ressources |
---|
626 | imagedestroy($source_image); |
---|
627 | imagedestroy($destination_image); |
---|
628 | |
---|
629 | // everything should be OK if we are here! |
---|
630 | return true; |
---|
631 | } |
---|
632 | |
---|
633 | function pwg_image_resize_im($source_filepath, $destination_filepath, $max_width, $max_height, $quality, $strip_metadata=false) |
---|
634 | { |
---|
635 | // extension of the picture filename |
---|
636 | $extension = strtolower(get_extension($source_filepath)); |
---|
637 | if (!in_array($extension, array('jpg', 'jpeg', 'png'))) |
---|
638 | { |
---|
639 | die('[Imagick] unsupported file extension'); |
---|
640 | } |
---|
641 | |
---|
642 | $image = new Imagick($source_filepath); |
---|
643 | |
---|
644 | $rotation = get_rotation_angle($source_filepath); |
---|
645 | |
---|
646 | // width/height |
---|
647 | $source_width = $image->getImageWidth(); |
---|
648 | $source_height = $image->getImageHeight(); |
---|
649 | |
---|
650 | $resize_dimensions = get_resize_dimensions($source_width, $source_height, $max_width, $max_height, $rotation); |
---|
651 | |
---|
652 | // testing on height is useless in theory: if width is unchanged, there |
---|
653 | // should be no resize, because width/height ratio is not modified. |
---|
654 | if ($resize_dimensions['width'] == $source_width and $resize_dimensions['height'] == $source_height) |
---|
655 | { |
---|
656 | // the image doesn't need any resize! We just copy it to the destination |
---|
657 | copy($source_filepath, $destination_filepath); |
---|
658 | return true; |
---|
659 | } |
---|
660 | |
---|
661 | $image->setImageCompressionQuality($quality); |
---|
662 | $image->setInterlaceScheme(Imagick::INTERLACE_LINE); |
---|
663 | |
---|
664 | if ($strip_metadata) |
---|
665 | { |
---|
666 | // we save a few kilobytes. For example a thumbnail with metadata |
---|
667 | // weights 25KB, without metadata 7KB. |
---|
668 | $image->stripImage(); |
---|
669 | } |
---|
670 | |
---|
671 | $image->resizeImage($resize_dimensions['width'], $resize_dimensions['height'], Imagick::FILTER_LANCZOS, 0.9); |
---|
672 | |
---|
673 | if (isset($rotation)) |
---|
674 | { |
---|
675 | $image->rotateImage(new ImagickPixel(), -$rotation); |
---|
676 | $image->setImageOrientation(Imagick::ORIENTATION_TOPLEFT); |
---|
677 | } |
---|
678 | |
---|
679 | $image->writeImage($destination_filepath); |
---|
680 | $image->destroy(); |
---|
681 | |
---|
682 | // everything should be OK if we are here! |
---|
683 | return true; |
---|
684 | } |
---|
685 | |
---|
686 | function get_rotation_angle($source_filepath) |
---|
687 | { |
---|
688 | global $conf; |
---|
689 | |
---|
690 | if (!$conf['upload_form_automatic_rotation']) |
---|
691 | { |
---|
692 | return null; |
---|
693 | } |
---|
694 | |
---|
695 | list($width, $height, $type) = getimagesize($source_filepath); |
---|
696 | if (IMAGETYPE_JPEG != $type) |
---|
697 | { |
---|
698 | return null; |
---|
699 | } |
---|
700 | |
---|
701 | if (!function_exists('exif_read_data')) |
---|
702 | { |
---|
703 | return null; |
---|
704 | } |
---|
705 | |
---|
706 | $rotation = null; |
---|
707 | |
---|
708 | $exif = exif_read_data($source_filepath); |
---|
709 | |
---|
710 | if (isset($exif['Orientation']) and preg_match('/^\s*(\d)/', $exif['Orientation'], $matches)) |
---|
711 | { |
---|
712 | $orientation = $matches[1]; |
---|
713 | if (in_array($orientation, array(3, 4))) |
---|
714 | { |
---|
715 | $rotation = 180; |
---|
716 | } |
---|
717 | elseif (in_array($orientation, array(5, 6))) |
---|
718 | { |
---|
719 | $rotation = 270; |
---|
720 | } |
---|
721 | elseif (in_array($orientation, array(7, 8))) |
---|
722 | { |
---|
723 | $rotation = 90; |
---|
724 | } |
---|
725 | } |
---|
726 | |
---|
727 | return $rotation; |
---|
728 | } |
---|
729 | |
---|
730 | function pwg_image_infos($path) |
---|
731 | { |
---|
732 | list($width, $height) = getimagesize($path); |
---|
733 | $filesize = floor(filesize($path)/1024); |
---|
734 | |
---|
735 | return array( |
---|
736 | 'width' => $width, |
---|
737 | 'height' => $height, |
---|
738 | 'filesize' => $filesize, |
---|
739 | ); |
---|
740 | } |
---|
741 | |
---|
742 | function is_valid_image_extension($extension) |
---|
743 | { |
---|
744 | return in_array(strtolower($extension), array('jpg', 'jpeg', 'png', 'gif')); |
---|
745 | } |
---|
746 | |
---|
747 | function file_upload_error_message($error_code) |
---|
748 | { |
---|
749 | switch ($error_code) { |
---|
750 | case UPLOAD_ERR_INI_SIZE: |
---|
751 | return sprintf( |
---|
752 | l10n('The uploaded file exceeds the upload_max_filesize directive in php.ini: %sB'), |
---|
753 | get_ini_size('upload_max_filesize', false) |
---|
754 | ); |
---|
755 | case UPLOAD_ERR_FORM_SIZE: |
---|
756 | return l10n('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'); |
---|
757 | case UPLOAD_ERR_PARTIAL: |
---|
758 | return l10n('The uploaded file was only partially uploaded'); |
---|
759 | case UPLOAD_ERR_NO_FILE: |
---|
760 | return l10n('No file was uploaded'); |
---|
761 | case UPLOAD_ERR_NO_TMP_DIR: |
---|
762 | return l10n('Missing a temporary folder'); |
---|
763 | case UPLOAD_ERR_CANT_WRITE: |
---|
764 | return l10n('Failed to write file to disk'); |
---|
765 | case UPLOAD_ERR_EXTENSION: |
---|
766 | return l10n('File upload stopped by extension'); |
---|
767 | default: |
---|
768 | return l10n('Unknown upload error'); |
---|
769 | } |
---|
770 | } |
---|
771 | |
---|
772 | function get_ini_size($ini_key, $in_bytes=true) |
---|
773 | { |
---|
774 | $size = ini_get($ini_key); |
---|
775 | |
---|
776 | if ($in_bytes) |
---|
777 | { |
---|
778 | $size = convert_shortand_notation_to_bytes($size); |
---|
779 | } |
---|
780 | |
---|
781 | return $size; |
---|
782 | } |
---|
783 | |
---|
784 | function convert_shortand_notation_to_bytes($value) |
---|
785 | { |
---|
786 | $suffix = substr($value, -1); |
---|
787 | $multiply_by = null; |
---|
788 | |
---|
789 | if ('K' == $suffix) |
---|
790 | { |
---|
791 | $multiply_by = 1024; |
---|
792 | } |
---|
793 | else if ('M' == $suffix) |
---|
794 | { |
---|
795 | $multiply_by = 1024*1024; |
---|
796 | } |
---|
797 | else if ('G' == $suffix) |
---|
798 | { |
---|
799 | $multiply_by = 1024*1024*1024; |
---|
800 | } |
---|
801 | |
---|
802 | if (isset($multiply_by)) |
---|
803 | { |
---|
804 | $value = substr($value, 0, -1); |
---|
805 | $value*= $multiply_by; |
---|
806 | } |
---|
807 | |
---|
808 | return $value; |
---|
809 | } |
---|
810 | |
---|
811 | function add_upload_error($upload_id, $error_message) |
---|
812 | { |
---|
813 | if (!isset($_SESSION['uploads_error'])) |
---|
814 | { |
---|
815 | $_SESSION['uploads_error'] = array(); |
---|
816 | } |
---|
817 | if (!isset($_SESSION['uploads_error'][$upload_id])) |
---|
818 | { |
---|
819 | $_SESSION['uploads_error'][$upload_id] = array(); |
---|
820 | } |
---|
821 | |
---|
822 | array_push($_SESSION['uploads_error'][$upload_id], $error_message); |
---|
823 | } |
---|
824 | |
---|
825 | function is_imagick() |
---|
826 | { |
---|
827 | if (class_exists('Imagick')) |
---|
828 | { |
---|
829 | return true; |
---|
830 | } |
---|
831 | |
---|
832 | return false; |
---|
833 | } |
---|
834 | |
---|
835 | function ready_for_upload_message() |
---|
836 | { |
---|
837 | global $conf; |
---|
838 | |
---|
839 | $relative_dir = preg_replace('#^'.PHPWG_ROOT_PATH.'#', '', $conf['upload_dir']); |
---|
840 | |
---|
841 | if (!is_dir($conf['upload_dir'])) |
---|
842 | { |
---|
843 | if (!is_writable(dirname($conf['upload_dir']))) |
---|
844 | { |
---|
845 | return sprintf( |
---|
846 | l10n('Create the "%s" directory at the root of your Piwigo installation'), |
---|
847 | $relative_dir |
---|
848 | ); |
---|
849 | } |
---|
850 | } |
---|
851 | else |
---|
852 | { |
---|
853 | if (!is_writable($conf['upload_dir'])) |
---|
854 | { |
---|
855 | @chmod($conf['upload_dir'], 0777); |
---|
856 | |
---|
857 | if (!is_writable($conf['upload_dir'])) |
---|
858 | { |
---|
859 | return sprintf( |
---|
860 | l10n('Give write access (chmod 777) to "%s" directory at the root of your Piwigo installation'), |
---|
861 | $relative_dir |
---|
862 | ); |
---|
863 | } |
---|
864 | } |
---|
865 | } |
---|
866 | |
---|
867 | return null; |
---|
868 | } |
---|
869 | |
---|
870 | function file_path_for_type($file_path, $type='thumb') |
---|
871 | { |
---|
872 | // resolve the $file_path depending on the $type |
---|
873 | if ('thumb' == $type) { |
---|
874 | $file_path = get_thumbnail_location( |
---|
875 | array( |
---|
876 | 'path' => $file_path, |
---|
877 | 'tn_ext' => 'jpg', |
---|
878 | ) |
---|
879 | ); |
---|
880 | } |
---|
881 | |
---|
882 | if ('high' == $type) { |
---|
883 | @include_once(PHPWG_ROOT_PATH.'include/functions_picture.inc.php'); |
---|
884 | $file_path = get_high_location( |
---|
885 | array( |
---|
886 | 'path' => $file_path, |
---|
887 | 'has_high' => 'true' |
---|
888 | ) |
---|
889 | ); |
---|
890 | } |
---|
891 | |
---|
892 | return $file_path; |
---|
893 | } |
---|
894 | ?> |
---|