Link to home
Start Free TrialLog in
Avatar of augustkrys
augustkrys

asked on

PHP Upload with IOS devices

When uploading through my website form IOS device the photos have the worng orientation. Which is know.  Below is the code that i use form my simple upload form and the orientation code-  Need help putting them together

$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);


if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& in_array($extension, $allowedExts)) {
  if ($_FILES["file"]["error"] > 0) {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
  } else {
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
    
    if (file_exists("upload/" . $_FILES["file"]["name"])) {
      echo $_FILES["file"]["name"] . " already exists. ";
    } else {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
    }
  }
} else {
  echo "Invalid file";
}

Open in new window


Here is the Orientation correction

$image = imagecreatefromstring(file_get_contents($_FILES['image_upload']['tmp_name']));
$exif = exif_read_data($_FILES['image_upload']['tmp_name']);
if(!empty($exif['Orientation'])) {
    switch($exif['Orientation']) {
        case 8:
            $image = imagerotate($image,90,0);
            break;
        case 3:
            $image = imagerotate($image,180,0);
            break;
        case 6:
            $image = imagerotate($image,-90,0);
            break;
    }
}

Open in new window

Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

After move_uploaded_file() there is this line of code, that seems to tell the location of the permanently stored image file.
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];

Open in new window

In the "Orientation correction" code, try substituting that file location for the $_FILES['image_upload']['tmp_name'].
Avatar of augustkrys
augustkrys

ASKER

That doesn't seem to work  - Uploading fails completely after doing that
How experienced are you in PHP?  For example, have you written and debugged a file upload script before?  I'm trying to figure out the best way to advise you as we go forward with this process.
noob - I was trying to echo
else {
  echo "couldnt read data";

Open in new window

to see if the code even gets executed. Here is my entire file
<?php

$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
$newfilename = rand(1,99999) . '.' .end($temp);

if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& in_array($extension, $allowedExts)) {
  if ($_FILES["file"]["error"] > 0) {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
  } else {
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
    
    if (file_exists("upload/" . $_FILES["file"]["name"])) {
      echo $_FILES["file"]["name"] . " already exists. ";
    } else {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $newfilename);
      echo "Stored in: " . "upload/" . $newfilename;
    }
  }
} else {
  echo "Invalid file";
}

$image = imagecreatefromstring(file_get_contents($_FILES["file"]["name"]));
$exif = exif_read_data($_FILES["file"]["name"]);
if(!empty($exif['Orientation'])) {
    switch($exif['Orientation']) {
        case 8:
            $image = imagerotate($image,90,0);
            break;
        case 3:
            $image = imagerotate($image,180,0);
            break;
        case 6:
            $image = imagerotate($image,-90,0);
            break;
    }
}
 else {
  echo "could read data";
}
?> 

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This all is pretty clear- no developer needed -this is just practice not real / just projects for practice  and better understanding  of more complex - thanks you so much for the resource
Thanks for the points and best of luck with it!  ~Ray
THanks- Have it post up already for testing - But same issue as im having with the code written before - Image orientation is landscape instead of portrait- Thanks for the try anyway and code - there is many useful snippets in here i can definitely adapt