1

We have a feature that lets users upload images on a website, which can be from a desktop or taken from any cellphone. But from some phones like Iphone images taken vertically end up rotated the wrong way. We need them to be displayed correctly on any device when uploaded from any device.

I found some similar questions but their solution didn't seem to work completely. I need to either rotate them correctly in php as soon as they are uploaded:

if(isset($_FILES["submit_image"])) {

$target_dir = "uploads/images/";
$name = $_FILES['submit_image']['name'];
$size = $_FILES['submit_image']['size'];
$tmp_name = $_FILES['submit_image']['tmp_name'];

//make sure the image is roated correctly? I tried this code that I found in other SO questions but then it doesn't seem to be saving it
// START ORIENTATION FIX
$info = getimagesize($tmp_name);
$image = imagecreatefrompng($tmp_name);

if($info['mime'] == 'image/jpeg') {
    $exif = exif_read_data($tmp_name);
    if(isset($exif['Orientation'])) {
        $orientation = $exif['Orientation'];
    }
}

if(isset($orientation)) { //rotate to match the orientation
    switch($orientation) {
        case 3:
            $image = imagerotate($image, 180, 0);
            break;
        case 6:
            $image = imagerotate($image, -90, 0);
            break;
        case 8:
            $image = imagerotate($image, 90, 0);
            break;
    }
}
if(isset($image)) { //get the variables of the rotated image
    $tmp_name = $image["tmp_name"];
    $name = $image['name'];
    $size = $image['size'];
}
// END ORIENTATION FIX

/*doing some other checks here for image name, size, and mime type
    ...
*/

$path = calculate_file_path($target_dir, $name);
if (move_uploaded_file($tmp_name, $path) == true) {
    $query = "UPDATE {$table} SET {$column} = '{$path}' WHERE id = {$new_pro_id}";
    $result = mysqli_query($db, $query);
    if ($result) {
        //saved
    }
    else{
        //failed
    }
}
}

Or maybe fix them later with css whenever the site needs to display them:

.profile_img
{
    image-orientation: from-image;
}

But neither seemed to work. the php option broke them completely, and the css option didn't fix the orientation.

EDIT I get a warning/error message: exif_read_data() expects parameter 1 to be a valid path, array given in...

But there is no file path anywhere at that point. All I have is $_FILES['submit_image'] which comes from a type="file" field in an html form. I'm sure this variable has all the information which would be in the file path because I use it later to save to the server.

Any ideas? Thanks :)

ioan
  • 295
  • 2
  • 7
  • 23
  • What is the input that you have, what is the output you are getting and what is the desired output? – Robert Sep 18 '17 at 01:09
  • The input would be an image file, usually a photo from an iphone. The output is the image rotated left by 90 degrees but we want it to be rotated the same way it was taken. – ioan Sep 18 '17 at 13:36
  • https://stackoverflow.com/questions/22308921/fix-ios-picture-orientation-after-upload-php – Robert Sep 18 '17 at 16:46
  • The first response there doesn't even seem to be php? and the second is kinda what I was trying and couldn't get to work. I think i found one of the problems so I'll edit the original post – ioan Sep 18 '17 at 19:03

0 Answers0