1

I am not sure if there is any fixed from this, but nevertheless the problem as follows.

Problem: My client took a picture using iphone. As it was in a wrong orientation, he used the inbuilt photo editor in his phone to flip it to the correct orientation. However, when he uploads the photo from his mac,the photo actually returns to the wrong orientation.

On mac, the corrected picture was seen in the correct orientation. But when sent to a windows pc or uploaded to a linux server, the picture returns to it's original wrong orientation.

Is this a mac issue or otherwise? Or is there any fix that can be done in PHP script. The following is the php script that uploads the photo.

 $file=array();
    $listingid=$_POST['listing'];
    if(!empty($_FILES['file'])){
        foreach($_FILES['file']['name'] as $key=>$name){
            $extt=pathinfo($name, PATHINFO_EXTENSION);
            $name=$this->mediacontrol->generateUniqueKey(32);
            $name.=".".$extt;
            if($_FILES['file']['error'][$key]==0 && move_uploaded_file($_FILES['file']['tmp_name'][$key],"uploads/{$name}")){
                $file['file_name']=$name;
                $file['file_type']=$_FILES['file']['type'][$key];
                $file['file_path']="uploads/{$name}";
                $file['full_path']=  base_url()."uploads/{$name}";
                $ext=explode(".",$name);
                $file['thumb_path']=base_url()."uploads/thumbnails/250/".$ext[0]."_thumb.".$ext[1];
                $file['thumb_short']="uploads/thumbnails/250/".$ext[0]."_thumb.".$ext[1];
                $file['link_folder']="uploads/";
                $file['raw_name']="null";
                $file['orig_name']="$name";
                $file['client_name']="null";
                $file['file_size']=$_FILES['file']['size'][$key];
                $file['is_image']="1";
                $file['image_width']="0";
                $file['image_height']="0";
                $file['image_type']=$_FILES['file']['type'][$key];
                $file['image_size_str']="width:0px;height:0px;";

                $id=$this->mediacontrol->insertData($file);
                $file_id[]=$id;

                    $config['image_library'] = 'GD2';
                    $config['source_image'] = "".$file['file_path'];
                    $config['new_image']="uploads/thumbnails/250/";
                    $config['create_thumb'] = TRUE;
                    $config['maintain_ratio'] = TRUE;
                    $config['width']     = 250;
                    $config['height']   = 250;

                    $this->image_lib->initialize($config);
                    $this->image_lib->resize();
                    $this->image_lib->clear();

                            $this->listing_model->matchMedia($id,$listingid);  
                    $this->listing_model->updateStage(1,$id);
                    echo "Successfully uploaded ".$name."<br>";
            }
            else{
                echo "Something went terribly wrong when uploading".$name;
            }
        }
    }

Please advise.

Thank you.

Mark Seah
  • 532
  • 5
  • 18
  • If image_lib uses imagemagick this might be applicable check out http://stackoverflow.com/questions/4266656/how-to-stop-php-imagick-auto-rotating-images-based-on-exif-orientation-data – Orangepill Jun 07 '13 at 15:00
  • Very close. But I am not using imagemagick library, I am using GD2. Is this relevant? Thanks anyway, because now I know the cause could be the orientation flag. – Mark Seah Jun 07 '13 at 15:07

1 Answers1

4

Sorry for wasting any body's time. I found the problem and the fix to my problem.

The problem: Some photo editors used by users does not physically rotate the photos, they basically just update the meta data attached to the image. This caused problems because some environments does not read that data. It is pretty obvious in windows and in my case.

The solution: So there is no choice but to do an additional step when uploading picture. That is to check the exif data of the image and do the necessary adjustment. I am using codeigniter with GD2 image manipulation library. The code that fixed my problem as follows.

$config['image_library'] = 'GD2';
                    $config['source_image'] = "../rentura/".$file['file_path'];
                    $config['new_image']="../rentura/uploads/thumbnails/250/";
                    $config['create_thumb'] = TRUE;
                    $config['maintain_ratio'] = TRUE;
                    $config['width']     = 250;
                    $config['height']   = 250;
                    $imgdata=exif_read_data($file['full_path'], 'IFD0');
                                        switch($imgdata['Orientation']) {
                    case 3:
                        $config['rotation_angle']=180;
                        break;
                    case 6:
                        $config['rotation_angle']=-90;
                        break;
                    case 8:
                        $config['rotation_angle']=90;
                        break;
                }

                    $this->image_lib->initialize($config);

                    if(!$this->image_lib->resize()){

                        echo $this->image_lib->display_errors();
                    }
                    $this->image_lib->rotate();
                    $this->image_lib->clear();

Not so sure how will you grade this solution. But if there is any better solution, please please let me know. Thank you.

Mark Seah
  • 532
  • 5
  • 18