【发布时间】:2019-01-07 12:33:52
【问题描述】:
我知道这个话题对你们大多数人来说可能很容易,但我在过去 2 天里一直在争论这个话题,但没有任何进展。
我正在为自己做一个网络应用程序,不需要安全性,因为它不是生产目的。
以下脚本工作正常,当我直接从相机上传图片时问题仍然存在:
<input
id="photoBox"
type="file"
class="form-control-file"
accept="image/*"
capture="camera"
name="photo"/>
当我从浏览器上传时一切正常,但是从智能手机服务器不遵守 EXIF 方向,因此图像被错误地旋转。
根据上传,我使用以下脚本:(也提供了 pastebin)。
function photoPlant($pID){
// db
include "includes/dbConfig.php";
// init
$out = null;
// gen hash
$varA = microtime();
$varB = time();
$varC = $varA . $varB;
$hash = md5($varC);
// prepare upload
$currentDir = getcwd();
$uploadDirectory = "/gallery/";
$errors = []; // Store all foreseen and unforseen errors here
$fileExtensions = ['jpeg','jpg','png', '']; // Get all the file
extensions, including empty for mobile
// reformat empty file extension
if ($fileExtension === ""){
$fileExtension = "jpg";
}
$fileName = $_FILES['photo']['name'];
$fileTmpName = $_FILES['photo']['tmp_name'];
$fileSize = $_FILES['photo']['size'];
$fileType = $_FILES['photo']['type'];
$fileExtension = strtolower(end(explode('.',$fileName)));
// reformat filename
$fileName = $hash . "." . $fileExtension;
$uploadPath = $currentDir . $uploadDirectory . basename($fileName);
if (! in_array($fileExtension,$fileExtensions)) {
$errors[] = "This file extension is not allowed. Please upload a
JPEG or PNG file";
}
if ($fileSize > 8000000) {
$errors[] = "This file is more than 8MB. Sorry, it has to be less
than or equal to 8MB";
}
if (empty($errors)) {
$didUpload = move_uploaded_file($fileTmpName, $uploadPath);
if ($didUpload) {
$out .= "ok"; // everything is ok give feedback ok
} else {
$out .= "An error occurred somewhere. Try again or contact the
admin";
}
} else {
foreach ($errors as $error) {
$out .= $error . "These are the errors" . "\n";
}
}
// store img on db
// prepare data
$timeStamp = time();
// query
$query = mysqli_query($con, "INSERT INTO photo_table
(photo_parent_id, photo_name, photo_timestamp) VALUES ($pID,
'$fileName', $timeStamp)");
// run query
if (!$query){
$out = mysqli_error($con);
}
// return
return $out;
}
我的意图很明确。根据 EXIF 方向在上传之前旋转 img,然后继续将其存储在磁盘上。
如果可能的话,我打算在相同的 photoPlant(arg) 函数中执行此操作。
谢谢。
【问题讨论】:
-
这里有一些文档可以帮助您入门
exif_read_data和 GD and Image Functions -
@DinoCoderSaurus 我不确定为什么这个问题在社区中如此受人憎恨。看来我的反对票比答案多,但是我解决了这个问题的唯一答案。我真的不知道在哪里看,所以非常感谢你。请作为最终答案提交,我很乐意将其标记为正确,因为它是正确的!