php ios上传图片旋转,上传图片解决ios端图片旋转90度问题

前一阵做的上传图片人脸识别的功能,测试人员告诉我说ios手机上传到后台,发现图片反转了90度,以前听说过这个问题,现在遇到了。为了能尽快解决这个问题(偷懒),百度了一下,雾草,坑的不要不要的,在此呼吁一下,开发者别把半吊子代码发到网上,很容易误导人的有木有。

废话不多说=>

//此处省略HTML

var Orientation = null;

var canvasHead = '';

$('#fileBtn').on('change',function(){

var files = this.files[0];

var reader = new FileReader();

if(typeof FileReader == 'undefined'){

require('module/common/dialog').showToast('抱歉,您手机暂不支持');

return;

}else{

reader.readAsDataURL(files);

}

reader.onloadstart = function(){

require('module/common/dialog').showLoading('正在读取');

};

reader.onabort = function(){

require('module/common/dialog').hideLoading();

require('module/common/dialog').showToast('读取中断,请重试');

return false;

};

reader.onerror = function(){

require('module/common/dialog').hideLoading();

require('module/common/dialog').showToast('读取发生错误,请重试');

return false;

};

reader.onload = function(){

require('module/common/dialog').hideLoading();

require('module/common/dialog').showLoading('读取完成,玩命加载中');

if(reader.readyState == 2){

var fileStr = reader.result;

var image = new Image();

image.src = fileStr;

image.onload = function(){

$('#preHead').addClass('preHead');

var preHead_canvas = document.getElementById('preHead');

var preHead_ctx = preHead_canvas.getContext('2d');

$('.fileBtn').addClass('hide');

//获取Orientation

EXIF.getData(image, function() {

Orientation = EXIF.getTag(image, 'Orientation');

});

//如果没有Orientation 则为Android

if(!Orientation){

preHead_canvas.width = image.width;

preHead_canvas.height = image.height;

preHead_ctx.drawImage(image, 0, 0, image.width, image.height);

}

//如果有Orientation 则为IOS

else{

switch (Orientation) {

case 1:

preHead_canvas.width = image.width;

preHead_canvas.height = image.height;

preHead_ctx.drawImage(image, 0, 0, image.width, image.height);

break;

case 3:

preHead_canvas.width = image.width;

preHead_canvas.height = image.height;

preHead_ctx.rotate(Math.PI);

preHead_ctx.drawImage(image, -image.width, -image.height, image.width, image.height);

break;

case 6:

preHead_canvas.width = image.height;

preHead_canvas.height = image.width;

preHead_ctx.rotate(Math.PI / 2);

preHead_ctx.drawImage(image, 0, -image.height, image.width, image.height);

break;

case 8:

preHead_canvas.width = image.height;

preHead_canvas.height = image.width;

preHead_ctx.rotate(3 * Math.PI / 2);

preHead_ctx.drawImage(image, -image.width, 0, image.width, image.height);

break;

}

}

require('module/common/dialog').hideLoading();

canvasHead = preHead_canvas.toDataURL();

$('.cameraHead').attr('src',canvasHead);

$('#preHead').removeClass('hide');

};

}

}

});

代码比较简单,重点是根据Orientation的值来编写逻辑。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值