h5拍照上传旋转90度问题

参考:https://www.cnblogs.com/Joe-and-Joan/p/10957818.html

<input class="myimg"  accept="image/*" type="file"  />

引入:        <script type="text/javascript" src="../js/exif.js" ></script>

                $(".myimg").change(function(e){
                    var file = this.files[0];//获取用户选择的文件
                    fixImageOrientation(file).then(file2 => {
                         console.log("新文件=="+file2);
                         console.log("新文件=="+window.URL.createObjectURL(file2));
                         //图片预览
                         //document.querySelector('.img_common').src = window.URL.createObjectURL(file2);
                       //上传文件到后台(我是上传到七牛云)
                       var formData = new FormData();
                       formData.append('file',file2); 
                       formData.append('token',qiniuToken);   
                       netupfile(formData);
                    })
                });    

                /**
                 * 修正图片旋转角度问题
                 * @param {file} 原图片
                 * @return {Promise} resolved promise 返回纠正后的新图片
                 */
                function fixImageOrientation (file) {
                  return new Promise((resolve, reject) => {
                    // 获取图片
                    const img = new Image();
                    img.src = window.URL.createObjectURL(file);
                    img.onerror = () => resolve(file);
                    img.onload = () => {
                      // 获取图片元数据(EXIF 变量是引入的 exif-js 库暴露的全局变量)
                      EXIF.getData(img, function() {
                        // 获取图片旋转标志位
                        var orientation = EXIF.getTag(this, "Orientation");
                        //console.log("获取图片旋转标志位=="+orientation);
                        // 根据旋转角度,在画布上对图片进行旋转
                        if (orientation === 3 || orientation === 6 || orientation === 8) {
                          const canvas = document.createElement("canvas");
                          const ctx = canvas.getContext("2d");
                          switch (orientation) {
                            case 3: // 旋转180°
                              canvas.width = img.width;
                              canvas.height = img.height;
                              ctx.rotate((180 * Math.PI) / 180);
                              ctx.drawImage(img, -img.width, -img.height, img.width, img.height);
                              break;
                            case 6: // 旋转90°
                              canvas.width = img.height;
                              canvas.height = img.width;
                              ctx.rotate((90 * Math.PI) / 180);
                              ctx.drawImage(img, 0, -img.height, img.width, img.height);
                              break;
                            case 8: // 旋转-90°
                              canvas.width = img.height; 
                              canvas.height = img.width;
                              ctx.rotate((-90 * Math.PI) / 180);
                              ctx.drawImage(img, -img.width, 0, img.width, img.height);
                              break;
                          }
                          // 返回新图片
                          canvas.toBlob(file => resolve(file), 'image/jpeg', 0.92)
                        } else {
                          return resolve(file);
                        }
                      });
                    };
                  });
                }    

                //上传图片
                function netupfile(formData){
                    jQuery.support.cors = true;  
                    $.ajax({
                        type:"post",    
                        url:img_url,  
                        data:formData,
                        dataType: 'json',
                        processData: false,
                        contentType: false,
                        async:true,
                        cache:false, 
                        processData: false,//不转化为信息
                        crossDomain: true == !(document.all),
                        success: function(res) {
                           console.log("上传结果=="+JSON.stringify(res));                        
                        },
                        error:function(res) {  

                       }   
                    });    
                }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
使用规范:  var file = $("#file_input")[0].files[0]; if(file.size / 1024 > 1024 * 5){ mui.toast('图片不能超过5M'); return; } //图片压缩 compress.rotatePhotoCompress(file,function(imageObj){ var formData = new FormData(); formData.append("uploadFile", imageObj, file.name); // 文件对象 //图片上传 uploadAdd(formData); }); /** * 图片上传 * @param formData */ function uploadAdd(formData){ var URL = apiUrl.addMultImageURL+"?plateTag=order&fileOwnerId=order&fileType=image&token;="+app.getToken(); $.ajax({ url: URL, //请求地址 type: "post", async: true, //默认为异步 data: formData, //参数 contentType: false, //数据请求格式 processData:false, xhr:function(){ myXhr = $.ajaxSettings.xhr(); if(myXhr.upload){ // check if upload property exists myXhr.upload.addEventListener('progress',function(e){ //显示进条 $("#progress1").show(); var loaded = e.loaded;//已经上传大小情况 var tot = e.total;//附件总大小 var per = Math.floor(100*loaded/tot); //已经上传的百分比 mui("#progress1").progressbar({progress:per}).show(); }, false); // for handling the progress of the upload } return myXhr; },// 成功 success: function (data) { //隐藏进条 $("#progress1").hide(); if (data.error == 0 && null != data.url) { mui.toast('上传图片成功'); $('.show_img').append("<div class='imgList'><span class='oClose'>X</span><img src="+app.fileUrl+data.url+"><input type='hidden' name='imgUrls["+count+"]' value="+data.url.replace(app.fileUrl,"")+"></div>"); count++; } else { mui.toast('上传图片失败'); $('#uploadPicButton').html('请你选择图片重新上传'); } }, error:function(rep) { $("#progress1").hide(); console.log(JSON.stringify(rep)); mui.toast('上传异常'); } }) }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值