| 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 | else |
|---|
| 243 | { |
|---|
| 244 | $file_path.= 'jpg'; |
|---|
| 245 | } |
|---|
| 246 | |
|---|
| 247 | prepare_directory($upload_dir); |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | if (is_uploaded_file($source_filepath)) |
|---|
| 251 | { |
|---|
| 252 | move_uploaded_file($source_filepath, $file_path); |
|---|
| 253 | } |
|---|
| 254 | else |
|---|
| 255 | { |
|---|
| 256 | copy($source_filepath, $file_path); |
|---|
| 257 | } |
|---|
| 258 | |
|---|
| 259 | if ($conf['upload_form_websize_resize'] |
|---|
| 260 | and need_resize($file_path, $conf['upload_form_websize_maxwidth'], $conf['upload_form_websize_maxheight'])) |
|---|
| 261 | { |
|---|
| 262 | $high_path = file_path_for_type($file_path, 'high'); |
|---|
| 263 | $high_dir = dirname($high_path); |
|---|
| 264 | prepare_directory($high_dir); |
|---|
| 265 | |
|---|
| 266 | rename($file_path, $high_path); |
|---|
| 267 | $high_infos = pwg_image_infos($high_path); |
|---|
| 268 | |
|---|
| 269 | trigger_event( |
|---|
| 270 | 'upload_image_resize', |
|---|
| 271 | false, |
|---|
| 272 | $high_path, |
|---|
| 273 | $file_path, |
|---|
| 274 | $conf['upload_form_websize_maxwidth'], |
|---|
| 275 | $conf['upload_form_websize_maxheight'], |
|---|
| 276 | $conf['upload_form_websize_quality'], |
|---|
| 277 | false |
|---|
| 278 | ); |
|---|
| 279 | |
|---|
| 280 | if (is_imagick()) |
|---|
| 281 | { |
|---|
| 282 | if ($conf['upload_form_hd_keep']) |
|---|
| 283 | { |
|---|
| 284 | if ($conf['upload_form_hd_resize']) |
|---|
| 285 | { |
|---|
| 286 | $need_resize = need_resize($high_path, $conf['upload_form_hd_maxwidth'], $conf['upload_form_hd_maxheight']); |
|---|
| 287 | |
|---|
| 288 | if ($need_resize) |
|---|
| 289 | { |
|---|
| 290 | pwg_image_resize( |
|---|
| 291 | false, |
|---|
| 292 | $high_path, |
|---|
| 293 | $high_path, |
|---|
| 294 | $conf['upload_form_hd_maxwidth'], |
|---|
| 295 | $conf['upload_form_hd_maxheight'], |
|---|
| 296 | $conf['upload_form_hd_quality'], |
|---|
| 297 | false |
|---|
| 298 | ); |
|---|
| 299 | $high_infos = pwg_image_infos($high_path); |
|---|
| 300 | } |
|---|
| 301 | } |
|---|
| 302 | } |
|---|
| 303 | else |
|---|
| 304 | { |
|---|
| 305 | unlink($high_path); |
|---|
| 306 | $high_infos = null; |
|---|
| 307 | } |
|---|
| 308 | } |
|---|
| 309 | } |
|---|
| 310 | |
|---|
| 311 | $file_infos = pwg_image_infos($file_path); |
|---|
| 312 | |
|---|
| 313 | $thumb_path = file_path_for_type($file_path, 'thumb'); |
|---|
| 314 | $thumb_dir = dirname($thumb_path); |
|---|
| 315 | prepare_directory($thumb_dir); |
|---|
| 316 | |
|---|
| 317 | trigger_event( |
|---|
| 318 | 'upload_thumbnail_resize', |
|---|
| 319 | false, |
|---|
| 320 | $file_path, |
|---|
| 321 | $thumb_path, |
|---|
| 322 | $conf['upload_form_thumb_maxwidth'], |
|---|
| 323 | $conf['upload_form_thumb_maxheight'], |
|---|
| 324 | $conf['upload_form_thumb_quality'], |
|---|
| 325 | true |
|---|
| 326 | ); |
|---|
| 327 | |
|---|
| 328 | $thumb_infos = pwg_image_infos($thumb_path); |
|---|
| 329 | |
|---|
| 330 | if (isset($image_id)) |
|---|
| 331 | { |
|---|
| 332 | $update = array( |
|---|
| 333 | 'id' => $image_id, |
|---|
| 334 | 'file' => pwg_db_real_escape_string(isset($original_filename) ? $original_filename : basename($file_path)), |
|---|
| 335 | 'filesize' => $file_infos['filesize'], |
|---|
| 336 | 'width' => $file_infos['width'], |
|---|
| 337 | 'height' => $file_infos['height'], |
|---|
| 338 | 'md5sum' => $md5sum, |
|---|
| 339 | 'added_by' => $user['id'], |
|---|
| 340 | ); |
|---|
| 341 | |
|---|
| 342 | if (isset($high_infos)) |
|---|
| 343 | { |
|---|
| 344 | $update['has_high'] = 'true'; |
|---|
| 345 | $update['high_filesize'] = $high_infos['filesize']; |
|---|
| 346 | } |
|---|
| 347 | else |
|---|
| 348 | { |
|---|
| 349 | $update['has_high'] = 'false'; |
|---|
| 350 | $update['high_filesize'] = null; |
|---|
| 351 | } |
|---|
| 352 | |
|---|
| 353 | if (isset($level)) |
|---|
| 354 | { |
|---|
| 355 | $update['level'] = $level; |
|---|
| 356 | } |
|---|
| 357 | |
|---|
| 358 | mass_updates( |
|---|
| 359 | IMAGES_TABLE, |
|---|
| 360 | array( |
|---|
| 361 | 'primary' => array('id'), |
|---|
| 362 | 'update' => array_keys($update) |
|---|
| 363 | ), |
|---|
| 364 | array($update) |
|---|
| 365 | ); |
|---|
| 366 | } |
|---|
| 367 | else |
|---|
| 368 | { |
|---|
| 369 | // database registration |
|---|
| 370 | $insert = array( |
|---|
| 371 | 'file' => pwg_db_real_escape_string(isset($original_filename) ? $original_filename : basename($file_path)), |
|---|
| 372 | 'date_available' => $dbnow, |
|---|
| 373 | 'tn_ext' => 'jpg', |
|---|
| 374 | 'path' => preg_replace('#^'.preg_quote(PHPWG_ROOT_PATH).'#', '', $file_path), |
|---|
| 375 | 'filesize' => $file_infos['filesize'], |
|---|
| 376 | 'width' => $file_infos['width'], |
|---|
| 377 | 'height' => $file_infos['height'], |
|---|
| 378 | 'md5sum' => $md5sum, |
|---|
| 379 | 'added_by' => $user['id'], |
|---|
| 380 | ); |
|---|
| 381 | |
|---|
| 382 | if (isset($high_infos)) |
|---|
| 383 | { |
|---|
| 384 | $insert['has_high'] = 'true'; |
|---|
| 385 | $insert['high_filesize'] = $high_infos['filesize']; |
|---|
| 386 | } |
|---|
| 387 | |
|---|
| 388 | if (isset($level)) |
|---|
| 389 | { |
|---|
| 390 | $insert['level'] = $level; |
|---|
| 391 | } |
|---|
| 392 | |
|---|
| 393 | mass_inserts( |
|---|
| 394 | IMAGES_TABLE, |
|---|
| 395 | array_keys($insert), |
|---|
| 396 | array($insert) |
|---|
| 397 | ); |
|---|
| 398 | |
|---|
| 399 | $image_id = pwg_db_insert_id(IMAGES_TABLE); |
|---|
| 400 | } |
|---|
| 401 | |
|---|
| 402 | if (isset($categories) and count($categories) > 0) |
|---|
| 403 | { |
|---|
| 404 | associate_images_to_categories( |
|---|
| 405 | array($image_id), |
|---|
| 406 | $categories |
|---|
| 407 | ); |
|---|
| 408 | } |
|---|
| 409 | |
|---|
| 410 | // update metadata from the uploaded file (exif/iptc) |
|---|
| 411 | if ($conf['use_exif'] and !function_exists('read_exif_data')) |
|---|
| 412 | { |
|---|
| 413 | $conf['use_exif'] = false; |
|---|
| 414 | } |
|---|
| 415 | update_metadata(array($image_id=>$file_path)); |
|---|
| 416 | |
|---|
| 417 | invalidate_user_cache(); |
|---|
| 418 | |
|---|
| 419 | return $image_id; |
|---|
| 420 | } |
|---|
| 421 | |
|---|
| 422 | function prepare_directory($directory) |
|---|
| 423 | { |
|---|
| 424 | if (!is_dir($directory)) { |
|---|
| 425 | if (substr(PHP_OS, 0, 3) == 'WIN') |
|---|
| 426 | { |
|---|
| 427 | $directory = str_replace('/', DIRECTORY_SEPARATOR, $directory); |
|---|
| 428 | } |
|---|
| 429 | umask(0000); |
|---|
| 430 | $recursive = true; |
|---|
| 431 | if (!@mkdir($directory, 0777, $recursive)) |
|---|
| 432 | { |
|---|
| 433 | die('[prepare_directory] cannot create directory "'.$directory.'"'); |
|---|
| 434 | } |
|---|
| 435 | } |
|---|
| 436 | |
|---|
| 437 | if (!is_writable($directory)) |
|---|
| 438 | { |
|---|
| 439 | // last chance to make the directory writable |
|---|
| 440 | @chmod($directory, 0777); |
|---|
| 441 | |
|---|
| 442 | if (!is_writable($directory)) |
|---|
| 443 | { |
|---|
| 444 | die('[prepare_directory] directory "'.$directory.'" has no write access'); |
|---|
| 445 | } |
|---|
| 446 | } |
|---|
| 447 | |
|---|
| 448 | secure_directory($directory); |
|---|
| 449 | } |
|---|
| 450 | |
|---|
| 451 | function need_resize($image_filepath, $max_width, $max_height) |
|---|
| 452 | { |
|---|
| 453 | // TODO : the resize check should take the orientation into account. If a |
|---|
| 454 | // rotation must be applied to the resized photo, then we should test |
|---|
| 455 | // invert width and height. |
|---|
| 456 | list($width, $height) = getimagesize($image_filepath); |
|---|
| 457 | |
|---|
| 458 | if ($width > $max_width or $height > $max_height) |
|---|
| 459 | { |
|---|
| 460 | return true; |
|---|
| 461 | } |
|---|
| 462 | |
|---|
| 463 | return false; |
|---|
| 464 | } |
|---|
| 465 | |
|---|
| 466 | function get_resize_dimensions($width, $height, $max_width, $max_height, $rotation=null) |
|---|
| 467 | { |
|---|
| 468 | $rotate_for_dimensions = false; |
|---|
| 469 | if (isset($rotation) and in_array(abs($rotation), array(90, 270))) |
|---|
| 470 | { |
|---|
| 471 | $rotate_for_dimensions = true; |
|---|
| 472 | } |
|---|
| 473 | |
|---|
| 474 | if ($rotate_for_dimensions) |
|---|
| 475 | { |
|---|
| 476 | list($width, $height) = array($height, $width); |
|---|
| 477 | } |
|---|
| 478 | |
|---|
| 479 | $ratio_width = $width / $max_width; |
|---|
| 480 | $ratio_height = $height / $max_height; |
|---|
| 481 | |
|---|
| 482 | // maximal size exceeded ? |
|---|
| 483 | if ($ratio_width > 1 or $ratio_height > 1) |
|---|
| 484 | { |
|---|
| 485 | if ($ratio_width < $ratio_height) |
|---|
| 486 | { |
|---|
| 487 | $destination_width = ceil($width / $ratio_height); |
|---|
| 488 | $destination_height = $max_height; |
|---|
| 489 | } |
|---|
| 490 | else |
|---|
| 491 | { |
|---|
| 492 | $destination_width = $max_width; |
|---|
| 493 | $destination_height = ceil($height / $ratio_width); |
|---|
| 494 | } |
|---|
| 495 | } |
|---|
| 496 | |
|---|
| 497 | if ($rotate_for_dimensions) |
|---|
| 498 | { |
|---|
| 499 | list($destination_width, $destination_height) = array($destination_height, $destination_width); |
|---|
| 500 | } |
|---|
| 501 | |
|---|
| 502 | return array( |
|---|
| 503 | 'width' => $destination_width, |
|---|
| 504 | 'height'=> $destination_height, |
|---|
| 505 | ); |
|---|
| 506 | } |
|---|
| 507 | |
|---|
| 508 | function pwg_image_resize($result, $source_filepath, $destination_filepath, $max_width, $max_height, $quality, $strip_metadata=false) |
|---|
| 509 | { |
|---|
| 510 | if ($result !== false) |
|---|
| 511 | { |
|---|
| 512 | //someone hooked us - so we skip |
|---|
| 513 | return $result; |
|---|
| 514 | } |
|---|
| 515 | |
|---|
| 516 | if (is_imagick()) |
|---|
| 517 | { |
|---|
| 518 | return pwg_image_resize_im($source_filepath, $destination_filepath, $max_width, $max_height, $quality, $strip_metadata); |
|---|
| 519 | } |
|---|
| 520 | else |
|---|
| 521 | { |
|---|
| 522 | return pwg_image_resize_gd($source_filepath, $destination_filepath, $max_width, $max_height, $quality); |
|---|
| 523 | } |
|---|
| 524 | } |
|---|
| 525 | |
|---|
| 526 | function pwg_image_resize_gd($source_filepath, $destination_filepath, $max_width, $max_height, $quality) |
|---|
| 527 | { |
|---|
| 528 | if (!function_exists('gd_info')) |
|---|
| 529 | { |
|---|
| 530 | return false; |
|---|
| 531 | } |
|---|
| 532 | |
|---|
| 533 | // extension of the picture filename |
|---|
| 534 | $extension = strtolower(get_extension($source_filepath)); |
|---|
| 535 | |
|---|
| 536 | $source_image = null; |
|---|
| 537 | if (in_array($extension, array('jpg', 'jpeg'))) |
|---|
| 538 | { |
|---|
| 539 | $source_image = imagecreatefromjpeg($source_filepath); |
|---|
| 540 | } |
|---|
| 541 | else if ($extension == 'png') |
|---|
| 542 | { |
|---|
| 543 | $source_image = imagecreatefrompng($source_filepath); |
|---|
| 544 | } |
|---|
| 545 | else |
|---|
| 546 | { |
|---|
| 547 | die('unsupported file extension'); |
|---|
| 548 | } |
|---|
| 549 | |
|---|
| 550 | $rotation = null; |
|---|
| 551 | if (function_exists('imagerotate')) |
|---|
| 552 | { |
|---|
| 553 | $rotation = get_rotation_angle($source_filepath); |
|---|
| 554 | } |
|---|
| 555 | |
|---|
| 556 | // width/height |
|---|
| 557 | $source_width = imagesx($source_image); |
|---|
| 558 | $source_height = imagesy($source_image); |
|---|
| 559 | |
|---|
| 560 | $resize_dimensions = get_resize_dimensions($source_width, $source_height, $max_width, $max_height, $rotation); |
|---|
| 561 | |
|---|
| 562 | // testing on height is useless in theory: if width is unchanged, there |
|---|
| 563 | // should be no resize, because width/height ratio is not modified. |
|---|
| 564 | if ($resize_dimensions['width'] == $source_width and $resize_dimensions['height'] == $source_height) |
|---|
| 565 | { |
|---|
| 566 | // the image doesn't need any resize! We just copy it to the destination |
|---|
| 567 | copy($source_filepath, $destination_filepath); |
|---|
| 568 | return true; |
|---|
| 569 | } |
|---|
| 570 | |
|---|
| 571 | $destination_image = imagecreatetruecolor($resize_dimensions['width'], $resize_dimensions['height']); |
|---|
| 572 | |
|---|
| 573 | imagecopyresampled( |
|---|
| 574 | $destination_image, |
|---|
| 575 | $source_image, |
|---|
| 576 | 0, |
|---|
| 577 | 0, |
|---|
| 578 | 0, |
|---|
| 579 | 0, |
|---|
| 580 | $resize_dimensions['width'], |
|---|
| 581 | $resize_dimensions['height'], |
|---|
| 582 | $source_width, |
|---|
| 583 | $source_height |
|---|
| 584 | ); |
|---|
| 585 | |
|---|
| 586 | // rotation occurs only on resized photo to avoid useless memory use |
|---|
| 587 | if (isset($rotation)) |
|---|
| 588 | { |
|---|
| 589 | $destination_image = imagerotate($destination_image, $rotation, 0); |
|---|
| 590 | } |
|---|
| 591 | |
|---|
| 592 | $extension = strtolower(get_extension($destination_filepath)); |
|---|
| 593 | if ($extension == 'png') |
|---|
| 594 | { |
|---|
| 595 | imagepng($destination_image, $destination_filepath); |
|---|
| 596 | } |
|---|
| 597 | else |
|---|
| 598 | { |
|---|
| 599 | imagejpeg($destination_image, $destination_filepath, $quality); |
|---|
| 600 | } |
|---|
| 601 | // freeing memory ressources |
|---|
| 602 | imagedestroy($source_image); |
|---|
| 603 | imagedestroy($destination_image); |
|---|
| 604 | |
|---|
| 605 | // everything should be OK if we are here! |
|---|
| 606 | return true; |
|---|
| 607 | } |
|---|
| 608 | |
|---|
| 609 | function pwg_image_resize_im($source_filepath, $destination_filepath, $max_width, $max_height, $quality, $strip_metadata=false) |
|---|
| 610 | { |
|---|
| 611 | // extension of the picture filename |
|---|
| 612 | $extension = strtolower(get_extension($source_filepath)); |
|---|
| 613 | if (!in_array($extension, array('jpg', 'jpeg', 'png'))) |
|---|
| 614 | { |
|---|
| 615 | die('[Imagick] unsupported file extension'); |
|---|
| 616 | } |
|---|
| 617 | |
|---|
| 618 | $image = new Imagick($source_filepath); |
|---|
| 619 | |
|---|
| 620 | $rotation = get_rotation_angle($source_filepath); |
|---|
| 621 | |
|---|
| 622 | // width/height |
|---|
| 623 | $source_width = $image->getImageWidth(); |
|---|
| 624 | $source_height = $image->getImageHeight(); |
|---|
| 625 | |
|---|
| 626 | $resize_dimensions = get_resize_dimensions($source_width, $source_height, $max_width, $max_height, $rotation); |
|---|
| 627 | |
|---|
| 628 | // testing on height is useless in theory: if width is unchanged, there |
|---|
| 629 | // should be no resize, because width/height ratio is not modified. |
|---|
| 630 | if ($resize_dimensions['width'] == $source_width and $resize_dimensions['height'] == $source_height) |
|---|
| 631 | { |
|---|
| 632 | // the image doesn't need any resize! We just copy it to the destination |
|---|
| 633 | copy($source_filepath, $destination_filepath); |
|---|
| 634 | return true; |
|---|
| 635 | } |
|---|
| 636 | |
|---|
| 637 | $image->setImageCompressionQuality($quality); |
|---|
| 638 | $image->setInterlaceScheme(Imagick::INTERLACE_LINE); |
|---|
| 639 | |
|---|
| 640 | if ($strip_metadata) |
|---|
| 641 | { |
|---|
| 642 | // we save a few kilobytes. For example a thumbnail with metadata |
|---|
| 643 | // weights 25KB, without metadata 7KB. |
|---|
| 644 | $image->stripImage(); |
|---|
| 645 | } |
|---|
| 646 | |
|---|
| 647 | $image->resizeImage($resize_dimensions['width'], $resize_dimensions['height'], Imagick::FILTER_LANCZOS, 0.9); |
|---|
| 648 | |
|---|
| 649 | if (isset($rotation)) |
|---|
| 650 | { |
|---|
| 651 | $image->rotateImage(new ImagickPixel(), -$rotation); |
|---|
| 652 | $image->setImageOrientation(Imagick::ORIENTATION_TOPLEFT); |
|---|
| 653 | } |
|---|
| 654 | |
|---|
| 655 | $image->writeImage($destination_filepath); |
|---|
| 656 | $image->destroy(); |
|---|
| 657 | |
|---|
| 658 | // everything should be OK if we are here! |
|---|
| 659 | return true; |
|---|
| 660 | } |
|---|
| 661 | |
|---|
| 662 | function get_rotation_angle($source_filepath) |
|---|
| 663 | { |
|---|
| 664 | global $conf; |
|---|
| 665 | |
|---|
| 666 | if (!$conf['upload_form_automatic_rotation']) |
|---|
| 667 | { |
|---|
| 668 | return null; |
|---|
| 669 | } |
|---|
| 670 | |
|---|
| 671 | if (!function_exists('exif_read_data')) |
|---|
| 672 | { |
|---|
| 673 | return null; |
|---|
| 674 | } |
|---|
| 675 | |
|---|
| 676 | $rotation = null; |
|---|
| 677 | |
|---|
| 678 | $exif = exif_read_data($source_filepath); |
|---|
| 679 | |
|---|
| 680 | if (isset($exif['Orientation']) and preg_match('/^\s*(\d)/', $exif['Orientation'], $matches)) |
|---|
| 681 | { |
|---|
| 682 | $orientation = $matches[1]; |
|---|
| 683 | if (in_array($orientation, array(3, 4))) |
|---|
| 684 | { |
|---|
| 685 | $rotation = 180; |
|---|
| 686 | } |
|---|
| 687 | elseif (in_array($orientation, array(5, 6))) |
|---|
| 688 | { |
|---|
| 689 | $rotation = 270; |
|---|
| 690 | } |
|---|
| 691 | elseif (in_array($orientation, array(7, 8))) |
|---|
| 692 | { |
|---|
| 693 | $rotation = 90; |
|---|
| 694 | } |
|---|
| 695 | } |
|---|
| 696 | |
|---|
| 697 | return $rotation; |
|---|
| 698 | } |
|---|
| 699 | |
|---|
| 700 | function pwg_image_infos($path) |
|---|
| 701 | { |
|---|
| 702 | list($width, $height) = getimagesize($path); |
|---|
| 703 | $filesize = floor(filesize($path)/1024); |
|---|
| 704 | |
|---|
| 705 | return array( |
|---|
| 706 | 'width' => $width, |
|---|
| 707 | 'height' => $height, |
|---|
| 708 | 'filesize' => $filesize, |
|---|
| 709 | ); |
|---|
| 710 | } |
|---|
| 711 | |
|---|
| 712 | function is_valid_image_extension($extension) |
|---|
| 713 | { |
|---|
| 714 | return in_array(strtolower($extension), array('jpg', 'jpeg', 'png')); |
|---|
| 715 | } |
|---|
| 716 | |
|---|
| 717 | function file_upload_error_message($error_code) |
|---|
| 718 | { |
|---|
| 719 | switch ($error_code) { |
|---|
| 720 | case UPLOAD_ERR_INI_SIZE: |
|---|
| 721 | return sprintf( |
|---|
| 722 | l10n('The uploaded file exceeds the upload_max_filesize directive in php.ini: %sB'), |
|---|
| 723 | get_ini_size('upload_max_filesize', false) |
|---|
| 724 | ); |
|---|
| 725 | case UPLOAD_ERR_FORM_SIZE: |
|---|
| 726 | return l10n('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'); |
|---|
| 727 | case UPLOAD_ERR_PARTIAL: |
|---|
| 728 | return l10n('The uploaded file was only partially uploaded'); |
|---|
| 729 | case UPLOAD_ERR_NO_FILE: |
|---|
| 730 | return l10n('No file was uploaded'); |
|---|
| 731 | case UPLOAD_ERR_NO_TMP_DIR: |
|---|
| 732 | return l10n('Missing a temporary folder'); |
|---|
| 733 | case UPLOAD_ERR_CANT_WRITE: |
|---|
| 734 | return l10n('Failed to write file to disk'); |
|---|
| 735 | case UPLOAD_ERR_EXTENSION: |
|---|
| 736 | return l10n('File upload stopped by extension'); |
|---|
| 737 | default: |
|---|
| 738 | return l10n('Unknown upload error'); |
|---|
| 739 | } |
|---|
| 740 | } |
|---|
| 741 | |
|---|
| 742 | function get_ini_size($ini_key, $in_bytes=true) |
|---|
| 743 | { |
|---|
| 744 | $size = ini_get($ini_key); |
|---|
| 745 | |
|---|
| 746 | if ($in_bytes) |
|---|
| 747 | { |
|---|
| 748 | $size = convert_shortand_notation_to_bytes($size); |
|---|
| 749 | } |
|---|
| 750 | |
|---|
| 751 | return $size; |
|---|
| 752 | } |
|---|
| 753 | |
|---|
| 754 | function convert_shortand_notation_to_bytes($value) |
|---|
| 755 | { |
|---|
| 756 | $suffix = substr($value, -1); |
|---|
| 757 | $multiply_by = null; |
|---|
| 758 | |
|---|
| 759 | if ('K' == $suffix) |
|---|
| 760 | { |
|---|
| 761 | $multiply_by = 1024; |
|---|
| 762 | } |
|---|
| 763 | else if ('M' == $suffix) |
|---|
| 764 | { |
|---|
| 765 | $multiply_by = 1024*1024; |
|---|
| 766 | } |
|---|
| 767 | else if ('G' == $suffix) |
|---|
| 768 | { |
|---|
| 769 | $multiply_by = 1024*1024*1024; |
|---|
| 770 | } |
|---|
| 771 | |
|---|
| 772 | if (isset($multiply_by)) |
|---|
| 773 | { |
|---|
| 774 | $value = substr($value, 0, -1); |
|---|
| 775 | $value*= $multiply_by; |
|---|
| 776 | } |
|---|
| 777 | |
|---|
| 778 | return $value; |
|---|
| 779 | } |
|---|
| 780 | |
|---|
| 781 | function add_upload_error($upload_id, $error_message) |
|---|
| 782 | { |
|---|
| 783 | if (!isset($_SESSION['uploads_error'])) |
|---|
| 784 | { |
|---|
| 785 | $_SESSION['uploads_error'] = array(); |
|---|
| 786 | } |
|---|
| 787 | if (!isset($_SESSION['uploads_error'][$upload_id])) |
|---|
| 788 | { |
|---|
| 789 | $_SESSION['uploads_error'][$upload_id] = array(); |
|---|
| 790 | } |
|---|
| 791 | |
|---|
| 792 | array_push($_SESSION['uploads_error'][$upload_id], $error_message); |
|---|
| 793 | } |
|---|
| 794 | |
|---|
| 795 | function is_imagick() |
|---|
| 796 | { |
|---|
| 797 | if (extension_loaded('imagick')) |
|---|
| 798 | { |
|---|
| 799 | return true; |
|---|
| 800 | } |
|---|
| 801 | |
|---|
| 802 | return false; |
|---|
| 803 | } |
|---|
| 804 | |
|---|
| 805 | function ready_for_upload_message() |
|---|
| 806 | { |
|---|
| 807 | global $conf; |
|---|
| 808 | |
|---|
| 809 | $relative_dir = preg_replace('#^'.PHPWG_ROOT_PATH.'#', '', $conf['upload_dir']); |
|---|
| 810 | |
|---|
| 811 | if (!is_dir($conf['upload_dir'])) |
|---|
| 812 | { |
|---|
| 813 | if (!is_writable(dirname($conf['upload_dir']))) |
|---|
| 814 | { |
|---|
| 815 | return sprintf( |
|---|
| 816 | l10n('Create the "%s" directory at the root of your Piwigo installation'), |
|---|
| 817 | $relative_dir |
|---|
| 818 | ); |
|---|
| 819 | } |
|---|
| 820 | } |
|---|
| 821 | else |
|---|
| 822 | { |
|---|
| 823 | if (!is_writable($conf['upload_dir'])) |
|---|
| 824 | { |
|---|
| 825 | @chmod($conf['upload_dir'], 0777); |
|---|
| 826 | |
|---|
| 827 | if (!is_writable($conf['upload_dir'])) |
|---|
| 828 | { |
|---|
| 829 | return sprintf( |
|---|
| 830 | l10n('Give write access (chmod 777) to "%s" directory at the root of your Piwigo installation'), |
|---|
| 831 | $relative_dir |
|---|
| 832 | ); |
|---|
| 833 | } |
|---|
| 834 | } |
|---|
| 835 | } |
|---|
| 836 | |
|---|
| 837 | return null; |
|---|
| 838 | } |
|---|
| 839 | |
|---|
| 840 | function file_path_for_type($file_path, $type='thumb') |
|---|
| 841 | { |
|---|
| 842 | // resolve the $file_path depending on the $type |
|---|
| 843 | if ('thumb' == $type) { |
|---|
| 844 | $file_path = get_thumbnail_location( |
|---|
| 845 | array( |
|---|
| 846 | 'path' => $file_path, |
|---|
| 847 | 'tn_ext' => 'jpg', |
|---|
| 848 | ) |
|---|
| 849 | ); |
|---|
| 850 | } |
|---|
| 851 | |
|---|
| 852 | if ('high' == $type) { |
|---|
| 853 | @include_once(PHPWG_ROOT_PATH.'include/functions_picture.inc.php'); |
|---|
| 854 | $file_path = get_high_location( |
|---|
| 855 | array( |
|---|
| 856 | 'path' => $file_path, |
|---|
| 857 | 'has_high' => 'true' |
|---|
| 858 | ) |
|---|
| 859 | ); |
|---|
| 860 | } |
|---|
| 861 | |
|---|
| 862 | return $file_path; |
|---|
| 863 | } |
|---|
| 864 | ?> |
|---|