Ben
Ben

Reputation: 89

How to automatically rotate images with exif orientation data with php

I've created an upload script which also creates a thumbnail which works fine, but I've now encountered a problem. When I upload some images the exif orientation data automatically rotates the image on my website.

I've tried searching online on how to check for the data and rotate it back if there is any but I can't understand what I'm meant to do.

Here is my code so far:

if(isset($_POST['submit'])) {
        $allowedExts = array("jpg", "jpeg", "gif", "png", "JPG", "GIF");
        $extension = end(explode(".", strtolower($_FILES["file"]["name"])));
        if ((($_FILES["file"]["type"] == "image/gif")
        || ($_FILES["file"]["type"] == "image/jpeg")
        || ($_FILES["file"]["type"] == "image/png")
        || ($_FILES["file"]["type"] == "image/pjpeg"))
        && in_array($extension, $allowedExts)) {
            if ($_FILES["file"]["error"] > 0) {
                echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
            } else {

                $random_numer = rand(10000000, 99999999);
                $newname1 = $random_numer . $_FILES["file"]["name"];
                $scramble = md5($newname1);
                $extension = end(explode(".", $_FILES["file"]["name"]));
                $final_name = $scramble . "." . $extension;

                move_uploaded_file($_FILES["file"]["tmp_name"], "../img/gallery/" . $final_name);
                create_thumbnail($final_name);
                $stmt = $conn->prepare('INSERT INTO gallery (image) VALUES (:image)');
                $stmt->bindParam(':image', $final_name);
                $stmt->execute();

                if ($stmt->rowCount() > 0) {
                    echo "<p class='success'>The Image has been added!
                     <a href='gallery.php'>Please click here to refresh</a></p>";
                } else {
                    echo "<p class='error'>The Image was not added, there was an error.</p>";
                }

            }
        } else {
            echo "Invalid file";
        }
    }

How can I make the images not rotate?

Upvotes: 3

Views: 7591

Answers (1)

ingernet
ingernet

Reputation: 1524

You'll have to write another custom function, as there's no way to tell move_uploaded_file to ignore EXIF data, which is kind of a bummer.

Upvotes: 5

Related Questions