The downloaded file from the iPhone / iPad does not rotate correctly, despite the image rotation script

I have a script that downloads a file. I added a function that should rotate the image for photos downloaded from iPhone or iPad .. because depending on how they were taken, they could be displayed sideways or upside down. I think I understood the code correctly, but for some reason it still doesn’t spin when loading. What am I doing wrong?

    $uploadedFileName = $this->relative_path . $base_dir . $file_name;
    $is_upload = @copy($file_details['tmp_name'], $uploadedFileName);
    $exif = exif_read_data($uploadedFileName);
    $orientation = $exif['COMPUTED']['Orientation'];


    if (isset($orientation)) {

       switch ($orientation) {
          case 3:
             $image_p = imagerotate($uploadedFileName, 180, 0);
             break;
          case 6:
             $image_p = imagerotate($uploadedFileName, -90, 0);
             break;
          case 8:
             $image_p = imagerotate($uploadedFileName, 90, 0);
             break;
       }
       // Output
       imagejpeg($image_p, $uploadedFileName, 100);
    }
+3
source share
1 answer

Orientation is directly stored in exif data. Therefore, your code should look like this:

$exif = exif_read_data($uploadedFileName);
$orientation = $exif['Orientation'];
switch ($orientation) {
   case 3:
      $image_p = imagerotate($uploadedFileName, 180, 0);
      break;
   case 6:
      $image_p = imagerotate($uploadedFileName, -90, 0);
      break;
   case 8:
      $image_p = imagerotate($uploadedFileName, 90, 0);
      break;
}
imagejpeg($image_p, $uploadedFileName, 100);
0
source

All Articles