title: How to change Image Orientation on uploading after compressing it using exif - PHP
body: <p>I tried compressing images using <strong>PHP compressImage function</strong>  which works perfectly fine.</p>
  <p>But when I try to upload a portrait image, it compresses it and uploads it in the landscape. After surfing on this query, I came to know that when an image gets compressed, it loses its <strong>EXIF</strong> info, and the default orientation becomes 0.</p>
  <p>So, I tried storing the <strong>EXIF</strong> info before compressing the image using the <strong>below code:</strong></p>
  <pre><code>if (getimagesize($file['tmp_name'])['mime'] === 'image/jpeg') {
        $exif = exif_read_data($file['tmp_name']);
        if (!empty($exif['Orientation'])) {
          $this-&gt;file_orientation = $exif['Orientation'];
        }
      }
  </code></pre>
  <p>Now, I tried a couple of methods to use this info to change the image orientation but all came to page load failure.</p>
  <p><strong>My whole code looks like this:</strong></p>
  <pre><code>$file = ($_FILES['dp']['name']);
      
      if (getimagesize($file['tmp_name'])['mime'] === 'image/jpeg') {
        $exif = exif_read_data($file['tmp_name']);
        if (!empty($exif['Orientation'])) {
          $this-&gt;file_orientation = $exif['Orientation'];
        }
      }
      
      
      $filename = $file;
      $valid_ext = array('png','jpeg','jpg');
      $location = &quot;images-main/users/&quot;.$filename;
      
      $tempt = explode(&quot;.&quot;, $_FILES[&quot;dp&quot;][&quot;name&quot;]);
      $newfilenamet = round(microtime(true)) . '.' . end($tempt).$file; 
      
      $file_extension = pathinfo($location, PATHINFO_EXTENSION);
      $file_extension = strtolower($file_extension);
      
      $path = &quot;images-main/users/&quot;.$newfilenamet;
      
      if(in_array($file_extension,$valid_ext)){  
          compressImage($_FILES['dp']['tmp_name'], $path, 10);
      }else{
          echo &quot;Invalid file type.&quot;;
      }
  
  
  function compressImage($source, $destination, $quality) {
      $info = getimagesize($source);
      if ($info['mime'] == 'image/jpeg') 
          $image = imagecreatefromjpeg($source);
      elseif ($info['mime'] == 'image/gif') 
          $image = imagecreatefromgif($source);
      elseif ($info['mime'] == 'image/png') 
          $image = imagecreatefrompng($source);
      imagejpeg($image, $destination, $quality);
  }
  </code></pre>
  <p>Any help is greatly appreciated.</p>