如何使用jQuery或PHP(以适当的为准)保持保存到服务器的图像的相同图像方向?
我正在为我的网站使用PHP5.5.18、jQuery、AJAX、HTML5、Bootstrap framework(v.3.3.0) 我的网站主要面向智能手机和智能设备的用户。因此,它在移动浏览器和PC/笔记本电脑浏览器上都能正常工作 在我的一个表单中,我为用户提供了一个工具,可以从设备摄像头捕获图像,也可以从设备的图像库中选择现有图像。在此之后,相同的图像应上传到服务器 现在一切都很好,非常完美。唯一持续存在的问题是图像方向的变化 让我把重点放在我面临的问题上。如果用户在“横向”模式下使用设备相机拍照,如果我查看保存在服务器上的图像,同一图像的方向将更改为“纵向”。如果用户以“纵向”方式拍摄照片,则会发生同样的情况,它会在服务器图像上变为“横向” 我在谷歌上搜索了很多关于这个解决方案的信息,然后我了解了“Imagick PHP扩展”。但由于默认情况下未激活此扩展,我无法在服务器上启用它,因为我没有权限执行此操作。因此,我正在寻求使用jQuery或PHP的其他解决方案,在这种情况下,这两种方法都是有益的 以下是我的代码供您参考: HTML代码:如何使用jQuery或PHP(以适当的为准)保持保存到服务器的图像的相同图像方向?,php,jquery,orientation,screen-orientation,exif,Php,Jquery,Orientation,Screen Orientation,Exif,我正在为我的网站使用PHP5.5.18、jQuery、AJAX、HTML5、Bootstrap framework(v.3.3.0) 我的网站主要面向智能手机和智能设备的用户。因此,它在移动浏览器和PC/笔记本电脑浏览器上都能正常工作 在我的一个表单中,我为用户提供了一个工具,可以从设备摄像头捕获图像,也可以从设备的图像库中选择现有图像。在此之后,相同的图像应上传到服务器 现在一切都很好,非常完美。唯一持续存在的问题是图像方向的变化 让我把重点放在我面临的问题上。如果用户在“横向”模式下使用设备
<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>
$('#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
$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代码:
<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>
$('#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
$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;
}
}
}
?>
如果你需要关于我面临的问题的任何进一步信息,请一定告诉我
谢谢。就我个人而言,我不会费心直接使用PHP的图像函数,而是依赖现有的(抽象)库 想象一下,它已经提供了三个驱动程序:GD、Imagick和Gmagick。 在项目中包含库之后,像这样使用它来保存从摄影机正确旋转的图像。它需要安装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/*);
读取元数据以手动执行此操作,请参见此处设备的旋转是否存储在EXIF数据中?查看以下链接以了解一些信息:如果安装了模块,PHP本身可以处理EXIF数据。在这里阅读:正如我们在聊天中所说的。没有做出任何努力或任何明显的尝试来解决这个问题。正如我告诉过你的,你可以用javascript解决这个问题。所以,请开始尝试解决它,如果你陷入困境,请编辑问题。到目前为止,这个问题是不合适的,因为它只是要求代码,不适合该网站。因为没有代码试图解决您描述的问题。