PHP 上传并基于 EXIF 方向旋转

标签 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.";
    }
}
?>

任何帮助将不胜感激!

最佳答案

试试这个。我们正在获取文件,如果它是 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.";
    }
}

关于PHP 上传并基于 EXIF 方向旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42033887/

相关文章:

jquery - 使用来自 AJAX 调用的图像 URL 获取数据?

php - PHP中的PDO中的PDO问题

php - drupal--为什么 $node->taxonomy 是一个数组

php - Container.php类镜像不存在的ReflectionException进行干预

android - 从图像中删除或更新 exif 缩略图

c++ - 是否有任何示例代码可以从 Jpeg exif header 中读取缩略图?

swift UIImagePickerController 并从现有照片中提取 EXIF 数据

php - 带有准备语句的 PostgreSQL 内部查询

php - Cakephp如何删除链接上的下划线

php - 如何使用PHP修改图像的exif数据