I am encountering a problem using imagefield crop together with imagecache actions autorotate. When my users upload images with iPhone or iPad, imagecache autorotate automatically reads EXIF data and gives the image the correct orientation (i.e. portrait or landscape). However, when I am using imagefield crop widget to upload these images, the thumbnail in the node edit screen has correct orientation, however the output after cropping is rotated 90 degrees.

After extensive searching, there is a common problem with correcting the orientation of iPhone and iPad image uploads using autorotate. However, it seems as though when you use imagefield crop as well, it does not work.

From my research, my hunch is that imagefield crop somehow modifies/deletes the EXIF data of the original uploaded image, and therefore the autorotate operation does not know how to correctly orient the resulting image.

Please let me know if I can help in any way, as I've spent quite some time on this with no success. I'm happy to walk people through it on my site if that helps as well. I would add a bounty for this, but not sure how.

Much thanks!

Comments

joetsuihk’s picture

This is marginally a bug. imagefield_crop 7.x use drupal's default image_crop and image_scale_and_crop function.

So it is actually Drupal core who removed the exif.

Further digging, ImageMagick seems can preserve exif data: http://drupal.org/node/1663518

So you may try that. Please report back if ImageMagick does work.

jcsnyder’s picture

Hi joetsuihk,

Thanks a lot for your reply. I installed imagemagick to try it. Long story short, it is handling the orientation of iPhone/iPad images correctly now. However, it is still encountering problems cropping portrait images when cropping off the top of the image.

John

joetsuihk’s picture

Can you open a new issue or edit the issue title for "problems cropping portrait images when cropping off the top of the image"?

Can you describe the bug a bit more?

Thanks!

jcsnyder’s picture

Title: Issue with using imagefield crop together with imagecache actions autorotate operation on iPhone and iPad EXIF data » Problems cropping portrait images from iPhone or iPad

Will do.

To clarify, the bug occurs anytime I am uploading an image from and iPhone or iPad (iOS6) that was taken in portrait mode. I am using Imagefield Crop with GD image-toolkit and Imagecache Actions Autorotate. Though you have to set the crop area such that the top of the image is cropped off in order to see the bug, it is just a coincidence that it works when you don't crop off the top, and therefore still considered a bug anytime in portrait mode.

The issue can be easily reproduced using imagefield crop, imagecache actions autorotate, and uploading from iphone or ipad. The issue has something to do with the EXIF data, when the autorotate is processed, etc.

Please let me know if I can help in any way or you'd like me to show you the bug in screenshare.

Thanks,
John

Anonymous’s picture

This issue still exists.

Using the following:

- 7.x-2.0+11-dev
- Jcrop-0.9.12
- ImageMagick

Issue: Photos taken with iPad, iPhone, etc. in Portrait mode are cut off in the crop preview box. Landscape photos work fine.

nlisgo’s picture

I have fixed this issue and will get round to submitting a patch but here is the updated imagefield_crop_widget_preview_process function

function imagefield_crop_widget_preview_process($element, &$form_state, $form) {
  $file = $element['#file'];
  if ($file->fid == 0) {
    return $element;
  }
  // The widget belongs to the parent, so we got to find it first
  $parents = array_slice($element['#array_parents'], 0, -1);
  $parent = drupal_array_get_nested_value($form, $parents);
  $instance = field_widget_instance($parent, $form_state);
  list($width, $height) = !empty($instance['widget']['settings']['resolution']) ? explode('x', $instance['widget']['settings']['resolution']) : array(0, 0);

  $exif = exif_read_data(drupal_realpath($file->uri));
  $degrees = 0;
  if (isset($exif['Orientation'])) {
    switch ($exif['Orientation']) {
      case 3:
        $degrees = 180;
        break;
      case 6:
        $degrees = 90;
        break;
      case 8:
        $degrees = 270;
        break;
    }
  }
  if ($degrees > 0) {
    $realpath = drupal_realpath($file->uri);
    $image = image_load($realpath);
    image_rotate($image, $degrees);
    $success = image_save($image, $file->uri);
    if ($success) {
      $file->uri = file_unmanaged_copy($file->uri, $file->uri);
      file_save($file);
      file_unmanaged_delete($realpath);
    }    
  }
  
  $image_info = image_get_info(drupal_realpath($file->uri));
  $settings = array(
    $parent['#id'] => array(
      'preview' => array(
        'orig_width' => $image_info['width'],
        'orig_height' => $image_info['height'],
        'width' => (integer)$width,
        'height' => (integer)$height,
      ),
    ),
  );

  $element['#attached']['js'][] = array(
    'data' => array('imagefield_crop' => $settings),
    'type' => 'setting',
    'scope' => 'header',
  );
  $element['#imagefield_crop'] = array(
    '#file' => $element['#file'],
    '#width' => $width,
    '#height' => $height,
    '#path' => file_create_url($file->uri),
  );
  return $element;
}
joetsuihk’s picture

Thanks. but we need this to be a patch to be reviewed by the community. Please refer to https://drupal.org/node/209591/git-instructions/7.x-1.x for easy patch creation.

I also suggest wrapping exif_read_data() with function_exists()