为什么在PHP中调整上传图像大小时我的图像会旋转? [重复]

Possible Duplicate:
php resizing image on upload rotates the image when i don't want it to

I have created my first ever upload code and was testing it, the image resizes and uploads fine. The only issue I have is that it rotates.

Here is the code:

$errors = array();
$message = "";

if(isset($_POST["test"])){

$name               = rand(1,999999) . $_FILES["uploadfile"]["name"];
$temp_name          = $_FILES["uploadfile"]["tmp_name"];
$size               = $_FILES["uploadfile"]["size"];
$extension          = strtolower(end(explode('.', $_FILES['uploadfile']['name'])));
$path               =   "testupload/" . $name;

$info               = getimagesize($temp_name);
$originalwidth      = $info[0];
$originalheight     = $info[1];
$mime               = $info["mime"];

$acceptedHeight     = 750;
$acceptedWidth      = 0;

$acceptedMimes = array('image/jpeg','image/png','image/gif');
$acceptedfileSize = 4102314;
$acceptedExtensions = array('jpg','jpeg','gif','png');
echo $size;
// check mimetype
if(!in_array($mime, $acceptedMimes)){$errors[] = "mime type not allowed - The file you have just uploaded was a: " . $extension . " file!";}
if(!$errors){if($size > $acceptedfileSize){$errors[] = "filesize is to big - Your file size of this file is: " . $size;}}
if(!$errors){if(!in_array($extension, $acceptedExtensions)){$errors[] = "File extension not allowed - The file you have just uploaded was a: " . $extension . " file!";}}

if(!$errors){

    // create the image from the temp file.
    if ($extension === 'png'){
        $orig = imagecreatefrompng($temp_name);
    }elseif ($extension === 'jpeg'){
        $orig = imagecreatefromjpeg($temp_name);
    }elseif ($extension === 'jpg'){
        $orig = imagecreatefromjpeg($temp_name);
    }elseif ($extension === 'gif'){
        $orig = imagecreatefromgif($temp_name);
    }

    // work out the new dimensions.
    if ($acceptedHeight === 0){
        $newWidth = $acceptedWidth;
        $newHeight = ($originalheight / $originalwidth) * $acceptedWidth;
    }else if ($acceptedWidth === 0){
        $newWidth = ($originalwidth / $originalheight) * $acceptedHeight;
        $newHeight = $acceptedHeight;
    }else{
        $newWidth = $acceptedWidth;
        $newHeight = $acceptedHeight;
    }

    $originalwidth = imagesx($orig);
    $originalheight = imagesy($orig);


    // make ssure they are valid.
    if ($newWidth  < 1){ $newWidth  = 1; }else{ $newWidth  = round($newWidth ); }
    if ($newHeight < 1){ $newHeight = 1; }else{ $newHeight = round($newHeight); }

    // don't bother copying the image if its alreay the right size.
    if ($originalwidth!== $newWidth || $originalheight !== $newHeight){

        // create a new image and copy the origional on to it at the new size.
        $new = imagecreatetruecolor($newWidth, $newHeight);
        imagecopyresampled($new, $orig, 0,0,0,0, $newWidth, $newHeight, $originalwidth, $originalheight);

    }else{

        // phps copy on write means this won't cause any harm.
        $new = $orig;
    }

    // save the image.
    if ($extension === 'jpeg' || $extension === 'jpg'){
        imagejpeg($new, $path, 100);
    }else if ($save_ext === 'gif'){
        imagegif($new, $path);
    }else{
        imagepng($new, $path, round(9 - (100 / (100 / 9))));
    }

    $message = $path;

Can someone please let me know what is going on?

Check the original image is actually in the orientation you expect. I work with images all day and the majority of the time it's Windows Photo Viewer showing the image in a certain orientation (reading an orientation change in the EXIF) but if you open it up in Photoshop it's different.

I tested your code with two images: one was landscape (width > height), the other was portrait (height > width). The code works.

The problem seems to be what @Tavocado said: newer cameras have a sensor to detect the orientation of the camera when the photo was taken, and they store that info in the photo. Also, newer photo viewing software reads that info back from the photo and rotates it before displaying the image. So you all the time see the image with the right orientation (sky up, earth down). However PHP functions (and the rest of the world) don't use that information and display the image as is was taken. That means that you will have to rotate yourself portrait images.

Just load your images in the browser (drag the file on the address bar) you you will see how the image is really stored in the file, without any automatic rotation.

The problem is that the image has embedded EXIF data, probably from the device that took the photo.

Investigate whether your images have embedded rotation information, using exif_read_data, and then auto-correct the rotation with a function like this.