Changeset 7868


Ignore:
Timestamp:
Nov 24, 2010, 1:45:16 PM (13 years ago)
Author:
plg
Message:

feature 2040 added: automatic rotation of the photo based on EXIF Orientation.

The code could have been shorter but this one 1) checks the resize dimension
with the rotation in mind 2) rotates the photo once resized to reduce memory
used.

File:
1 edited

Legend:

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

    r7490 r7868  
    184184function need_resize($image_filepath, $max_width, $max_height)
    185185{
     186  // TODO : the resize check should take the orientation into account. If a
     187  // rotation must be applied to the resized photo, then we should test
     188  // invert width and height.
    186189  list($width, $height) = getimagesize($image_filepath);
    187190 
     
    192195
    193196  return false;
     197}
     198
     199function get_resize_dimensions($width, $height, $max_width, $max_height, $rotation=null)
     200{
     201  $rotate_for_dimensions = false;
     202  if (isset($rotation) and in_array(abs($rotation), array(90, 270)))
     203  {
     204    $rotate_for_dimensions = true;
     205  }
     206
     207  if ($rotate_for_dimensions)
     208  {
     209    list($width, $height) = array($height, $width);
     210  }
     211 
     212  $ratio_width  = $width / $max_width;
     213  $ratio_height = $height / $max_height;
     214 
     215  // maximal size exceeded ?
     216  if ($ratio_width > 1 or $ratio_height > 1)
     217  {
     218    if ($ratio_width < $ratio_height)
     219    {
     220      $destination_width = ceil($width / $ratio_height);
     221      $destination_height = $max_height;
     222    }
     223    else
     224    {
     225      $destination_width = $max_width;
     226      $destination_height = ceil($height / $ratio_width);
     227    }
     228  }
     229
     230  if ($rotate_for_dimensions)
     231  {
     232    list($destination_width, $destination_height) = array($destination_height, $destination_width);
     233  }
     234 
     235  return array(
     236    'width' => $destination_width,
     237    'height'=> $destination_height,
     238    );
    194239}
    195240
     
    222267  {
    223268    die('unsupported file extension');
     269  }
     270
     271  $rotation = null;
     272  if (function_exists('imagerotate'))
     273  {
     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    }
    224291  }
    225292 
     
    228295  $source_height = imagesy($source_image);
    229296 
    230   $ratio_width  = $source_width / $max_width;
    231   $ratio_height = $source_height / $max_height;
    232  
    233   // maximal size exceeded ?
    234   if ($ratio_width > 1 or $ratio_height > 1)
    235   {
    236     if ($ratio_width < $ratio_height)
    237     {
    238       $destination_width = ceil($source_width / $ratio_height);
    239       $destination_height = $max_height;
    240     }
    241     else
    242     {
    243       $destination_width = $max_width;
    244       $destination_height = ceil($source_height / $ratio_width);
    245     }
    246   }
    247   else
     297  $resize_dimensions = get_resize_dimensions($source_width, $source_height, $max_width, $max_height, $rotation);
     298
     299  // testing on height is useless in theory: if width is unchanged, there
     300  // should be no resize, because width/height ratio is not modified.
     301  if ($resize_dimensions['width'] == $source_width and $resize_dimensions['height'] == $source_height)
    248302  {
    249303    // the image doesn't need any resize! We just copy it to the destination
     
    252306  }
    253307 
    254   $destination_image = imagecreatetruecolor($destination_width, $destination_height);
     308  $destination_image = imagecreatetruecolor($resize_dimensions['width'], $resize_dimensions['height']);
    255309 
    256310  imagecopyresampled(
     
    261315    0,
    262316    0,
    263     $destination_width,
    264     $destination_height,
     317    $resize_dimensions['width'],
     318    $resize_dimensions['height'],
    265319    $source_width,
    266320    $source_height
    267321    );
     322
     323  // rotation occurs only on resized photo to avoid useless memory use
     324  if (isset($rotation))
     325  {
     326    $destination_image = imagerotate($destination_image, $rotation, 0);
     327  }
    268328 
    269329  $extension = strtolower(get_extension($destination_filepath));
Note: See TracChangeset for help on using the changeset viewer.