Hi
I am using EXIF Orientation to get the orientation of the images coming from iphone5 and 6. Unfortunately when I print $file_exif not able to get the orientation of that images. If I have download some sample images from google having orientation I can see the value in the variable.

I have also tried to customize a bit still not able get the orientation.

Original Code in the Module :


function _exif_orientation_rotate($file) {
  if (function_exists('exif_read_data') && $file->filemime == 'image/jpeg') {
    $file_exif = exif_read_data(drupal_realpath($file->uri));

    // Ensure that the Orientation key|value exists, otherwise leave.
    if (!isset($file_exif['Orientation'])) {
      return;
    }
    // Orientation numbers and corresponding degrees.
    // @note: Odd numbers are flipped images, would need different process.
    switch ($file_exif['Orientation']) {
      case 3:
          $degrees = 180;
          break;
      case 6:
          $degrees = 90;
          break;
      case 8:
          $degrees = 270;
          break;
      default:
          $degrees = 0;
    }

    if ($degrees > 0) {
      // Load the image object for manipulation
      $file_image = image_load(drupal_realpath($file->uri));

      if (image_rotate($file_image, $degrees)) {
        image_save($file_image);
      }
    }
  }
}

Modified Code to check :

if (function_exists('exif_read_data') && $file->filemime == 'image/jpeg') {
//    $file_exif = exif_read_data(drupal_realpath($file->uri));
//    // Ensure that the Orientation key|value exists, otherwise leave.
//    if (!isset($file_exif['Orientation'])) {
//      return;
//    }
    $image_file_path = drupal_realpath($file->uri);
//    $image_file_path = $image_file_info[0]->filepath;
    $imge_info = image_get_info($image_file_path);
    $img = imagecreatefromjpeg("{$image_file_path}");
        $width = $imge_info['width'];
        $height = $imge_info['height'];
        $thumbWidth = $imge_info['width'];
// calculate thumbnail size
        $new_width = $thumbWidth;
        $new_height = floor($height * ( $thumbWidth / $width ));
// create a new temporary image
        $tmp_img = imagecreatetruecolor($new_width, $new_height);
// copy and resize old image into new image
        imagecopyresized($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
        $exif = exif_read_data(drupal_realpath($file->uri));
        $file_exif = $exif["Orientation"];
        
        
    // Orientation numbers and corresponding degrees.
    // @note: Odd numbers are flipped images, would need different process.
        echo '<pre>';
print_r($exif); die;
    switch ($file_exif['Orientation']) {
      case 3:
          $degrees = 180;
          break;
      case 6:
          $degrees = 90;
          break;
      case 8:
          $degrees = 270;
          break;
      default:
          $degrees = 0;
    }

    if ($degrees > 0) {
      // Load the image object for manipulation
      $file_image = image_load(drupal_realpath($file->uri));

      if (image_rotate($file_image, $degrees)) {
        image_save($file_image);
      }
    }
  }
}

Thanks
Jyotisankar

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

ReBa’s picture

I've been dealing with the same issue for my iphone device.
There was an issue within the image.gd.inc image_gd_rotate function where the (only) jpeg image can't get a proper background and the background returned -1 which broke the resource to become a boolean in stead of a resource object.

Please review.

mglaman’s picture

Issue summary: View changes

Updating issue formatting.

mglaman’s picture

Status: Needs review » Fixed

Wow, great catch, and that makes complete sense.

  • mglaman committed b9883a3 on 7.x-1.x authored by ReBa
    Issue #2540228 by ReBa: Not able to get the orientation of images from...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

emilymoi’s picture

Status: Closed (fixed) » Needs work
FileSize
434 bytes

Was running an older version of the module and just noticed the problem with image.gd.inc today.

What I don't understand is why a string 'FFFFFF' is passed instead of a hex int 0xFFFFFF here. The documentation clearly asks for a hex int and if you take a look in image.gd.inc, it's doing shift operations on the background variable.

Passing a string 'FFFFFF' does not break only because the shift operations in image.gd.inc end up with a valid background color of RGB 0,0,0 and because we're only rotating in increments of 90 degrees we don't see the black background.

This should be changed to

image_rotate($file_image, $degrees, 0xFFFFFF);
RunePhilosof’s picture

Status: Needs work » Needs review

The last submitted patch, 1: exif_orientation-orientation-iphone5-6-2540228-2-7.39.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 6: correct_background_color.patch, failed testing.

othermachines’s picture

Status: Needs work » Fixed

Problem mentioned in #6 was fixed in #2781621-4: Invalid hex value specified.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.