2017-02-03 30 views
1

我一直有一个非常困难的时间,试图通过PHP自动上传图片时,我上传它们。目前,它们在浏览器中查看时横向显示。PHP上传,带EXIF基于Orientation的旋转

我一直在寻找几个小时,并发现了很多提示和例子,但我不知道如何实现它们。

我也尝试使用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."; 
    } 
} 
?> 

任何帮助将非常感激!

+0

为什么要“去掉”你要求帮助的一段代码? – miken32

+0

我在代码上面发布的代码是我尝试实现的代码,但是我无法使其工作。我对“剥离”的用法可能不正确。 – Travis

+0

但是在那里没有与图像旋转相关的代码。你有两段代码,你说的是“把它们放在一起”,而不是“我试图把它们放在一起,但我得到了错误信息x。” – miken32

回答

0

试试这个。我们正在接收这个文件,如果它是一个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."; 
    } 
} 
+0

非常感谢。这很好用! – Travis

0

主要有两个问题: 1中的代码,你在网上找到 “$ _FILES [ 'image_upload'] [ 'tmp_name的值']” 必须用 “$ _FILES [” uploadReceipt “] [” 取代tmp_name“]”

  1. 如果文件被旋转,它必须被保存(如何保存它取决于文件类型)。

试试下面的代码,让我知道它是如何工作的?

// 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."; 
} 
}