Same name and namespace in other branches
  1. 7.x-1.x exif_orientation.module \_exif_orientation_rotate() 1 comment

Rotates an image to its EXIF Orientation.

iPhone 4 and up save all images in landscape, relying on EXIF data to set the orientation properly. This does not always translate well in the browser or other devices. @link: http://www.daveperrett.com/articles/2012/07/28/exif-orientation-handlin…

2 calls to _exif_orientation_rotate()
exif_orientation_file_presave in ./exif_orientation.module
Implements hook_file_presave().
exif_orientation_validate_image_rotation in ./exif_orientation.module
Ensures that image orientation is according to exif data.

File

./exif_orientation.module, line 58

Code

function _exif_orientation_rotate($file) {
    if (function_exists('exif_read_data') && $file->getMimeType() == 'image/jpeg') {
        $file_exif = @exif_read_data(\Drupal::service('file_system')->realpath($file->getFileUri()));
        // Ensure that the Orientation key|value exists, otherwise leave.
        if (!is_array($file_exif) || !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) {
            $image = \Drupal::service('image.factory')->get($file->getFileUri());
            if ($image->rotate($degrees)) {
                $image->save();
            }
        }
    }
}