title: PHP Image orientation on upload
body: <p>Im trying to make iphone images rotate when resizing, but i just cant seem to get it working.</p>
  
  <p>The upload and resize part is working fine. Its the rotation part that is giving me trouble. </p>
  
  <p>I hope someone can spot my rookie error! :-)</p>
  
  <pre><code>function image_fix_orientation($src, $filename) {
      $exif = exif_read_data($filename, 0, true);
  
      if (!empty($exif['Orientation'])) {
          $ort = $exif['IFD0']['Orientation'];
          switch ($ort) {
              case 3:
                  $src = imagerotate($src, 180, 0);
                  break;
  
              case 6:
                  $src = imagerotate($src, -90, 0);
                  break;
  
              case 8:
                  $src = imagerotate($src, 90, 0);
                  break;
          }
      }
  
      return $src;
  
  }
  
  function uploadImage($inputImage){
  
      $inputImage = $inputImage; 
      $uniqueId = uniqid();
  
      $errors=0;
  
      if ( isset($_POST['submitNewProduct']) || isset($_POST['productImageEdit']) || isset($_POST['productImageSecondEdit']) || isset($_POST['productImageThirdEdit']) ) {
  
          ini_set('memory_limit', '-1');
  
          $image =$_FILES[$inputImage]["name"];
  
          $uploadedfile = $_FILES[$inputImage]['tmp_name'];
  
          $size=filesize($_FILES[$inputImage]['tmp_name']);
  
          if(isset($_POST['productImageValue'])) {
              $productInputValue = $_POST['productImageValue'];
          } else {
              $productInputValue = 0;
          }
  
          if(isset($_POST['productImageSecondValue'])) {
              $productInputValueSecond = $_POST['productImageValueSecond'];
          } else {
              $productInputValueSecond = 0;
          }
  
          if(isset($_POST['productImageThirdValue'])) {
              $productInputValueThird = $_POST['productImageValueThird'];
          } else {
              $productInputValueThird = 0;
          }
  
  
  
          if ($productInputValue &lt; 18388608 || $productInputValueSecond &lt; 18388608 || $productInputValueThird &lt; 18388608)  {
  
              if ($size &lt; 18388608) {
  
                  if (!empty($image)) {
  
                      $filename = stripslashes($_FILES[$inputImage]['name']);
                      $extension = getExtension($filename);
                      $extension = strtolower($extension);
  
                      if (($extension != "jpg") &amp;&amp; ($extension != "jpeg") &amp;&amp; ($extension != "png") &amp;&amp; ($extension != "gif")) {
  
                          echo "&lt;h3&gt;Forkert filformat, kun JPG, PNG og GIF er tilladt.&lt;/h3&gt;";
                          $errors=1;
  
                      } 
  
                      else {
  
                          if($extension=="jpg" || $extension=="jpeg" ) {
                            $uploadedfile = $_FILES[$inputImage]['tmp_name'];
                            $src = imagecreatefromjpeg($uploadedfile);
                            $src = image_fix_orientation($src, $filename);
                          }
  
                          else if($extension=="png") {
                              $uploadedfile = $_FILES[$inputImage]['tmp_name'];
                              $src = imagecreatefrompng($uploadedfile);
                          } 
  
                          else {
                              $src = imagecreatefromgif($uploadedfile);
                          }
  
                          list($width,$height)=getimagesize($uploadedfile);
  
                          if($width &lt;= 800) {
                              $newwidth= $width;
                          } else {
                              $newwidth= 800;
                          }
  
                          if(!$errors) {
  
                              $newheight=($height/$width)*$newwidth;
                              $tmp=imagecreatetruecolor($newwidth,$newheight);
                              imagefilledrectangle($tmp, 0, 0, $newwidth, $newheight, imagecolorallocate($tmp, 255, 255, 255));
                              imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
  
                              $filename = "uploads/". $uniqueId . "-" . $_FILES[$inputImage]['name'];
  
                              imagejpeg($tmp,$filename,100);
  
                              imagedestroy($src);
                              imagedestroy($tmp);
  
                          } 
  
                          else {
                              echo "&lt;h3&gt;Der skete en fejl, prøv venligst igen&lt;/h3&gt;";
                          }
  
                      }
  
                  }
  
              } else {
                  echo "&lt;h3&gt;Billedet: " . $_FILES[$inputImage]["name"] . " fylder for meget.&lt;/h3&gt;";
              }
  
          } else {
              echo "&lt;h3&gt;Et af billederne fylder for meget.&lt;/h3&gt;";
          }
  
      }
  
          $productImage['name'] = $uniqueId . "-" . basename($_FILES[$inputImage]["name"]);
          $productImage['ort'] = $convertToInt;
  
          return $productImage;
  
  }
  </code></pre>
  
  <p>EDIT: I tried the solution posted in answers, but now i get this error:
  <a href="https://gyazo.com/591feaa396e194a69a012715d02a32e7" rel="nofollow">https://gyazo.com/591feaa396e194a69a012715d02a32e7</a></p>
  
  <p>Thank you so much for your time in advance!</p>
  
  <p>Best regards! :-) </p>