未经沧桑 2020-06-09 16:55 采纳率: 0%
浏览 254

iPhone手机html5上传图片时会旋转,该怎么办?

iPhone手机html5上传图片时,需要旋转90度,我用了exif.js,发现它有兼容问题。
用了exif之后,iPhone6、iPhone11都没问题,但是iPhone7和iPhone8还有
iPhoneX都有问题,图片并没有旋转,这个兼容该怎么解决?有没有什么好的
办法?或者是换一个插件?在或者把这一步放到服务器去执行?但是放到服务
器去执行,我该怎么做啊?有没有大神教教我。

<!DOCTYPE html>  
<html>  
<head>  
    <meta charset="utf-8">  
    <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />  
    <title>图片上传</title>  
    <script type="text/javascript" src="http://code.jquery.com/jquery-2.1.4.min.js"></script>  
    <!-- <script type="text/javascript" src="js/uploadPicture/uploadImage.js" ></script>   -->
    <script src="https://cdn.bootcdn.net/ajax/libs/exif-js/2.3.0/exif.min.js" type="text/javascript" charset="utf-8"></script> 
    <script>  

    </script>  
</head>  
<body>  
    <div style="height: 50px; line-height: 50px;text-align: center;border-bottom: 1px solid #171E28;">  
            上传图片:  
            <!-- <input type="file" accept="image/*" id="uploadImage" capture="camera" onchange="selectFileImage(this);" /> -->
            <input type="file" accept="image/*" id="uploadImage" capture="camera" style="cursor:pointer"/>    
            <!-- 调取手机摄像头的属性 -->
        </div>  
        <div style="margin-top: 10px;">  
            <img alt="preview" src="" id="myImage" style="width: 300px;height: 300px;"/>  
        </div>  
</body>  
<script>
    var uploadImage=document.getElementById("uploadImage");
    /* uploadImage.onclick=function(){
        var file = this.files['0'];  
        alert(file)
    } */
    $('#uploadImage').on("change",function(){
        var file = this.files['0'];  
        console.log(this.value)
        console.log(file)
        alert(file)
        //图片方向角 added by lzk  
        var Orientation = null;  

        if (file) {  
            console.log("正在上传,请稍后...");  
            var rFilter = /^(image\/jpeg|image\/png)$/i; // 检查图片格式  
            if (!rFilter.test(file.type)) {  
                //showMyTips("请选择jpeg、png格式的图片", false);  
                return;  
            }  


            // var URL = URL || webkitURL;  
            //获取照片方向角属性,用户旋转控制  
            EXIF.getData(file, function() {  
            // alert(EXIF.pretty(this));  
                EXIF.getAllTags(this);   
                //alert(EXIF.getTag(this, 'Orientation'));   
                Orientation = EXIF.getTag(this, 'Orientation');  
                //return;  
            });  
            // alert(Orientation)
            var oReader = new FileReader();  
            oReader.onload = function(e) {  
                //var blob = URL.createObjectURL(file);  
                //_compress(blob, file, basePath);  
                alert(Orientation)
                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 > 800) {  
                        expectWidth = 800;  
                        expectHeight = expectWidth * this.naturalHeight / this.naturalWidth;  
                    } else if (this.naturalHeight > this.naturalWidth && this.naturalHeight > 1200) {  
                        expectHeight = 1200;  
                        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, 0, 0, expectWidth, expectHeight);  
                    var base64 = null;  
                    //修复ios  
                    if (navigator.userAgent.match(/iphone/i)) {  
                        alert('iphone');  
                        //alert(expectWidth + ',' + expectHeight);  
                        //如果方向角不为1,都需要进行旋转 added by lzk  
                        if(Orientation != "" && Orientation != 1){  
                            alert('旋转处理');  
                            switch(Orientation){  
                                case 6://需要顺时针(向左)90度旋转  
                                    alert('需要顺时针(向左)90度旋转');  
                                    rotateImg(this,'left',canvas);  
                                    break;  
                                case 8://需要逆时针(向右)90度旋转  
                                    alert('需要顺时针(向右)90度旋转');  
                                    rotateImg(this,'right',canvas);  
                                    break;  
                                case 3://需要180度旋转  
                                    alert('需要180度旋转');  
                                    rotateImg(this,'right',canvas);//转两次  
                                    rotateImg(this,'right',canvas);  
                                    break;  
                            }         
                        }  
                        base64 = canvas.toDataURL("image/jpeg", 0.8);  
                    }else {  
                        //alert(Orientation);  
                        if(Orientation != "" && Orientation != 1){  
                            //alert('旋转处理');  
                            switch(Orientation){  
                                case 6://需要顺时针(向左)90度旋转  
                                    alert('需要顺时针(向左)90度旋转');  
                                    rotateImg(this,'left',canvas);  
                                    break;  
                                case 8://需要逆时针(向右)90度旋转  
                                    alert('需要顺时针(向右)90度旋转');  
                                    rotateImg(this,'right',canvas);  
                                    break;  
                                case 3://需要180度旋转  
                                    alert('需要180度旋转');  
                                    rotateImg(this,'right',canvas);//转两次  
                                    rotateImg(this,'right',canvas);  
                                    break;  
                            }         
                        }  
                        base64 = canvas.toDataURL("image/jpeg", 0.8);  
                    }  
                    //uploadImage(base64);  
                    alert(4444)

                    $("#myImage").attr("src", base64);  
                };  
            };  
            oReader.readAsDataURL(file);  
        } 
    })

    /* function selectFileImage(fileObj) {  
        var file = fileObj.files['0'];  
        //图片方向角 added by lzk  
        var Orientation = null;  
        alert(11)
        if (file) {  
            console.log("正在上传,请稍后...");  
            var rFilter = /^(image\/jpeg|image\/png)$/i; // 检查图片格式  
            if (!rFilter.test(file.type)) {  
                //showMyTips("请选择jpeg、png格式的图片", false);  
                return;  
            }  


            // var URL = URL || webkitURL;  
            //获取照片方向角属性,用户旋转控制  
            EXIF.getData(file, function() {  
            // alert(EXIF.pretty(this));  
                EXIF.getAllTags(this);   
                //alert(EXIF.getTag(this, 'Orientation'));   
                Orientation = EXIF.getTag(this, 'Orientation');  
                //return;  
            });  
            // alert(Orientation)
            var oReader = new FileReader();  
            oReader.onload = function(e) {  
                //var blob = URL.createObjectURL(file);  
                //_compress(blob, file, basePath);  
                alert(Orientation)
                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 > 800) {  
                        expectWidth = 800;  
                        expectHeight = expectWidth * this.naturalHeight / this.naturalWidth;  
                    } else if (this.naturalHeight > this.naturalWidth && this.naturalHeight > 1200) {  
                        expectHeight = 1200;  
                        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, 0, 0, expectWidth, expectHeight);  
                    var base64 = null;  
                    //修复ios  
                    if (navigator.userAgent.match(/iphone/i)) {  
                        alert('iphone');  
                        //alert(expectWidth + ',' + expectHeight);  
                        //如果方向角不为1,都需要进行旋转 added by lzk  
                        if(Orientation != "" && Orientation != 1){  
                            alert('旋转处理');  
                            switch(Orientation){  
                                case 6://需要顺时针(向左)90度旋转  
                                    alert('需要顺时针(向左)90度旋转');  
                                    rotateImg(this,'left',canvas);  
                                    break;  
                                case 8://需要逆时针(向右)90度旋转  
                                    alert('需要顺时针(向右)90度旋转');  
                                    rotateImg(this,'right',canvas);  
                                    break;  
                                case 3://需要180度旋转  
                                    alert('需要180度旋转');  
                                    rotateImg(this,'right',canvas);//转两次  
                                    rotateImg(this,'right',canvas);  
                                    break;  
                            }         
                        }  


                        base64 = canvas.toDataURL("image/jpeg", 0.8);  
                    }else {  
                        //alert(Orientation);  
                        if(Orientation != "" && Orientation != 1){  
                            //alert('旋转处理');  
                            switch(Orientation){  
                                case 6://需要顺时针(向左)90度旋转  
                                    alert('需要顺时针(向左)90度旋转');  
                                    rotateImg(this,'left',canvas);  
                                    break;  
                                case 8://需要逆时针(向右)90度旋转  
                                    alert('需要顺时针(向右)90度旋转');  
                                    rotateImg(this,'right',canvas);  
                                    break;  
                                case 3://需要180度旋转  
                                    alert('需要180度旋转');  
                                    rotateImg(this,'right',canvas);//转两次  
                                    rotateImg(this,'right',canvas);  
                                    break;  
                            }         
                        }  

                        base64 = canvas.toDataURL("image/jpeg", 0.8);  
                    }  
                    //uploadImage(base64);  
                    $("#myImage").attr("src", base64);  
                };  
            };  
            oReader.readAsDataURL(file);  
        }  
    } 
   */
//对图片旋转处理 added by lzk  
function rotateImg(img, direction,canvas) {    
        //alert(img);  
        //最小与最大旋转方向,图片旋转4次后回到原方向    
        var min_step = 0;    
        var max_step = 3;    
        //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 = 2;    
        if (step == null) {    
            step = min_step;    
        }    
        if (direction == 'right') {    
            step++;    
            //旋转到原位置,即超过最大值    
            step > max_step && (step = min_step);    
        } else {    
            step--;    
            step < min_step && (step = max_step);    
        }    
        //img.setAttribute('step', step);    
        /*var canvas = document.getElementById('pic_' + pid);   
        if (canvas == null) {   
            img.style.display = 'none';   
            canvas = document.createElement('canvas');   
            canvas.setAttribute('id', 'pic_' + pid);   
            img.parentNode.appendChild(canvas);   
        }  */  
        //旋转角度以弧度值为参数    
        var degree = step * 90 * Math.PI / 180;    
        var ctx = canvas.getContext('2d');    
        switch (step) {    
            case 0:    
                canvas.width = width;    
                canvas.height = height;    
                ctx.drawImage(img, 0, 0);    
                break;    
            case 1:    
                canvas.width = height;    
                canvas.height = width;    
                ctx.rotate(degree);    
                ctx.drawImage(img, 0, -height);    
                break;    
            case 2:    
                canvas.width = width;    
                canvas.height = height;    
                ctx.rotate(degree);    
                ctx.drawImage(img, -width, -height);    
                break;    
            case 3:    
                canvas.width = height;    
                canvas.height = width;    
                ctx.rotate(degree);    
                ctx.drawImage(img, -width, 0);    
                break;    
        }    
    }
</script>
</html>
  • 写回答

2条回答 默认 最新

  • dabocaiqq 2020-09-01 11:05
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3