通过旋转问题从智能手机上传照片?

I tried the "Update Code" on this Question and the uploaded image does rotate but the destination image created by PHP's GD stays landscape and not portrait. So I end up with a rotated portrait image cut off at the bottom within a landscape rectangle that has a black background on the right side. I have studied the code and can't figure out why. It appears that the destination image created by GD is still pulling from original uploaded image for its orientation. Not sure.

if (isset($_FILES['image'])) {

if (empty($_FILES['image']['name'])) {

        ?><div class="add-errors">Please choose an image!</div><?php 

}   else {


    function getOrientedImage($imagePath){
        $image = imagecreatefromstring(file_get_contents($imagePath));
        $exif = exif_read_data($imagePath);
        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;
            }
        }
        return $image;
    }

    $name       =   $_FILES['image']['name'];
    $temp       =   $_FILES['image']['tmp_name'];
    $type       =   $_FILES['image']['type'];
    $size       =   $_FILES['image']['size'];
    $ext        =   strtolower(end(explode('.', $name)));
    $size2      =   getimagesize($temp);
    $width      =   $size2[0];
    $height     =   $size2[1];
    $upload     =   md5( rand( 0, 1000 ) . rand( 0, 1000 ) . rand( 0, 1000 ) . rand( 0, 1000 ));

    // Restrictions for uploading
    $maxwidth   =   6000;
    $maxheight  =   6000;
    $allowed    =   array('image/jpeg', 'image/jpg', 'image/png', 'image/gif');

    // Recognizing the extension
    switch($type){

        // Image/Jpeg
        case 'image/jpeg':
                $ext= '.jpeg';
        break;

        // Image/Jpg
        case 'image/jpg':
                $ext= '.jpg';
        break;

        // Image/png
        case 'image/png':
                $ext= '.png';
        break;

        // Image/gif
        case 'image/gif':
                $ext= '.gif';
        break;
    }

    // upload variables
    $path           =   $userDir . $upload . $ext;
    $thumb_path     =   $userDir . 'thumb_' . $upload . $ext;

    // check if extension is allowed.
    if (in_array($type, $allowed)) {

        // Checking if the resolution is FULLHD or under this resolution.
        if ($width <= $maxwidth && $height <= $maxheight) {
            if ($size <= 5242880) {

                // check the shape of the image
                if ($width == $height) {$shape = 1;}
                if ($width > $height) {$shape = 2;}
                if ($width < $height) {$shape = 2;}

                //Adjusting the resize script on shape.
                switch($shape) {

                    // Code to resize a square image.
                    case 1:
                        $newwidth =     690;
                        $newheight =    690;
                    break;

                    // Code to resize a tall image
                    case 2:
                        $newwidth   =   690;
                        $ratio      =   $newwidth / $width;
                        $newheight  =   round($height * $ratio);

                    break;

                }

                // Resizing according to extension.
                switch ($type) {

                    // Image/Jpeg   
                    case 'image/jpeg';
                        $img =      getOrientedImage($temp);
                        $thumb =    imagecreatetruecolor($newwidth, $newheight);
                                    imagecopyresized($thumb, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
                                    imagejpeg($thumb, $thumb_path);
                    break;

                    // Image/Jpg    
                    case 'image/jpg';
                        $img =      getOrientedImage($temp);
                        $thumb =    imagecreatetruecolor($newwidth, $newheight);
                                    imagecopyresized($thumb, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
                                    imagejpeg($thumb, $thumb_path);
                    break;

                    // Image/png    
                    case 'image/png';
                        $img =      getOrientedImage($temp);
                        $thumb =    imagecreatetruecolor($newwidth, $newheight);
                                    imagecopyresized($thumb, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
                                    imagepng($thumb, $thumb_path);
                    break;

                    // Image/gif    
                    case 'image/gif';
                        $img =      getOrientedImage($temp);
                        $thumb =    imagecreatetruecolor($newwidth, $newheight);
                                    imagecopyresized($thumb, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
                                    imagegif($thumb, $thumb_path);
                    break;
                }


                    // Move the original file aswell.
                    move_uploaded_file($temp, $path);


            } else {
                ?><div class="add-errors">Your image size is too big!</div><?php
            }
        } else {
            ?><div class="add-errors">Your image resolution exceeds the limit!</div><?php
        }

    } else {
        ?><div class="add-errors">Your have uploaded a forbidden extension!</div><?php

    }

}

}

Check if adding this line works for you:

$height     =   $size2[1];
$upload     =   md5( rand( 0, 1000 ) . rand( 0, 1000 ) . rand( 0, 1000 ) . rand( 0, 1000 ));

//Add this lines to create a swap
$exif = exif_read_data($temp);
if (isset($exif['Orientation']) && in_array($exif['Orientation'], Array(8, 6)))
   { list($width, $height) = array($height, $width); }


// Restrictions for uploading
$maxwidth   =   6000;
$maxheight  =   6000;
$allowed    =   array('image/jpeg', 'image/jpg', 'image/png', 'image/gif');

They basically just swap out height and width of the image in case of it being rotated. Please note that you should really consider refactoring the solution into something you can properly read and that doesn't force the application to read the exif twice.