tags:

views:

470

answers:

3

I'm using Codeigniter's image manipulation library to re-size an uploaded image to three sizes, small, normal and large.

The re-sizing is working great. However, if I'm resizing a vertical image, the library is rotating the image so it's horizontal.

These are the config settings I have in place:

     $this->resize_config['image_library'] = 'gd2';
     $this->resize_config['source_image'] = $this->file_data['full_path'];
     $this->resize_config['maintain_ratio'] = TRUE;

     // These change based on the type (small, normal, large)
     $this->resize_config['new_image'] = './uploads/large/'.$this->new_file_name.'.jpg';
     $this->resize_config['width'] = 432;
     $this->resize_config['height'] = 288;

I'm not setting the master_dim property because the default it set to auto, which is what I want.

My assumption is that the library would take a vertical image, see that the height is greater than the width and translate the height/width config appropriately so the image remains vertical.

What is happening (apparently) is that the library is rotating the image when it is vertical and sizing it per the configuration.

This is the code in place I have to do the actual re-sizing:

        log_message('debug', 'attempting '.$size.' photo resize');
        $this->CI->load->library('image_lib');
        $this->CI->image_lib->initialize($this->resize_config);
        if ($this->CI->image_lib->resize())
        {
              $return_value = TRUE;
              log_message('debug', $size.' photo resize successful');
        }
        else
        {
            $this->errors[] = $this->CI->image_lib->display_errors();
            log_message('debug', $size.' photo resize failed');
        }
        $this->CI->image_lib->clear();
        return $return_value;

EDIT

I think the problem may be from the upload library. When I get the image_height and image_width back from the upload, the width seems to be larger even though I uploaded a vertical image.

This is a portion of the code I'm using to upload the photo:

     $this->upload_config['allowed_types'] = 'jpg|jpeg';
     $this->upload_config['max_size']  = '2000';
     $this->upload_config['max_width']  = '0';
     $this->upload_config['max_height']  = '0';
     $this->upload_config['upload_path'] = './uploads/working/';


     $this->CI->load->library('upload', $this->upload_config);
     if ($this->CI->upload->do_upload($this->posted_file))
     {
         $this->file_data = $this->CI->upload->data();
         $return_value = TRUE;
         log_message('debug', 'upload successful');
     }

I added some logging to check the values:

     $this->is_vertical = $this->file_data['image_height'] > $this->file_data['image_width'];
     log_message('debug', 'image height:'.$this->file_data['image_height']);
     log_message('debug', 'image width:'.$this->file_data['image_width']);
     if ($this->is_vertical)
     {
         $this->resize_config['master_dim'] = 'height';
     }
     else
     {
         $this->resize_config['master_dim'] = 'width';
     }
    log_message('debug', 'master_dim setting:'.$this->resize_config['master_dim']); 

These are the results of the log:

DEBUG - 2010-03-16 18:35:06 --> image height:1536 DEBUG - 2010-03-16 18:35:06 --> image width:2048 DEBUG - 2010-03-16 18:35:06 --> master_dim setting:width

Looking at the image in photoshop, these are the dimensions:

height: 2048 width: 1536

Anyone know what might be causing the upload library to do this?

+1  A: 

I've never used this library, but having read the documentation, I wonder whether the master_dim property might help. If you set this to 'height' for vertical images that might keep them the right way up. You could just parse each image through a conditional to see if the image is vertically aligned and then only set this property if need be. My other thought is about the maintain_ratio property. The documentation says that with this set to 'TRUE' it will resize as close to the target values as possible whilst maintaining the aspect ratio. I wonder if it thinks that rotating the image allows it to preserve this ratio more accurately? As an experiment, try setting this value to 'FALSE' for vertical images.

musoNic80
Thanks for the reply. I'll give this a shot tonight. Wouldn't you think though, that the library would handle setting the orientation properly by itself? The documentation makes it sound like it will.
someoneinomaha
See my edits above. I discovered that the upload class was bringing back inaccurate height and width information for the uploaded photo.
someoneinomaha
A: 

Ok - I decided not to trust photoshop and opened the images I was testing in quicktime and safari. I discovered that they were actually still horizontal.

So Codeigniter was operating exactly as expected.

I went back into photoshop, did a save for web on the test images, re-uploaded them and it worked as expected.

I then stripped out the extra code that I had added to test whether the image was vertical and the library works as I expected it would.

Now - I need to figure out how to prevent end users from doing this exact thing.

Thanks for taking the time to answer my question musoNic80. Hopefully someone else can learn from my mistakes here.

someoneinomaha
Glad you've worked it out. Good look with figuring out the next bit!
musoNic80
A: 

I have the exact same problem in my script.

Then I tried uploading a vertical image taken with my Canon 350D and it worked fine, but then uploading vertical images from my Powershot A510 the problem reappeared. I then saved for web from photoshop and then my script started working again like you experienced.

I checked the EXIF data for both pictures and on the one codeigniter is having problems with there was this extra bit of info:

Orientation: 6 (Rotated 90° CCW)

If only there was some way of removing this EXIF data from the picture in Codeigniter after the upload...

EDIT

Just had a quick look on the web and found that ImageMagick has a command line function

mogrify -strip imagename.jpg

that will remove all EXIF data from the image.

Does anyone know how to run a command line like that through Codeigniter or even if that is possible?

calumogg
Not sure if it's possible... might be something you should post as a separate question - how to programmatically remove EXIF data in php/Codeigniter from files uploaded.
someoneinomaha