EllisLab text mark
Advanced Search
     
Automatic image rotation based on EXIF data
Posted: 06 October 2012 10:55 PM   [ Ignore ]
Joined: 2012-09-09
3 posts

The library automatically rotates the provided image based on the embedded EXIF data. I use it in my projects to correctly display images uploaded from mobile devices.

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
* @file application/libraries/Image_autorotate.php
*/
class Image_autorotate
{
 
function __construct($params NULL{
  
  
if (!is_array($params) || empty($params)) return FALSE;
  
  
$filepath $params['filepath'];
  
$exif = @exif_read_data($filepath);
  
  if (empty(
$exif['Orientation'])) return FALSE;
  
  
$CI =& get_instance();
  
$CI->load->library('image_lib');
  
  
$config['image_library''gd2';
  
$config['source_image'$filepath;
  
  
$oris = array();
  
  switch(
$exif['Orientation'])
  
{
   
case 1// no need to perform any changes
   
break;
 
   case 
2// horizontal flip
   
$oris[] 'hor';
   break;
                                 
   case 
3// 180 rotate left
   
$oris[] '180';
   break;
                     
   case 
4// vertical flip
   
$oris[] 'ver';
   break;
                 
   case 
5// vertical flip + 90 rotate right
   
$oris[] 'ver';
   
$oris[] '270';
   break;
                 
   case 
6// 90 rotate right
   
$oris[] '270';
   break;
                 
   case 
7// horizontal flip + 90 rotate right
   
$oris[] 'hor';
   
$oris[] '270';
   break;
                 
   case 
8// 90 rotate left
   
$oris[] '90';
   break;
   
   default: break;
  
}
  
  
foreach ($oris as $ori{
   $config[
'rotation_angle'$ori;
   
$CI->image_lib->initialize($config); 
   
$CI->image_lib->rotate();
  
}
 }
}

// END class Image_autorotate

/* End of file Image_autorotate.php */
/* Location: ./application/libraries/Image_autorotate.php */ 

Usage example:

$imageinfo $this->upload->data();
$full_path $imageinfo['full_path'];

// check EXIF and autorotate if needed
$this->load->library('image_autorotate', array('filepath' => $full_path)); 

Inspired by this post on PHP.net (thanks to the author).

 
Posted: 07 October 2012 04:55 AM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Joined: 2009-04-15
454 posts

nice - I like simple, useful classes. You should consider posting to a github repo and/or composer to make it easier to bookmark and share

 Signature 

Code By Jeff

Mahana Messaging Library

Problem with your query? Did you run

$this->db->last_query(); 

before you came to the forums for help?

 
Posted: 09 October 2012 11:56 PM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Joined: 2012-09-09
3 posts

Thanks for feedback. It’s certainly in my plans to share this on github, but as of the time being I’m pretty busy with my projects. I’m new to Codeigniter (and I absolutely love it) so I wanted to hear from other community members if anyone had the same issue with mobile devices and if the problem was supposed to be solved in some other way.

 
Posted: 21 October 2012 12:03 PM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Joined: 2012-09-09
3 posts

Here is a GitHub link as requested. Share and enjoy!
P.S. I was not able to update my original post with a link due to forum’s restrictions.