[5089] | 1 | <?php |
---|
[8249] | 2 | // +-----------------------------------------------------------------------+ |
---|
[8728] | 3 | // | Piwigo - a PHP based photo gallery | |
---|
[8249] | 4 | // +-----------------------------------------------------------------------+ |
---|
[8728] | 5 | // | Copyright(C) 2008-2011 Piwigo Team http://piwigo.org | |
---|
[8249] | 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 | // +-----------------------------------------------------------------------+ |
---|
[5089] | 23 | |
---|
| 24 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
| 25 | |
---|
[5957] | 26 | // add default event handler for image and thumbnail resize |
---|
[8219] | 27 | add_event_handler('upload_image_resize', 'pwg_image_resize', EVENT_HANDLER_PRIORITY_NEUTRAL, 7); |
---|
[10552] | 28 | add_event_handler('upload_thumbnail_resize', 'pwg_image_resize', EVENT_HANDLER_PRIORITY_NEUTRAL, 9); |
---|
[5957] | 29 | |
---|
[8249] | 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 | ), |
---|
[10552] | 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 | ), |
---|
[8249] | 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 | { |
---|
[8253] | 159 | $conf[$param_name] = $param['default']; |
---|
[8249] | 160 | |
---|
| 161 | array_push( |
---|
| 162 | $inserts, |
---|
| 163 | array( |
---|
| 164 | 'param' => $param_name, |
---|
[8253] | 165 | 'value' => boolean_to_string($param['default']), |
---|
[8249] | 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 | |
---|
[9191] | 181 | function add_uploaded_file($source_filepath, $original_filename=null, $categories=null, $level=null, $image_id=null) |
---|
[5089] | 182 | { |
---|
[8249] | 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 | |
---|
[8464] | 197 | global $conf, $user; |
---|
[5089] | 198 | |
---|
| 199 | $md5sum = md5_file($source_filepath); |
---|
[9191] | 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 | } |
---|
[5089] | 221 | |
---|
[9191] | 222 | // delete all physical files related to the photo (thumbnail, web site, HD) |
---|
| 223 | delete_element_files(array($image_id)); |
---|
[6617] | 224 | } |
---|
| 225 | else |
---|
| 226 | { |
---|
[9191] | 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 | } |
---|
[9952] | 252 | elseif (IMAGETYPE_GIF == $type) |
---|
| 253 | { |
---|
| 254 | $file_path.= 'gif'; |
---|
| 255 | } |
---|
[9191] | 256 | else |
---|
| 257 | { |
---|
| 258 | $file_path.= 'jpg'; |
---|
| 259 | } |
---|
| 260 | |
---|
| 261 | prepare_directory($upload_dir); |
---|
[6617] | 262 | } |
---|
| 263 | |
---|
[5089] | 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 | |
---|
[8219] | 283 | trigger_event( |
---|
| 284 | 'upload_image_resize', |
---|
[5957] | 285 | false, |
---|
[5089] | 286 | $high_path, |
---|
| 287 | $file_path, |
---|
| 288 | $conf['upload_form_websize_maxwidth'], |
---|
| 289 | $conf['upload_form_websize_maxheight'], |
---|
[8219] | 290 | $conf['upload_form_websize_quality'], |
---|
| 291 | false |
---|
[5089] | 292 | ); |
---|
[8227] | 293 | |
---|
| 294 | if (is_imagick()) |
---|
| 295 | { |
---|
| 296 | if ($conf['upload_form_hd_keep']) |
---|
| 297 | { |
---|
[8249] | 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']); |
---|
[8227] | 301 | |
---|
[8249] | 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 | } |
---|
[8227] | 315 | } |
---|
| 316 | } |
---|
| 317 | else |
---|
| 318 | { |
---|
| 319 | unlink($high_path); |
---|
| 320 | $high_infos = null; |
---|
| 321 | } |
---|
| 322 | } |
---|
[5089] | 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 | |
---|
[8219] | 331 | trigger_event( |
---|
| 332 | 'upload_thumbnail_resize', |
---|
[5957] | 333 | false, |
---|
[5089] | 334 | $file_path, |
---|
| 335 | $thumb_path, |
---|
| 336 | $conf['upload_form_thumb_maxwidth'], |
---|
| 337 | $conf['upload_form_thumb_maxheight'], |
---|
[8219] | 338 | $conf['upload_form_thumb_quality'], |
---|
[10552] | 339 | true, |
---|
| 340 | $conf['upload_form_thumb_crop'], |
---|
| 341 | $conf['upload_form_thumb_follow_orientation'] |
---|
[5089] | 342 | ); |
---|
| 343 | |
---|
| 344 | $thumb_infos = pwg_image_infos($thumb_path); |
---|
| 345 | |
---|
[9191] | 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']; |
---|
[10157] | 362 | $update['high_width'] = $high_infos['width']; |
---|
| 363 | $update['high_height'] = $high_infos['height']; |
---|
[9191] | 364 | } |
---|
| 365 | else |
---|
| 366 | { |
---|
| 367 | $update['has_high'] = 'false'; |
---|
| 368 | $update['high_filesize'] = null; |
---|
[10157] | 369 | $update['high_width'] = null; |
---|
| 370 | $update['high_height'] = null; |
---|
[9191] | 371 | } |
---|
[5089] | 372 | |
---|
[9191] | 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 |
---|
[5089] | 388 | { |
---|
[9191] | 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 | ); |
---|
[5089] | 401 | |
---|
[9191] | 402 | if (isset($high_infos)) |
---|
| 403 | { |
---|
| 404 | $insert['has_high'] = 'true'; |
---|
| 405 | $insert['high_filesize'] = $high_infos['filesize']; |
---|
[10157] | 406 | $insert['high_width'] = $high_infos['width']; |
---|
| 407 | $insert['high_height'] = $high_infos['height']; |
---|
[9191] | 408 | } |
---|
| 409 | |
---|
| 410 | if (isset($level)) |
---|
| 411 | { |
---|
| 412 | $insert['level'] = $level; |
---|
| 413 | } |
---|
[5089] | 414 | |
---|
[9191] | 415 | mass_inserts( |
---|
| 416 | IMAGES_TABLE, |
---|
| 417 | array_keys($insert), |
---|
| 418 | array($insert) |
---|
| 419 | ); |
---|
[5089] | 420 | |
---|
[9191] | 421 | $image_id = pwg_db_insert_id(IMAGES_TABLE); |
---|
| 422 | } |
---|
[5089] | 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) |
---|
[6622] | 433 | if ($conf['use_exif'] and !function_exists('read_exif_data')) |
---|
| 434 | { |
---|
| 435 | $conf['use_exif'] = false; |
---|
| 436 | } |
---|
[5089] | 437 | update_metadata(array($image_id=>$file_path)); |
---|
[9191] | 438 | |
---|
[5089] | 439 | invalidate_user_cache(); |
---|
| 440 | |
---|
| 441 | return $image_id; |
---|
| 442 | } |
---|
| 443 | |
---|
| 444 | function prepare_directory($directory) |
---|
| 445 | { |
---|
| 446 | if (!is_dir($directory)) { |
---|
[6385] | 447 | if (substr(PHP_OS, 0, 3) == 'WIN') |
---|
| 448 | { |
---|
| 449 | $directory = str_replace('/', DIRECTORY_SEPARATOR, $directory); |
---|
| 450 | } |
---|
[5089] | 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 | { |
---|
[7868] | 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. |
---|
[5089] | 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 | |
---|
[7868] | 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; |
---|
[9952] | 503 | $destination_width = $width; |
---|
| 504 | $destination_height = $height; |
---|
[7868] | 505 | |
---|
| 506 | // maximal size exceeded ? |
---|
| 507 | if ($ratio_width > 1 or $ratio_height > 1) |
---|
| 508 | { |
---|
| 509 | if ($ratio_width < $ratio_height) |
---|
| 510 | { |
---|
[10383] | 511 | $destination_width = round($width / $ratio_height); |
---|
[7868] | 512 | $destination_height = $max_height; |
---|
| 513 | } |
---|
| 514 | else |
---|
| 515 | { |
---|
| 516 | $destination_width = $max_width; |
---|
[10383] | 517 | $destination_height = round($height / $ratio_width); |
---|
[7868] | 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 | |
---|
[10552] | 532 | function pwg_image_resize($result, $source_filepath, $destination_filepath, $max_width, $max_height, $quality, $strip_metadata=false, $crop=false, $follow_orientation=true) |
---|
[5089] | 533 | { |
---|
[5957] | 534 | if ($result !== false) |
---|
| 535 | { |
---|
| 536 | //someone hooked us - so we skip |
---|
| 537 | return $result; |
---|
| 538 | } |
---|
| 539 | |
---|
[9952] | 540 | $extension = strtolower(get_extension($source_filepath)); |
---|
| 541 | |
---|
| 542 | if (is_imagick() and $extension != 'gif') |
---|
[8219] | 543 | { |
---|
[10552] | 544 | return pwg_image_resize_im($source_filepath, $destination_filepath, $max_width, $max_height, $quality, $strip_metadata, $crop, $follow_orientation); |
---|
[8219] | 545 | } |
---|
| 546 | else |
---|
| 547 | { |
---|
[10552] | 548 | return pwg_image_resize_gd($source_filepath, $destination_filepath, $max_width, $max_height, $quality, $crop, $follow_orientation); |
---|
[8219] | 549 | } |
---|
| 550 | } |
---|
| 551 | |
---|
[10552] | 552 | function pwg_image_resize_gd($source_filepath, $destination_filepath, $max_width, $max_height, $quality, $crop=false, $follow_orientation=true) |
---|
[8219] | 553 | { |
---|
[5089] | 554 | if (!function_exists('gd_info')) |
---|
| 555 | { |
---|
| 556 | return false; |
---|
| 557 | } |
---|
| 558 | |
---|
[9952] | 559 | $gd_info = gd_info(); |
---|
| 560 | |
---|
[5089] | 561 | // extension of the picture filename |
---|
| 562 | $extension = strtolower(get_extension($source_filepath)); |
---|
| 563 | |
---|
| 564 | $source_image = null; |
---|
| 565 | if (in_array($extension, array('jpg', 'jpeg'))) |
---|
| 566 | { |
---|
[6617] | 567 | $source_image = imagecreatefromjpeg($source_filepath); |
---|
[5089] | 568 | } |
---|
| 569 | else if ($extension == 'png') |
---|
| 570 | { |
---|
[6617] | 571 | $source_image = imagecreatefrompng($source_filepath); |
---|
[5089] | 572 | } |
---|
[9952] | 573 | elseif ($extension == 'gif' and $gd_info['GIF Read Support'] and $gd_info['GIF Create Support']) |
---|
| 574 | { |
---|
| 575 | $source_image = imagecreatefromgif($source_filepath); |
---|
| 576 | } |
---|
[5089] | 577 | else |
---|
| 578 | { |
---|
[9952] | 579 | die('[GD] unsupported file extension'); |
---|
[5089] | 580 | } |
---|
[7868] | 581 | |
---|
| 582 | $rotation = null; |
---|
| 583 | if (function_exists('imagerotate')) |
---|
| 584 | { |
---|
[8219] | 585 | $rotation = get_rotation_angle($source_filepath); |
---|
[7868] | 586 | } |
---|
[5089] | 587 | |
---|
| 588 | // width/height |
---|
| 589 | $source_width = imagesx($source_image); |
---|
| 590 | $source_height = imagesy($source_image); |
---|
[10552] | 591 | |
---|
| 592 | // Crop |
---|
| 593 | if ($crop) |
---|
| 594 | { |
---|
| 595 | $coord = get_crop_coord($source_width, $source_height, $max_width, $max_height, $follow_orientation); |
---|
| 596 | } |
---|
| 597 | |
---|
[7868] | 598 | $resize_dimensions = get_resize_dimensions($source_width, $source_height, $max_width, $max_height, $rotation); |
---|
| 599 | |
---|
| 600 | // testing on height is useless in theory: if width is unchanged, there |
---|
| 601 | // should be no resize, because width/height ratio is not modified. |
---|
| 602 | if ($resize_dimensions['width'] == $source_width and $resize_dimensions['height'] == $source_height) |
---|
[5089] | 603 | { |
---|
| 604 | // the image doesn't need any resize! We just copy it to the destination |
---|
| 605 | copy($source_filepath, $destination_filepath); |
---|
| 606 | return true; |
---|
| 607 | } |
---|
| 608 | |
---|
[7868] | 609 | $destination_image = imagecreatetruecolor($resize_dimensions['width'], $resize_dimensions['height']); |
---|
[5089] | 610 | |
---|
| 611 | imagecopyresampled( |
---|
| 612 | $destination_image, |
---|
| 613 | $source_image, |
---|
| 614 | 0, |
---|
| 615 | 0, |
---|
[10552] | 616 | $crop ? $coord['x'] : 0, |
---|
| 617 | $crop ? $coord['y'] : 0, |
---|
[7868] | 618 | $resize_dimensions['width'], |
---|
| 619 | $resize_dimensions['height'], |
---|
[5089] | 620 | $source_width, |
---|
| 621 | $source_height |
---|
| 622 | ); |
---|
[7868] | 623 | |
---|
| 624 | // rotation occurs only on resized photo to avoid useless memory use |
---|
| 625 | if (isset($rotation)) |
---|
| 626 | { |
---|
| 627 | $destination_image = imagerotate($destination_image, $rotation, 0); |
---|
| 628 | } |
---|
[5089] | 629 | |
---|
[6617] | 630 | $extension = strtolower(get_extension($destination_filepath)); |
---|
| 631 | if ($extension == 'png') |
---|
| 632 | { |
---|
| 633 | imagepng($destination_image, $destination_filepath); |
---|
| 634 | } |
---|
[9952] | 635 | elseif ($extension == 'gif') |
---|
| 636 | { |
---|
| 637 | imagegif($destination_image, $destination_filepath); |
---|
| 638 | } |
---|
[6617] | 639 | else |
---|
| 640 | { |
---|
| 641 | imagejpeg($destination_image, $destination_filepath, $quality); |
---|
| 642 | } |
---|
[5089] | 643 | // freeing memory ressources |
---|
| 644 | imagedestroy($source_image); |
---|
| 645 | imagedestroy($destination_image); |
---|
| 646 | |
---|
| 647 | // everything should be OK if we are here! |
---|
| 648 | return true; |
---|
| 649 | } |
---|
| 650 | |
---|
[10552] | 651 | function pwg_image_resize_im($source_filepath, $destination_filepath, $max_width, $max_height, $quality, $strip_metadata=false, $crop=false, $follow_orientation=true) |
---|
[8219] | 652 | { |
---|
| 653 | // extension of the picture filename |
---|
| 654 | $extension = strtolower(get_extension($source_filepath)); |
---|
| 655 | if (!in_array($extension, array('jpg', 'jpeg', 'png'))) |
---|
| 656 | { |
---|
| 657 | die('[Imagick] unsupported file extension'); |
---|
| 658 | } |
---|
| 659 | |
---|
| 660 | $image = new Imagick($source_filepath); |
---|
| 661 | |
---|
[9022] | 662 | $rotation = get_rotation_angle($source_filepath); |
---|
[8219] | 663 | |
---|
| 664 | // width/height |
---|
| 665 | $source_width = $image->getImageWidth(); |
---|
| 666 | $source_height = $image->getImageHeight(); |
---|
[10552] | 667 | |
---|
| 668 | // Crop |
---|
| 669 | if ($crop) |
---|
| 670 | { |
---|
| 671 | $coord = get_crop_coord($source_width, $source_height, $max_width, $max_height, $follow_orientation); |
---|
| 672 | $image->cropImage($source_width, $source_height, $coord['x'], $coord['y']); |
---|
| 673 | } |
---|
[8219] | 674 | |
---|
| 675 | $resize_dimensions = get_resize_dimensions($source_width, $source_height, $max_width, $max_height, $rotation); |
---|
| 676 | |
---|
| 677 | // testing on height is useless in theory: if width is unchanged, there |
---|
| 678 | // should be no resize, because width/height ratio is not modified. |
---|
| 679 | if ($resize_dimensions['width'] == $source_width and $resize_dimensions['height'] == $source_height) |
---|
| 680 | { |
---|
| 681 | // the image doesn't need any resize! We just copy it to the destination |
---|
| 682 | copy($source_filepath, $destination_filepath); |
---|
| 683 | return true; |
---|
| 684 | } |
---|
| 685 | |
---|
| 686 | $image->setImageCompressionQuality($quality); |
---|
| 687 | $image->setInterlaceScheme(Imagick::INTERLACE_LINE); |
---|
| 688 | |
---|
| 689 | if ($strip_metadata) |
---|
| 690 | { |
---|
| 691 | // we save a few kilobytes. For example a thumbnail with metadata |
---|
| 692 | // weights 25KB, without metadata 7KB. |
---|
| 693 | $image->stripImage(); |
---|
| 694 | } |
---|
| 695 | |
---|
| 696 | $image->resizeImage($resize_dimensions['width'], $resize_dimensions['height'], Imagick::FILTER_LANCZOS, 0.9); |
---|
| 697 | |
---|
| 698 | if (isset($rotation)) |
---|
| 699 | { |
---|
| 700 | $image->rotateImage(new ImagickPixel(), -$rotation); |
---|
| 701 | $image->setImageOrientation(Imagick::ORIENTATION_TOPLEFT); |
---|
| 702 | } |
---|
| 703 | |
---|
| 704 | $image->writeImage($destination_filepath); |
---|
| 705 | $image->destroy(); |
---|
| 706 | |
---|
| 707 | // everything should be OK if we are here! |
---|
| 708 | return true; |
---|
| 709 | } |
---|
| 710 | |
---|
| 711 | function get_rotation_angle($source_filepath) |
---|
| 712 | { |
---|
[8763] | 713 | global $conf; |
---|
| 714 | |
---|
| 715 | if (!$conf['upload_form_automatic_rotation']) |
---|
| 716 | { |
---|
| 717 | return null; |
---|
| 718 | } |
---|
[9952] | 719 | |
---|
| 720 | list($width, $height, $type) = getimagesize($source_filepath); |
---|
| 721 | if (IMAGETYPE_JPEG != $type) |
---|
| 722 | { |
---|
| 723 | return null; |
---|
| 724 | } |
---|
[8763] | 725 | |
---|
[9021] | 726 | if (!function_exists('exif_read_data')) |
---|
| 727 | { |
---|
| 728 | return null; |
---|
| 729 | } |
---|
| 730 | |
---|
[8219] | 731 | $rotation = null; |
---|
| 732 | |
---|
| 733 | $exif = exif_read_data($source_filepath); |
---|
| 734 | |
---|
| 735 | if (isset($exif['Orientation']) and preg_match('/^\s*(\d)/', $exif['Orientation'], $matches)) |
---|
| 736 | { |
---|
| 737 | $orientation = $matches[1]; |
---|
| 738 | if (in_array($orientation, array(3, 4))) |
---|
| 739 | { |
---|
| 740 | $rotation = 180; |
---|
| 741 | } |
---|
| 742 | elseif (in_array($orientation, array(5, 6))) |
---|
| 743 | { |
---|
| 744 | $rotation = 270; |
---|
| 745 | } |
---|
| 746 | elseif (in_array($orientation, array(7, 8))) |
---|
| 747 | { |
---|
| 748 | $rotation = 90; |
---|
| 749 | } |
---|
| 750 | } |
---|
| 751 | |
---|
| 752 | return $rotation; |
---|
| 753 | } |
---|
| 754 | |
---|
[10552] | 755 | function get_crop_coord(&$source_width, &$source_height, &$max_width, &$max_height, $follow_orientation) |
---|
| 756 | { |
---|
| 757 | $x = 0; |
---|
| 758 | $y = 0; |
---|
| 759 | |
---|
| 760 | if ($source_width < $source_height and $follow_orientation) |
---|
| 761 | { |
---|
| 762 | list($width, $height) = array($max_height, $max_width); |
---|
| 763 | $max_width = $width; |
---|
| 764 | $max_height = $height; |
---|
| 765 | } |
---|
| 766 | |
---|
| 767 | $img_ratio = $source_width / $source_height; |
---|
| 768 | $dest_ratio = $max_width / $max_height; |
---|
| 769 | |
---|
| 770 | if($dest_ratio > $img_ratio) |
---|
| 771 | { |
---|
| 772 | $destHeight = round($source_width * $max_height / $max_width); |
---|
| 773 | $y = round(($source_height - $destHeight) / 2 ); |
---|
| 774 | $source_height = $destHeight; |
---|
| 775 | } |
---|
| 776 | elseif ($dest_ratio < $img_ratio) |
---|
| 777 | { |
---|
| 778 | $destWidth = round($source_height * $max_width / $max_height); |
---|
| 779 | $x = round(($source_width - $destWidth) / 2 ); |
---|
| 780 | $source_width = $destWidth; |
---|
| 781 | } |
---|
| 782 | |
---|
| 783 | return array('x' => $x, 'y' => $y); |
---|
| 784 | } |
---|
| 785 | |
---|
[5089] | 786 | function pwg_image_infos($path) |
---|
| 787 | { |
---|
| 788 | list($width, $height) = getimagesize($path); |
---|
| 789 | $filesize = floor(filesize($path)/1024); |
---|
| 790 | |
---|
| 791 | return array( |
---|
| 792 | 'width' => $width, |
---|
| 793 | 'height' => $height, |
---|
| 794 | 'filesize' => $filesize, |
---|
| 795 | ); |
---|
| 796 | } |
---|
| 797 | |
---|
| 798 | function is_valid_image_extension($extension) |
---|
| 799 | { |
---|
[9952] | 800 | return in_array(strtolower($extension), array('jpg', 'jpeg', 'png', 'gif')); |
---|
[5089] | 801 | } |
---|
[6625] | 802 | |
---|
| 803 | function file_upload_error_message($error_code) |
---|
| 804 | { |
---|
| 805 | switch ($error_code) { |
---|
| 806 | case UPLOAD_ERR_INI_SIZE: |
---|
| 807 | return sprintf( |
---|
| 808 | l10n('The uploaded file exceeds the upload_max_filesize directive in php.ini: %sB'), |
---|
| 809 | get_ini_size('upload_max_filesize', false) |
---|
| 810 | ); |
---|
| 811 | case UPLOAD_ERR_FORM_SIZE: |
---|
| 812 | return l10n('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'); |
---|
| 813 | case UPLOAD_ERR_PARTIAL: |
---|
| 814 | return l10n('The uploaded file was only partially uploaded'); |
---|
| 815 | case UPLOAD_ERR_NO_FILE: |
---|
| 816 | return l10n('No file was uploaded'); |
---|
| 817 | case UPLOAD_ERR_NO_TMP_DIR: |
---|
| 818 | return l10n('Missing a temporary folder'); |
---|
| 819 | case UPLOAD_ERR_CANT_WRITE: |
---|
| 820 | return l10n('Failed to write file to disk'); |
---|
| 821 | case UPLOAD_ERR_EXTENSION: |
---|
| 822 | return l10n('File upload stopped by extension'); |
---|
| 823 | default: |
---|
| 824 | return l10n('Unknown upload error'); |
---|
| 825 | } |
---|
| 826 | } |
---|
| 827 | |
---|
| 828 | function get_ini_size($ini_key, $in_bytes=true) |
---|
| 829 | { |
---|
| 830 | $size = ini_get($ini_key); |
---|
| 831 | |
---|
| 832 | if ($in_bytes) |
---|
| 833 | { |
---|
| 834 | $size = convert_shortand_notation_to_bytes($size); |
---|
| 835 | } |
---|
| 836 | |
---|
| 837 | return $size; |
---|
| 838 | } |
---|
| 839 | |
---|
| 840 | function convert_shortand_notation_to_bytes($value) |
---|
| 841 | { |
---|
| 842 | $suffix = substr($value, -1); |
---|
| 843 | $multiply_by = null; |
---|
| 844 | |
---|
| 845 | if ('K' == $suffix) |
---|
| 846 | { |
---|
| 847 | $multiply_by = 1024; |
---|
| 848 | } |
---|
| 849 | else if ('M' == $suffix) |
---|
| 850 | { |
---|
| 851 | $multiply_by = 1024*1024; |
---|
| 852 | } |
---|
| 853 | else if ('G' == $suffix) |
---|
| 854 | { |
---|
| 855 | $multiply_by = 1024*1024*1024; |
---|
| 856 | } |
---|
| 857 | |
---|
| 858 | if (isset($multiply_by)) |
---|
| 859 | { |
---|
| 860 | $value = substr($value, 0, -1); |
---|
| 861 | $value*= $multiply_by; |
---|
| 862 | } |
---|
| 863 | |
---|
| 864 | return $value; |
---|
| 865 | } |
---|
| 866 | |
---|
| 867 | function add_upload_error($upload_id, $error_message) |
---|
| 868 | { |
---|
| 869 | if (!isset($_SESSION['uploads_error'])) |
---|
| 870 | { |
---|
| 871 | $_SESSION['uploads_error'] = array(); |
---|
| 872 | } |
---|
| 873 | if (!isset($_SESSION['uploads_error'][$upload_id])) |
---|
| 874 | { |
---|
| 875 | $_SESSION['uploads_error'][$upload_id] = array(); |
---|
| 876 | } |
---|
| 877 | |
---|
| 878 | array_push($_SESSION['uploads_error'][$upload_id], $error_message); |
---|
| 879 | } |
---|
[8227] | 880 | |
---|
| 881 | function is_imagick() |
---|
| 882 | { |
---|
[9844] | 883 | if (class_exists('Imagick')) |
---|
[8227] | 884 | { |
---|
| 885 | return true; |
---|
| 886 | } |
---|
| 887 | |
---|
| 888 | return false; |
---|
| 889 | } |
---|
[8249] | 890 | |
---|
| 891 | function ready_for_upload_message() |
---|
| 892 | { |
---|
| 893 | global $conf; |
---|
| 894 | |
---|
| 895 | $relative_dir = preg_replace('#^'.PHPWG_ROOT_PATH.'#', '', $conf['upload_dir']); |
---|
| 896 | |
---|
| 897 | if (!is_dir($conf['upload_dir'])) |
---|
| 898 | { |
---|
| 899 | if (!is_writable(dirname($conf['upload_dir']))) |
---|
| 900 | { |
---|
| 901 | return sprintf( |
---|
| 902 | l10n('Create the "%s" directory at the root of your Piwigo installation'), |
---|
| 903 | $relative_dir |
---|
| 904 | ); |
---|
| 905 | } |
---|
| 906 | } |
---|
| 907 | else |
---|
| 908 | { |
---|
| 909 | if (!is_writable($conf['upload_dir'])) |
---|
| 910 | { |
---|
| 911 | @chmod($conf['upload_dir'], 0777); |
---|
| 912 | |
---|
| 913 | if (!is_writable($conf['upload_dir'])) |
---|
| 914 | { |
---|
| 915 | return sprintf( |
---|
| 916 | l10n('Give write access (chmod 777) to "%s" directory at the root of your Piwigo installation'), |
---|
| 917 | $relative_dir |
---|
| 918 | ); |
---|
| 919 | } |
---|
| 920 | } |
---|
| 921 | } |
---|
| 922 | |
---|
| 923 | return null; |
---|
| 924 | } |
---|
| 925 | |
---|
| 926 | function file_path_for_type($file_path, $type='thumb') |
---|
| 927 | { |
---|
| 928 | // resolve the $file_path depending on the $type |
---|
| 929 | if ('thumb' == $type) { |
---|
| 930 | $file_path = get_thumbnail_location( |
---|
| 931 | array( |
---|
| 932 | 'path' => $file_path, |
---|
| 933 | 'tn_ext' => 'jpg', |
---|
| 934 | ) |
---|
| 935 | ); |
---|
| 936 | } |
---|
| 937 | |
---|
| 938 | if ('high' == $type) { |
---|
| 939 | @include_once(PHPWG_ROOT_PATH.'include/functions_picture.inc.php'); |
---|
| 940 | $file_path = get_high_location( |
---|
| 941 | array( |
---|
| 942 | 'path' => $file_path, |
---|
| 943 | 'has_high' => 'true' |
---|
| 944 | ) |
---|
| 945 | ); |
---|
| 946 | } |
---|
| 947 | |
---|
| 948 | return $file_path; |
---|
| 949 | } |
---|
[5089] | 950 | ?> |
---|