Wireless Army
This is a blog / tips and tricks website for web developers and security researchers.
follow us in feedly


fix image upload rotation
by admin
 at 2017-06-06 08:17:00.

if you are making a web app that allow upload, it may have a problem with the oriantation since mobile cameras are horizontal.
here is a half example for fixing it in php

<form id="post">
<input type="file" accept="image/*" name="upload">
<input name="post" value="submit" type="submit" />
</form>

 

if(isset($_POST["post"])) {
  $target_dir ="imagefolder";
  $name = $_FILES["upload"]["name"];
  $type = $_FILES["upload"]["type"];
  $size = $_FILES["upload"]["size"];
  $error = $_FILES["upload"]["error"];
  $temp = $_FILES["upload"]["tmp_name"];   if(move_uploaded_file($temp, $target_dir.$name)) {
    $exif = exif_read_data($target_dir.$name);
    if (!empty($exif['Orientation'])) {
      $img = imagecreatefromjpeg($target_dir.$name);
          switch ($exif['Orientation']) {
          case 3:
              $newimg = imagerotate($img, 180, 0);
              break;
          case 6:
              $newimg = imagerotate($img, -90, 0);
              break;
          case 8:
              $newimg = imagerotate($img, 90, 0);
              break;
    }
  }
    imagejpeg($newimg, $target_dir.$name);
  }
  else {
    echo $error_message = 'Error: Upload Unsuccessful<br />Please Try Again';
  }
}