.ios上传会在exif中带一个 Orientation的属性,这个属性在windows中不会生效,在ios浏览器中会生效,造成图片在windows资源管理器中与ios浏览器中方向不一致 
为了用户体验,需要把图片矫正成正常的图片。短文
需要用到一个 exif 插件 地址 https://github.com/exif-js/exif-js/
代码
function check_file(files){
//校验收集表单数据
// var formdata = new FormData();
if(!files[] || !/image\/\w+/.test(files[].type)){ $.hidePreloader();
$('.li_imgs').children('.imgs').last().children().first().removeAttr('disabled');
_deny_request = false;
return;
} // formdata.append("imgFile0", files[0]); //处理IOS 拍照方向
EXIF.getData(files[],function(){
Orientation = EXIF.getTag(this,'Orientation');
});
var reader = new FileReader();
reader.readAsDataURL(files[]);
reader.onload = function(e) {
getImgData(e);
}
return;
} //e 图片的base64
function getImgData(e){
var image = new Image();
image.src = e.target.result;
image.onload = function() {
var expectWidth = this.naturalWidth;
var expectHeight = this.naturalHeight; if (this.naturalWidth > this.naturalHeight && this.naturalWidth > ) {
expectWidth = ;
expectHeight = expectWidth * this.naturalHeight / this.naturalWidth;
} else if (this.naturalHeight > this.naturalWidth && this.naturalHeight > ) {
expectHeight = ;
expectWidth = expectHeight * this.naturalWidth / this.naturalHeight;
}
var canvas = document.createElement("canvas");
var ctx = canvas.getContext("2d");
canvas.width = expectWidth;
canvas.height = expectHeight;
ctx.drawImage(this, , , expectWidth, expectHeight);
var base64 = null;
//修复ios
switch(Orientation){
case ://需要顺时针(向左)90度旋转
rotateImg(this,'left',canvas);
break;
case ://需要逆时针(向右)90度旋转
rotateImg(this,'right',canvas);
break;
case ://需要180度旋转
rotateImg(this,'right',canvas);//转两次
rotateImg(this,'right',canvas);
break;
}
base64 = canvas.toDataURL("image/jpeg", 0.7).substring();//这里处理一下base64编码头,以便php的 base64_decode可以解析,压缩一下图片,否则会413错误
        displayImg(base64);
}
} //对图片旋转处理
function rotateImg(img, direction,canvas) {
//alert(img);
//最小与最大旋转方向,图片旋转4次后回到原方向
var min_step = ;
var max_step = ;
//var img = document.getElementById(pid);
if (img == null)return;
//img的高度和宽度不能在img元素隐藏后获取,否则会出错
var height = img.height;
var width = img.width;
//var step = img.getAttribute('step');
var step = ;
if (step == null) {
step = min_step;
}
if (direction == 'right') {
step++;
//旋转到原位置,即超过最大值
step > max_step && (step = min_step);
} else {
step--;
step < min_step && (step = max_step);
}
//旋转角度以弧度值为参数
var degree = step * * Math.PI / ;
var ctx = canvas.getContext('2d');
switch (step) {
case :
canvas.width = width;
canvas.height = height;
ctx.drawImage(img, , );
break;
case :
canvas.width = height;
canvas.height = width;
ctx.rotate(degree);
ctx.drawImage(img, , -height);
break;
case :
canvas.width = width;
canvas.height = height;
ctx.rotate(degree);
ctx.drawImage(img, -width, -height);
break;
case :
canvas.width = height;
canvas.height = width;
ctx.rotate(degree);
ctx.drawImage(img, -width, );
break;
}
} /**
* android / ios 图片上传
*/
function displayImg(imgBinaryContentbase64){
remove_file_input($('.li_imgs').children('.imgs').last().children().first()); //删除旧的file域
$.showPreloader(_up_img_msg);
var data = {};
if(_IsIosDev){
data['ios'] = imgBinaryContentbase64;
}else{
data['content'] = imgBinaryContentbase64;
} $.ajax({
type:'post',
url:'?c=bzymgr/washcar&a=uploadAndroidImg&openid=<?php echo $this->openid;?>',
data:data,
dataType: "json",
async: true,
success:function(res){
if(res==''){
public_toast('上传失败,请稍后重试',);
return;
}
var html = '';
for(var i in res){
html += '<div class="imgs">\
<img src="'+res[i]+'" class="thumb_img"/>\
<b class="img_cancel" onclick="remove_img(this)">X</b>\
</div>';
//存储到localStorage
add_imgs_LocalStorage(res[i]);
}
//插入dom
$('.li_imgs').children('.imgs').last().before(html);
setTimeout(function(){
$.hidePreloader();
_deny_request = false;
},);
},
error:function(){
public_toast('服务器离家出走了,请稍后重试',);
},
});
}

修正ios h5上传图时的图片方向问题的更多相关文章

  1. iOS工程上传AppStore时遇到的问题“ERROR ITMS-90046”解析

    在我们将代码写完整,测试没有bug之后,我们就可以将它上传到AppStore了,上传的过程只要操作正确并不会有太大的问题,但是打包的过程中会出现一些小问题,导致打的包不能上传或者上传的时候会出现错误. ...

  2. iOS 应用上传所需 Icon图片大小

    iPhone-only Apps Include the following in your application's Resources group in the Xcode project: T ...

  3. 在使用 AjaxFileUpload 上传文件时,在项目发布到 iis 后,图片不能预览

    在使用 AjaxFileUpload  上传文件时,图片已经上传成功了,在站点没有发布时,可以预览,可是在项目发布到 iis 后,图片就不能预览,在网上找了很多的方案也没解决,最后的解决方案如下: 1 ...

  4. 利用exif.js解决ios手机上传竖拍照片旋转90度问题

    html5+canvas进行移动端手机照片上传时,发现ios手机上传竖拍照片会逆时针旋转90度,横拍照片无此问题:Android手机没这个问题. 因此解决这个问题的思路是:获取到照片拍摄的方向角,对非 ...

  5. 解决ios手机上传竖拍照片旋转90度问题

    html5+canvas进行移动端手机照片上传时,发现ios手机上传竖拍照片会逆时针旋转90度,横拍照片无此问题:Android手机没这个问题. 因此解决这个问题的思路是:获取到照片拍摄的方向角,对非 ...

  6. 微信JSSDK多图片上传并且解决IOS系统上传一直加载的问题

    微信多图片上传必须挨个上传,也就是不能并行,得串行: 那么我们可以定义一个如下所示的上传函数: var serverIds = []; function uploadImages(localImage ...

  7. Iphone H5上传照片被旋转

    最近做项目发现在Iphone下,我们上传图片都会被翻转,最后查阅资料发现,的确是IOS的问题 不说过程,直接解决方法 iOS下,html方式使用<input type="file&qu ...

  8. iOS App上传中遇到的问题

    1. 今天打包上传文件时出现“Missing iOS Distribution signing identity for XXXX” 导致问题的原因是:下边这个证书过期了 以下是苹果官方给出的回应: ...

  9. java接受安卓及ios App上传的图片,并保存到阿里OSS

    做后台的时候,写了两个方法,分别用来获取安卓和苹果IOS端上传的头像,保存到阿里云OSS图片存储服务器上.(SMM框架) 安卓及H5版本: /** * 上传用户头像 */ @RequestMappin ...

随机推荐

  1. syntactically incorrect() 404

    遇到这个错误就来记录一下吧. 这个错误是springMVC报出的错误.大致的意思就是form传值的类型和controller中的参数不符. 有可能是名字错误,有可能是类型不对.比如前面你传来的是  p ...

  2. Spring AOP:面向切面编程,AspectJ,是基于spring 的xml文件的方法

    导包等不在赘述: 建立一个接口:ArithmeticCalculator,没有实例化的方法: package com.atguigu.spring.aop.impl.panpan; public in ...

  3. UVa(12821),MCMF

    题目链接:https://uva.onlinejudge.org/external/128/12821.pdf 比赛的时候,准备用最短路来做,存两张图,做两次最短路,本来还觉得第二张图的设计很好的,很 ...

  4. python 中time.sleep没有作用

    很简单的一个程序: # -*- coding: utf-8 -*- import MySQLdb,os,json,time #from wsgiref.simple_server import mak ...

  5. 1. WP8.1学习笔记

    数据绑定 含义:将对象绑定到控件上 2.基本名词 控件:绑定目标 对象:绑定源(数据源) 控件与对象属性的联系:路径 如何绑定 创建对象,设置控件 在控件需要数据绑定的地方使用拓展语法 <But ...

  6. 2016 ACM/ICPC Asia Regional Qingdao Online HDU5879

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=5879 解法:我们知道到某个极限之后结果相同,所以找到那个极限,其他保存之后输出就好了 #include&l ...

  7. Repeater的Command操作

    Repeater的Command操作 1.ItemCommand事件 :在Repeater中所有能触发事件的控件,都会来触发这一个事件 后台创建:在Page_Load中 Repeater1.ItemC ...

  8. 开源的49款Java 网络爬虫软件

    参考地址 搜索引擎 Nutch Nutch 是一个开源Java 实现的搜索引擎.它提供了我们运行自己的搜索引擎所需的全部工具.包括全文搜索和Web爬虫. Nutch的创始人是Doug Cutting, ...

  9. SUSE Linux Enterprise Server 设置防火墙开启ssh远程端口

    1.vi /etc/sysconfig/SuSEfirewall2   #编辑防火墙设置 FW_SERVICES_EXT_TCP="22"   #开启22端口 rcSuSEfire ...

  10. .Net文件操作

    文件操作 File类,FileInfo类.using System.IO命名空间(一)创建 方法一: 1 private string path = @"F:\Text\aaa.txt&qu ...