如何保持使用jQuery或PHP保存到服务器的图像的相同图像方向哪个合适?

时间:2014-12-08 07:44:02

标签: php jquery orientation screen-orientation exif

我在我的网站上使用 PHP 5.5.18,jQuery,AJAX,HTML5,Bootstrap框架(v.3.3.0)

我的网站主要用于智能手机'和智能设备'用户。所以它应该可以在移动浏览器以及PC /笔记本电脑浏览器上正常工作。

在我的一种表格中,我为用户提供了一种设施,可以从设备相机中捕获图像,或者在设备的图像库中选择现有图像。在此之后,相同的图像应该上传到服务器。

现在一切都运转得非常好和完美。持续存在的唯一问题是图像方向的变化。

让我关注我所面临的问题。如果用户使用设备相机在"横向方向"同一图像的方向改为"肖像"如果我看一下保存在服务器上的图像。如果用户在"纵向方向"中拍摄照片,则会发生同样的情况,它会变为"风景"在服务器映像上。

我搜索了这个解决方案,然后我开始了解" Imagick PHP扩展" 。但由于此扩展程序默认情况下未激活,因此无法在服务器上启用此扩展程序,因为我没有权限执行此操作。所以我要求使用jQuery或PHP的其他解决方案,无论哪种情况都有益。

以下是我的代码供您参考:

HTML代码:

<form id="request_form" method="post" class="form-horizontal" enctype="multipart/form-data" action="">
  <input type="file" name="student_image" id="student_image" accept="image/*" capture/>
</form>

jQuery-AJAX代码:

$('#request_form').submit(function(e) {

  var form = $(this);

  var formdata = false;

  if(window.FormData) {
    formdata = new FormData(form[0]);
  }

  var formAction = form.attr('action');

  $.ajax({
    url         : 'request.php',
    type        : 'POST',    
    cache       : false,
    data        : formdata ? formdata : form.serialize(),
    contentType : false,
    processData : false,
    success: function(response) {

      var responseObject = $.parseJSON(response);    
      if(responseObject.error_message) {

        if ($(".alert-dismissible")[0]) {
          $('.alert-dismissible').remove();   
        }  
        alert(responseObject.error_message);    

      } else {
        alert("Success");       
      }
    }
  });
  e.preventDefault();
});

PHP代码:

<?php
  $target_dir = "uploads/";
  $target_file = $target_dir . basename($_FILES["student_image"]["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["student_image"]["tmp_name"]);
    if($check !== false) {
      $error_msg = "File is an image - " . $check["mime"] . ".";
      $uploadOk = 1;

    } else {
      $error_msg = "File is not an image.";
      $uploadOk = 0;      
    }
  }

  // Allow certain file formats
  if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
    $error_msg = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
  }
  // Check if $uploadOk is set to 0 by an error
  if ($uploadOk == 0) {
    $error_msg = "Sorry, your file was not uploaded.";
  // if everything is ok, try to upload file
  } else {
    if (move_uploaded_file($_FILES["student_image"]["tmp_name"], $target_file)) {
      $success = "The file ". basename( $_FILES["student_image"]["name"]). " has been uploaded.";
    } else {
      $error_msg = "Sorry, there was an error uploading your file.";
    }
  }

  if($error_msg != ''){
    $data = array();
    $data['error_message'] = $error_msg;
    $data = json_encode($data);     
    echo $data;
    die;
  } else {
    $data = array();
    $data['success_message'] = $success;
    $data = json_encode($data);
    echo $data;
    die;   
  }
?>

我有以下功能可以为我完成这项工作,但我不了解如何调用此功能以及如何从中获取要保存到服务器的图像。请帮帮我。

<?php
function image_fix_orientation(&$image, $filename) {
    $exif = exif_read_data($filename);

    if (!empty($exif['Orientation'])) {
        switch ($exif['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;
        }
    }
}
?>

如果您需要有关我面临的问题的任何进一步信息,请告诉我。

感谢。

1 个答案:

答案 0 :(得分:0)

就个人而言,我不会费心直接使用PHP的图像功能,而是依赖现有的(抽象)库。

想象一下,已经提供了三个驱动程序:GD,Imagick和Gmagick。 https://github.com/avalanche123/Imagine

在项目中包含库后,使用它来保存从相机正确旋转的图像。它将需要安装exif扩展。

    $imagefile = $_FILES["..."]["tmp_name"];
    if(class_exists('Gmagick'))
    {
        $imagine = new \Imagine\Gmagick\Imagine();
    }
    elseif(class_exists('Imagick'))
    {
        $imagine = new \Imagine\Imagick\Imagine();
    }
    else
    {
        $imagine = new \Imagine\Gd\Imagine();
    }

    $image = $imagine->open($imagefile);

    $filterAutorotate = new \Imagine\Filter\Basic\Autorotate();
    $filterAutorotate->apply($image);

    $filename = 'brand new .jpg';
    $image->save($filename/*, array() $options/*);

阅读手动执行此操作的元数据,请参阅此处https://github.com/avalanche123/Imagine/blob/develop/lib/Imagine/Image/Metadata/ExifMetadataReader.php

相关问题