PHP上传与基于EXIF方向的轮换

时间:2017-02-03 22:17:57

标签: php exif

当我上传图像时,我一直很难通过PHP自动旋转图像。目前,它们在浏览器中查看时会侧向显示。

我已经搜索了几个小时,并找到了很多提示和示例,但我不太清楚如何实现它们。

我也试过在PHP手册上使用评论者的代码,没有运气。

这是我引用的代码:

        <?php
    $image = imagecreatefromstring(file_get_contents($_FILES['image_upload']['tmp_name']));
    $exif = exif_read_data($_FILES['image_upload']['tmp_name']);
    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;
        }
    }
    // $image now contains a resource with the image oriented correctly
    ?>

这是我正在使用的页面。它似乎功能正常,但图像横向出现。我从众多失败的尝试中删除了代码,以使轮换工作。

<?php
include 'includes/democonnect.php';
$cnum=$_POST['cnum'];
$amount1=$_POST['amount1'];



$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["uploadReceipt"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["uploadReceipt"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}



$filename = 'receipt'.time() . basename($_FILES["uploadReceipt"]["name"]);




 // Check file size
if ($_FILES["uploadReceipt"]["size"] > 5000000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif"  && $imageFileType != "bmp" ) {
    echo "Sorry, only JPG, JPEG, PNG, GIF, and BMP files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["uploadReceipt"]["tmp_name"], $target_dir.$filename)) {
        echo "The file ". $filename. " has been uploaded.";

$query = "INSERT INTO tblReceiptUpload
        (cnum,pointer1,amount1)
        VALUES(?,?,?)";
$params1 = array($cnum,$filename,$amount1);                       
$result = sqlsrv_query($conn,$query,$params1);

sqlsrv_close($conn);        

    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}
?>

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:3)

试试这个。我们正在接收该文件,如果它是jpeg,那么我们将其旋转。如果没有,我们不会。我们采用创建的$image变量,并在您想要的位置生成一个jpeg。

include 'includes/democonnect.php';
$cnum=$_POST['cnum'];
$amount1=$_POST['amount1'];

$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["uploadReceipt"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["uploadReceipt"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;

        $info = $check;
        if ($info['mime'] == 'image/jpeg') $image = imagecreatefromjpeg($_FILES["uploadReceipt"]["tmp_name"]);
        elseif ($info['mime'] == 'image/gif') $image = imagecreatefromgif($_FILES["uploadReceipt"]["tmp_name"]);
        elseif ($info['mime'] == 'image/png') $image = imagecreatefrompng($_FILES["uploadReceipt"]["tmp_name"]);
        else exit;//Do whatever you want here.

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

        if(isset($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;
            }
        }

        //////////
        // $image is your new, rotated file.
        //////////
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}

$filename = 'receipt'.time() . basename($_FILES["uploadReceipt"]["name"]);

 // Check file size
if ($_FILES["uploadReceipt"]["size"] > 5000000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif"  && $imageFileType != "bmp" ) {
    echo "Sorry, only JPG, JPEG, PNG, GIF, and BMP files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0 || !isset($image)) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (imagejpeg($image, $target_dir.$filename)) {
        echo "The file ". $filename. " has been uploaded.";

        $query = "INSERT INTO tblReceiptUpload
        (cnum,pointer1,amount1)
        VALUES(?,?,?)";
        $params1 = array($cnum,$filename,$amount1);
        $result = sqlsrv_query($conn,$query,$params1);

        sqlsrv_close($conn);

    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}

答案 1 :(得分:0)

有两个主要问题: 1.在您在线找到的代码中&#34; $ _ FILES [&#39; image_upload&#39;] [&#39; tmp_name&#39;]&#34;必须替换为&#34; $ _ FILES [&#34; uploadReceipt&#34;] [&#34; tmp_name&#34;]&#34;

  1. 如果文件被旋转,则必须保存(如何保存它会因文件类型而异)。
  2. 请尝试下面的代码,让我知道它是如何工作的?

    // if everything is ok, try to upload file
    } else {
    
    $image = imagecreatefromstring(file_get_contents($_FILES['uploadReceipt']['tmp_name']));
    $exif = exif_read_data($_FILES['uploadReceipt']['tmp_name']);
    $was_rotated = 0;
    if(!empty($exif['Orientation'])) {
        switch($exif['Orientation']) {
            case 8:
                $image = imagerotate($image,90,0); $was_rotated = 1;
                break;
            case 3:
                $image = imagerotate($image,180,0); $was_rotated = 1;
                break;
            case 6:
                $image = imagerotate($image,-90,0); $was_rotated = 1;
                break;
        }
    }
    
    if($was_rotated == 1)
        {
        switch($imageFileType)  // making the assumption that the image file has the correct exstention!
            {
            case 'bmp':
                if(function_exists(imagebmp)) { imagebmp($image,$target_dir.$filename); }  // PHP 7 required for imagebmp 
                else { $was_rotated = 0; }
            break;
    
            case 'png':
                imagepng($image,$target_dir.$filename);
            break;
    
            case 'gif':
                imagegif($image,$target_dir.$filename);
            break;
    
            case 'jpg':
                imagejpeg($image,$target_dir.$filename,92);  // 92 is jpeg quality setting
            break;
    
            case 'jpeg':
                imagejpeg($image,$target_dir.$filename,92);  
            break;
            }
        }   
    
    if(($was_rotated == 1) or (move_uploaded_file($_FILES["uploadReceipt"]["tmp_name"], $target_dir.$filename))) {
        echo "The file ". $filename. " has been uploaded.";
    
    $query = "INSERT INTO tblReceiptUpload
         (cnum,pointer1,amount1)
        VALUES(?,?,?)";
    $params1 = array($cnum,$filename,$amount1);                       
    $result = sqlsrv_query($conn,$query,$params1);
    
    sqlsrv_close($conn);        
    
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
    }