【问题标题】:Orient image according to EXIF orientation before upload with PHP在使用 PHP 上传之前根据 EXIF 方向来定位图像
【发布时间】:2019-01-07 12:33:52
【问题描述】:

我知道这个话题对你们大多数人来说可能很容易,但我在过去 2 天里一直在争论这个话题,但没有任何进展。

我正在为自己做一个网络应用程序,不需要安全性,因为它不是生产目的。

以下脚本工作正常,当我直接从相机上传图片时问题仍然存在:

<input
id="photoBox"
type="file"
class="form-control-file"
accept="image/*"
capture="camera"
name="photo"/>

当我从浏览器上传时一切正常,但是从智能手机服务器不遵守 EXIF 方向,因此图像被错误地旋转。

根据上传,我使用以下脚本:(也提供了 pastebin)。

https://pastebin.com/mvgah9Ud

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 我不确定为什么这个问题在社区中如此受人憎恨。看来我的反对票比答案多,但是我解决了这个问题的唯一答案。我真的不知道在哪里看,所以非常感谢你。请作为最终答案提交,我很乐意将其标记为正确,因为它是正确的!

标签: php exif capture


【解决方案1】:

嗯。经过一些无法解释的反对意见和来自 DinoCoderSaurus 的非常有用的评论,这就是我一直在寻找的答案。

我必须安装并启用 Imagick for PHP7。 这不是一项简单的工作,但谷歌提供了一些指南。根据您的版本/操作系统,安装说明会不同,因此请谨慎操作。

我的上传功能(来自原始帖子)在上传部分发生了变化。 它在哪里说:

if (empty($errors)){
// old code here.
}

为以下验证而更改:

if (empty($errors)) {
    // this is my new validation code.
    $img = new Imagick($fileTmpName);
    $orient = $img->getImageOrientation();
    if($orient === 6){
        // we need to rotate it 90deg
        $img->rotateImage("rgba(255, 255, 255, 0.0)", 90);
    }
    if ($orient === 3){
        // we need to rotate it 180deg
        $img->rotateImage("rgba(255, 255, 255, 0.0)", 180);
    }
    // Note that imagick does the storage for me as well!
    $img->writeImage("gallery/" . $fileName);
}
else{
    $out .= "Errors on upload";
}

这以相当好的响应时间解决了我的所有问题。 希望像我这样的新手能从这篇文章中获得一些技能收益。

作为告别说明,我需要添加...无法找到为什么它不起作用!

特别感谢 DinoCoderSaurus,他用大约 10 个单词将我引向了正确的方向。

【讨论】:

    猜你喜欢
    • 2014-11-05
    • 2018-12-26
    • 2016-05-27
    • 1970-01-01
    • 2020-07-22
    • 2020-05-26
    • 1970-01-01
    • 2018-12-26
    • 1970-01-01
    相关资源
    最近更新 更多