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