Ignore:
Timestamp:
Apr 21, 2011, 11:55:20 PM (13 years ago)
Author:
patdenice
Message:

feature:2273
Ability to crop thumbnail (fixed size)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/admin/include/functions_upload.inc.php

    r10383 r10552  
    2626// add default event handler for image and thumbnail resize
    2727add_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);
     28add_event_handler('upload_thumbnail_resize', 'pwg_image_resize', EVENT_HANDLER_PRIORITY_NEUTRAL, 9);
    2929
    3030function get_upload_form_config()
     
    8989      'can_be_null' => false,
    9090      '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,
    91101      ),
    92102 
     
    327337    $conf['upload_form_thumb_maxheight'],
    328338    $conf['upload_form_thumb_quality'],
    329     true
     339    true,
     340    $conf['upload_form_thumb_crop'],
     341    $conf['upload_form_thumb_follow_orientation']
    330342    );
    331343 
     
    518530}
    519531
    520 function pwg_image_resize($result, $source_filepath, $destination_filepath, $max_width, $max_height, $quality, $strip_metadata=false)
     532function pwg_image_resize($result, $source_filepath, $destination_filepath, $max_width, $max_height, $quality, $strip_metadata=false, $crop=false, $follow_orientation=true)
    521533{
    522534  if ($result !== false)
     
    530542  if (is_imagick() and $extension != 'gif')
    531543  {
    532     return pwg_image_resize_im($source_filepath, $destination_filepath, $max_width, $max_height, $quality, $strip_metadata);
     544    return pwg_image_resize_im($source_filepath, $destination_filepath, $max_width, $max_height, $quality, $strip_metadata, $crop, $follow_orientation);
    533545  }
    534546  else
    535547  {
    536     return pwg_image_resize_gd($source_filepath, $destination_filepath, $max_width, $max_height, $quality);
    537   }
    538 }
    539 
    540 function pwg_image_resize_gd($source_filepath, $destination_filepath, $max_width, $max_height, $quality)
     548    return pwg_image_resize_gd($source_filepath, $destination_filepath, $max_width, $max_height, $quality, $crop, $follow_orientation);
     549  }
     550}
     551
     552function pwg_image_resize_gd($source_filepath, $destination_filepath, $max_width, $max_height, $quality, $crop=false, $follow_orientation=true)
    541553{
    542554  if (!function_exists('gd_info'))
     
    577589  $source_width  = imagesx($source_image);
    578590  $source_height = imagesy($source_image);
    579  
     591
     592  // Crop
     593  if ($crop)
     594  {
     595    $coord = get_crop_coord($source_width, $source_height, $max_width, $max_height, $follow_orientation);
     596  }
     597
    580598  $resize_dimensions = get_resize_dimensions($source_width, $source_height, $max_width, $max_height, $rotation);
    581599
     
    596614    0,
    597615    0,
    598     0,
    599     0,
     616    $crop ? $coord['x'] : 0,
     617    $crop ? $coord['y'] : 0,
    600618    $resize_dimensions['width'],
    601619    $resize_dimensions['height'],
     
    631649}
    632650
    633 function pwg_image_resize_im($source_filepath, $destination_filepath, $max_width, $max_height, $quality, $strip_metadata=false)
     651function pwg_image_resize_im($source_filepath, $destination_filepath, $max_width, $max_height, $quality, $strip_metadata=false, $crop=false, $follow_orientation=true)
    634652{
    635653  // extension of the picture filename
     
    647665  $source_width  = $image->getImageWidth();
    648666  $source_height = $image->getImageHeight();
     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  }
    649674 
    650675  $resize_dimensions = get_resize_dimensions($source_width, $source_height, $max_width, $max_height, $rotation);
     
    726751
    727752  return $rotation;
     753}
     754
     755function 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);
    728784}
    729785
Note: See TracChangeset for help on using the changeset viewer.