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

3
可能重复:
上传时 PHP 调整图像大小会旋转我不想要的图像 我创建了我的第一个上传代码并进行了测试,图像的大小调整和上传都很好。唯一的问题是它会旋转。
以下是代码:
$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;

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


所有高度大于宽度的图像似乎都会被旋转,我该如何防止这种情况发生? - Robert
我在你的代码中没有看到任何明显的问题。在每个步骤之后,使用var_dump(getimagesize($new_image))来确定它在哪个点上发生了变化。 - amctavish
3个回答

1

检查原始图像是否实际上处于您期望的方向。我整天都在处理图像,大多数情况下是Windows照片查看器以某种方向显示图像(读取EXIF中的方向更改),但如果您在Photoshop中打开它,则会有所不同。


上传的图片原本是倒过来的,我现在测试了几张图片,所有高度大于宽度的图片都会旋转。 - Robert

0
问题在于图像嵌入了 EXIF 数据,可能来自拍摄照片的设备。
使用 exif_read_data 来调查您的图像是否有嵌入的旋转信息,然后使用类似这样的 函数 自动校正旋转。

0

我使用了两张图片测试了你的代码:一张是横向的(宽度>高度),另一张是纵向的(高度>宽度)。代码可以正常运行。

问题似乎就像@Tavocado所说的那样:新相机有一个传感器来检测拍摄照片时相机的方向,并将该信息存储在照片中。此外,新的照片查看软件会从照片中读取该信息并在显示图像之前旋转它。因此,您始终会以正确的方向(天空朝上,地面朝下)查看图像。但是PHP函数(和其他所有人)不使用该信息并按原样显示图像。这意味着您必须自己旋转纵向图像。

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接