Changeset 8219
- Timestamp:
- Dec 21, 2010, 10:55:04 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/admin/include/functions_upload.inc.php
r7868 r8219 19 19 20 20 // add default event handler for image and thumbnail resize 21 add_event_handler('upload_image_resize', 'pwg_image_resize', EVENT_HANDLER_PRIORITY_NEUTRAL, 6);22 add_event_handler('upload_thumbnail_resize', 'pwg_image_resize', EVENT_HANDLER_PRIORITY_NEUTRAL, 6);21 add_event_handler('upload_image_resize', 'pwg_image_resize', EVENT_HANDLER_PRIORITY_NEUTRAL, 7); 22 add_event_handler('upload_thumbnail_resize', 'pwg_image_resize', EVENT_HANDLER_PRIORITY_NEUTRAL, 7); 23 23 24 24 function add_uploaded_file($source_filepath, $original_filename=null, $categories=null, $level=null) … … 75 75 $high_infos = pwg_image_infos($high_path); 76 76 77 trigger_event('upload_image_resize', 77 trigger_event( 78 'upload_image_resize', 78 79 false, 79 80 $high_path, … … 81 82 $conf['upload_form_websize_maxwidth'], 82 83 $conf['upload_form_websize_maxheight'], 83 $conf['upload_form_websize_quality'] 84 $conf['upload_form_websize_quality'], 85 false 84 86 ); 85 87 } … … 91 93 prepare_directory($thumb_dir); 92 94 93 trigger_event('upload_thumbnail_resize', 95 trigger_event( 96 'upload_thumbnail_resize', 94 97 false, 95 98 $file_path, … … 97 100 $conf['upload_form_thumb_maxwidth'], 98 101 $conf['upload_form_thumb_maxheight'], 99 $conf['upload_form_thumb_quality'] 102 $conf['upload_form_thumb_quality'], 103 true 100 104 ); 101 105 … … 239 243 } 240 244 241 function pwg_image_resize($result, $source_filepath, $destination_filepath, $max_width, $max_height, $quality )245 function pwg_image_resize($result, $source_filepath, $destination_filepath, $max_width, $max_height, $quality, $strip_metadata=false) 242 246 { 243 247 if ($result !== false) … … 247 251 } 248 252 253 if (extension_loaded('imagick')) 254 { 255 return pwg_image_resize_im($source_filepath, $destination_filepath, $max_width, $max_height, $quality, $strip_metadata); 256 } 257 else 258 { 259 return pwg_image_resize_gd($source_filepath, $destination_filepath, $max_width, $max_height, $quality); 260 } 261 } 262 263 function pwg_image_resize_gd($source_filepath, $destination_filepath, $max_width, $max_height, $quality) 264 { 249 265 if (!function_exists('gd_info')) 250 266 { … … 272 288 if (function_exists('imagerotate')) 273 289 { 274 $exif = exif_read_data($source_filepath); 275 if (isset($exif['Orientation']) and preg_match('/^\s*(\d)/', $exif['Orientation'], $matches)) 276 { 277 $orientation = $matches[1]; 278 if (in_array($orientation, array(3, 4))) 279 { 280 $rotation = 180; 281 } 282 elseif (in_array($orientation, array(5, 6))) 283 { 284 $rotation = 270; 285 } 286 elseif (in_array($orientation, array(7, 8))) 287 { 288 $rotation = 90; 289 } 290 } 290 $rotation = get_rotation_angle($source_filepath); 291 291 } 292 292 … … 342 342 // everything should be OK if we are here! 343 343 return true; 344 } 345 346 function pwg_image_resize_im($source_filepath, $destination_filepath, $max_width, $max_height, $quality, $strip_metadata=false) 347 { 348 // extension of the picture filename 349 $extension = strtolower(get_extension($source_filepath)); 350 if (!in_array($extension, array('jpg', 'jpeg', 'png'))) 351 { 352 die('[Imagick] unsupported file extension'); 353 } 354 355 $image = new Imagick($source_filepath); 356 357 $rotation = null; 358 if (function_exists('imagerotate')) 359 { 360 $rotation = get_rotation_angle($source_filepath); 361 } 362 363 // width/height 364 $source_width = $image->getImageWidth(); 365 $source_height = $image->getImageHeight(); 366 367 $resize_dimensions = get_resize_dimensions($source_width, $source_height, $max_width, $max_height, $rotation); 368 369 // testing on height is useless in theory: if width is unchanged, there 370 // should be no resize, because width/height ratio is not modified. 371 if ($resize_dimensions['width'] == $source_width and $resize_dimensions['height'] == $source_height) 372 { 373 // the image doesn't need any resize! We just copy it to the destination 374 copy($source_filepath, $destination_filepath); 375 return true; 376 } 377 378 $image->setImageCompressionQuality($quality); 379 $image->setInterlaceScheme(Imagick::INTERLACE_LINE); 380 381 if ($strip_metadata) 382 { 383 // we save a few kilobytes. For example a thumbnail with metadata 384 // weights 25KB, without metadata 7KB. 385 $image->stripImage(); 386 } 387 388 $image->resizeImage($resize_dimensions['width'], $resize_dimensions['height'], Imagick::FILTER_LANCZOS, 0.9); 389 390 if (isset($rotation)) 391 { 392 $image->rotateImage(new ImagickPixel(), -$rotation); 393 $image->setImageOrientation(Imagick::ORIENTATION_TOPLEFT); 394 } 395 396 $image->writeImage($destination_filepath); 397 $image->destroy(); 398 399 // everything should be OK if we are here! 400 return true; 401 } 402 403 function get_rotation_angle($source_filepath) 404 { 405 $rotation = null; 406 407 $exif = exif_read_data($source_filepath); 408 409 if (isset($exif['Orientation']) and preg_match('/^\s*(\d)/', $exif['Orientation'], $matches)) 410 { 411 $orientation = $matches[1]; 412 if (in_array($orientation, array(3, 4))) 413 { 414 $rotation = 180; 415 } 416 elseif (in_array($orientation, array(5, 6))) 417 { 418 $rotation = 270; 419 } 420 elseif (in_array($orientation, array(7, 8))) 421 { 422 $rotation = 90; 423 } 424 } 425 426 return $rotation; 344 427 } 345 428
Note: See TracChangeset
for help on using the changeset viewer.