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

在PHP中调整上传图像的大小时,为什么我的图像会旋转?,php,file,upload,rotation,Php,File,Upload,Rotation,可能重复: 我已经创建了我的第一个上传代码,并正在测试它,图像的大小和上传很好。我唯一的问题是它会旋转 代码如下: $errors = array(); $message = ""; if(isset($_POST["test"])){ $name = rand(1,999999) . $_FILES["uploadfile"]["name"]; $temp_name = $_FILES["uploadfile"]["tmp_name"]; $

可能重复:

我已经创建了我的第一个上传代码,并正在测试它,图像的大小和上传很好。我唯一的问题是它会旋转

代码如下:

$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;
$errors=array();
$message=“”;
如果(isset($_POST[“test”])){
$name=rand(1999999)。$_文件[“uploadfile”][“name”];
$temp_name=$_FILES[“uploadfile”][“tmp_name”];
$size=$\u文件[“上载文件”][“大小”];
$extension=strtolower(end(explode('..,$文件['uploadfile']['name']));
$path=“testupload/”$name;
$info=getimagesize($temp\u 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');
回声$大小;
//检查mimetype
如果(!in_array($mime,$acceptedMimes)){$errors[]=“不允许mime类型-您刚才上载的文件是:“.$extension.”file!”;}
if(!$errors){if($size>$acceptedfileSize){$errors[]=“文件大小太大-此文件的文件大小为:“.$size;}”
if(!$errors){if(!in_数组($extension,$acceptedExtensions)){$errors[]=“不允许文件扩展名-您刚才上载的文件是:“.$extension.”文件!;}”
如果(!$errors){
//从临时文件创建映像。
如果($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\U名称);
}
//计算出新的维度。
如果($acceptedHeight==0){
$newWidth=$acceptedWidth;
$newHeight=($originalheight/$originalwidth)*$acceptedWidth;
}else if($acceptedWidth==0){
$newWidth=($originalwidth/$originalheight)*$acceptedHeight;
$newHeight=$acceptedHeight;
}否则{
$newWidth=$acceptedWidth;
$newHeight=$acceptedHeight;
}
$originalwidth=imagesx($orig);
$originalheight=imagesy($orig);
//确保它们是有效的。
if($newWidth<1){$newWidth=1;}else{$newWidth=round($newWidth);}
if($newHeight<1){$newHeight=1;}else{$newHeight=round($newHeight);}
//如果图像大小正确,就不要复制它。
如果($originalwidth!=$newWidth | |$originalheight!=$newHeight){
//创建一个新图像,并将原始图像以新的大小复制到该图像上。
$new=ImageCreateTureColor($newWidth,$newHeight);
imagecopyresampled($new,$orig,0,0,0,$newWidth,$newHeight,$originalwidth,$originalheight);
}否则{
//phps写时拷贝意味着这不会造成任何伤害。
$new=$orig;
}
//保存图像。
如果($extension=='jpeg'| |$extension==='jpg'){
imagejpeg($new$path,100);
}else if($save_ext=='gif'){
imagegif($new$path);
}否则{
imagepng($new,$path,round)(9-(100/(100/9));
}
$message=$path;

有人能告诉我发生了什么吗?

检查原始图像是否与您期望的方向一致。我整天都在处理图像,大部分时间是Windows照片查看器以特定方向显示图像(读取EXIF中的方向更改)但是如果你在Photoshop中打开它,它就不同了。

我用两个图像测试了你的代码:一个是横向(宽度>高度),另一个是纵向(高度>宽度)。代码工作正常

问题似乎在于@Tavocado所说的:较新的相机有一个传感器,可以在拍摄照片时检测相机的方向,并将该信息存储在照片中。此外,较新的照片查看软件会从照片中读取该信息,并在显示图像之前旋转该信息。因此,您始终可以看到具有正确方向的图像但PHP函数(以及其他函数)不使用这些信息,而是按照拍摄的图像显示。这意味着您必须旋转自己的肖像图像


只需在浏览器中加载图像(拖动地址栏上的文件),您就可以看到图像是如何真正存储在文件中的,而无需进行任何自动旋转。

问题在于图像中嵌入了数据,可能来自拍摄照片的设备


调查您的图像是否嵌入了旋转信息,使用,然后使用自动更正旋转。

上传的图像最初是以另一种方式上传的,我现在测试了一些图像,所有高度大于宽度的图像都会旋转。如果所有高度大于宽度的图像都会旋转,它会旋转图像,如何旋转我能阻止这种情况发生吗?我在代码中看不到任何明显的东西。在每一步
var\u dump(getimagesize($new\u image))
之后,我都会计算出它在哪一点发生了变化