我一直有一个非常困难的时间,试图通过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.";
}
}
?>
任何帮助将非常感激!
为什么要“去掉”你要求帮助的一段代码? – miken32
我在代码上面发布的代码是我尝试实现的代码,但是我无法使其工作。我对“剥离”的用法可能不正确。 – Travis
但是在那里没有与图像旋转相关的代码。你有两段代码,你说的是“把它们放在一起”,而不是“我试图把它们放在一起,但我得到了错误信息x。” – miken32