Search code examples
phpexifcapture

Orient image according to EXIF orientation before upload with PHP


I know that this topic is probably easy for most of you but i've been fighting over this for the last 2 days without any progress.

I'm doing a web app for myself, no security needed as it is non production intended.

Following scripts work fine, the problem remains when i upload picture directly from camera using:

<input
id="photoBox"
type="file"
class="form-control-file"
accept="image/*"
capture="camera"
name="photo"/>

When i upload from browser everything works fine, however from smartphone the server does not respect EXIF orientation therefore images are wrongly rotated.

As per upload i am using the following script: (pastebin provided as well).

https://pastebin.com/mvgah9Ud

function photoPlant($pID){
// db
include "includes/dbConfig.php";
// init
$out = null;
// gen hash
$varA = microtime();
$varB = time();
$varC = $varA . $varB;
$hash = md5($varC);
// prepare upload
$currentDir = getcwd();
$uploadDirectory = "/gallery/";
$errors = []; // Store all foreseen and unforseen errors here
$fileExtensions = ['jpeg','jpg','png', '']; // Get all the file 
extensions, including empty for mobile
// reformat empty file extension
if ($fileExtension === ""){
  $fileExtension = "jpg";
}
$fileName = $_FILES['photo']['name'];
$fileTmpName = $_FILES['photo']['tmp_name'];
$fileSize = $_FILES['photo']['size'];
$fileType = $_FILES['photo']['type'];
$fileExtension = strtolower(end(explode('.',$fileName)));
// reformat filename
$fileName = $hash . "." . $fileExtension;
$uploadPath = $currentDir . $uploadDirectory . basename($fileName);
if (! in_array($fileExtension,$fileExtensions)) {
    $errors[] = "This file extension is not allowed. Please upload a 
JPEG or PNG file";
}
if ($fileSize > 8000000) {
    $errors[] = "This file is more than 8MB. Sorry, it has to be less 
than or equal to 8MB";
}
if (empty($errors)) {
    $didUpload = move_uploaded_file($fileTmpName, $uploadPath);
    if ($didUpload) {
        $out .= "ok"; // everything is ok give feedback ok
    } else {
        $out .= "An error occurred somewhere. Try again or contact the 
admin";
    }
} else {
    foreach ($errors as $error) {
        $out .= $error . "These are the errors" . "\n";
    }
}
  // store img on db
  // prepare data
  $timeStamp = time();
  // query
  $query = mysqli_query($con, "INSERT INTO photo_table 
(photo_parent_id, photo_name, photo_timestamp) VALUES ($pID, 
'$fileName', $timeStamp)");
  // run query
  if (!$query){
    $out = mysqli_error($con);
  }
// return
return $out;
}

My intentions are clear. Rotate img BEFORE upload according to EXIF orientation and then proceed to store it on the disk. If possible i intend to do it in the very same photoPlant(arg) function.

Thanks.


Solution

  • Well. After a few unexplained downvotes and a very useful comment from DinoCoderSaurus this is the answer i was looking for.

    I had to install and enable Imagick for PHP7. It was not a plain simple job but there are a few guides available to google. Depending on your version / os installation notes are different so proceed with care.

    My upload function (from original post) changed on the upload part. Where it says:

    if (empty($errors)){
    // old code here.
    }
    

    It was changed for the following validation:

    if (empty($errors)) {
        // this is my new validation code.
        $img = new Imagick($fileTmpName);
        $orient = $img->getImageOrientation();
        if($orient === 6){
            // we need to rotate it 90deg
            $img->rotateImage("rgba(255, 255, 255, 0.0)", 90);
        }
        if ($orient === 3){
            // we need to rotate it 180deg
            $img->rotateImage("rgba(255, 255, 255, 0.0)", 180);
        }
        // Note that imagick does the storage for me as well!
        $img->writeImage("gallery/" . $fileName);
    }
    else{
        $out .= "Errors on upload";
    }
    

    This fixed ALL of my issues with a reasonably good response time. Hopefully some newbies like me will take some skill profit from this post.

    As a farewell note i need to add... If you downvote a post, comment the reason why you did that, because this topic was already discussed here countless times, but after 2 days researching on SO old posts i didn't manage to find WHY it was not working!

    A special thanks to DinoCoderSaurus who sent me in the right direction with around 10 words.