title: PHP read_exif_data and Adjust Orientation
body: <p>I am using the following code to rotate an uploaded jpeg image if the orientation is off.  I am only having problems with images uploaded from iPhones and Android.</p>
  
  <pre><code>if(move_uploaded_file($_FILES['photo']['tmp_name'], $upload_path . $newfilename)){
              chmod($upload_path . $newfilename, 0755);
              $exif = exif_read_data($upload_path . $newfilename);
              $ort = $exif['IFD0']['Orientation'];
              switch($ort)
              {
  
                  case 3: // 180 rotate left
                      $image-&gt;imagerotate($upload_path . $newfilename, 180, -1);
                      break;
  
  
                  case 6: // 90 rotate right
                      $image-&gt;imagerotate($upload_path . $newfilename, -90, -1);
                      break;
  
                  case 8:    // 90 rotate left
                      $image-&gt;imagerotate($upload_path . $newfilename, 90, -1);
                      break;
              }
              imagejpeg($image, $upload_path . $newfilename, 100);
              $success_message = 'Photo Successfully Uploaded';
          }else{
              $error_count++;
              $error_message = 'Error: Upload Unsuccessful&lt;br /&gt;Please Try Again';
          }
  </code></pre>
  
  <p>Am I doing something wrong with the way I am reading the EXIF data from the jpeg?  It is not rotating the images as it is supposed to.</p>
  
  <p>This is what happens when I run a var_dump($exif);</p>
  
  <pre><code>array(41) {
      ["FileName"]=&gt; string(36) "126e7c0efcac2b76b3320e6187d03cfd.JPG"
      ["FileDateTime"]=&gt; int(1316545667)
      ["FileSize"]=&gt; int(1312472)
      ["FileType"]=&gt; int(2)
      ["MimeType"]=&gt; string(10) "image/jpeg"
      ["SectionsFound"]=&gt; string(30) "ANY_TAG, IFD0, THUMBNAIL, EXIF"
      ["COMPUTED"]=&gt; array(8) {
          ["html"]=&gt; string(26) "width="2048" height="1536""
          ["Height"]=&gt; int(1536)
          ["Width"]=&gt; int(2048)
          ["IsColor"]=&gt; int(1)
          ["ByteOrderMotorola"]=&gt; int(1)
          ["ApertureFNumber"]=&gt; string(5) "f/2.8"
          ["Thumbnail.FileType"]=&gt; int(2)
          ["Thumbnail.MimeType"]=&gt; string(10) "image/jpeg" }
          ["Make"]=&gt; string(5) "Apple"
          ["Model"]=&gt; string(10) "iPhone 3GS"
          ["Orientation"]=&gt; int(6)
          ["XResolution"]=&gt; string(4) "72/1"
              ["YResolution"]=&gt; string(4) "72/1" ["ResolutionUnit"]=&gt; int(2) ["Software"]=&gt; string(5) "4.3.5" ["DateTime"]=&gt; string(19) "2011:09:16 21:18:46" ["YCbCrPositioning"]=&gt; int(1) ["Exif_IFD_Pointer"]=&gt; int(194) ["THUMBNAIL"]=&gt; array(6) { ["Compression"]=&gt; int(6) ["XResolution"]=&gt; string(4) "72/1" ["YResolution"]=&gt; string(4) "72/1" ["ResolutionUnit"]=&gt; int(2) ["JPEGInterchangeFormat"]=&gt; int(658) ["JPEGInterchangeFormatLength"]=&gt; int(8231) } ["ExposureTime"]=&gt; string(4) "1/15" ["FNumber"]=&gt; string(4) "14/5" ["ExposureProgram"]=&gt; int(2) ["ISOSpeedRatings"]=&gt; int(200) ["ExifVersion"]=&gt; string(4) "0221" ["DateTimeOriginal"]=&gt; string(19) "2011:09:16 21:18:46" ["DateTimeDigitized"]=&gt; string(19) "2011:09:16 21:18:46" ["ComponentsConfiguration"]=&gt; string(4) "" ["ShutterSpeedValue"]=&gt; string(8) "3711/949" ["ApertureValue"]=&gt; string(9) "4281/1441" ["MeteringMode"]=&gt; int(1) ["Flash"]=&gt; int(32) ["FocalLength"]=&gt; string(5) "77/20" ["SubjectLocation"]=&gt; array(4) { [0]=&gt; int(1023) [1]=&gt; int(767) [2]=&gt; int(614) [3]=&gt; int(614) } ["FlashPixVersion"]=&gt; string(4) "0100" ["ColorSpace"]=&gt; int(1) ["ExifImageWidth"]=&gt; int(2048) ["ExifImageLength"]=&gt; int(1536) ["SensingMethod"]=&gt; int(2) ["ExposureMode"]=&gt; int(0) ["WhiteBalance"]=&gt; int(0) ["SceneCaptureType"]=&gt; int(0) ["Sharpness"]=&gt; int(1) }
  </code></pre>