微信网页IOS上传图片旋转解决方案

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>分享</title>
    <meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="stylesheet" href="../../Resource/css/mui.min.css">
    <script src="../../Resource/js/mui.min.js"></script>
    <link href="../../Resource/layer_mobile/need/layer.css" rel="stylesheet" />
    <script src="../../Resource/layer_mobile/layer.js"></script>
    <script src="../../Resource/js/jquery-1.9.1.js" type="text/javascript" charset="utf-8"></script>
    <script src="../../Resource/js/exif.js"></script>
    <script src="http://res.wx.qq.com/open/js/jweixin-1.1.0.js"></script>
    <script src="../../Resource/weixin/hyx_base.js"></script>
    <script src="../../Resource/weixin/WeiXinShare.js"></script>
    <style>
        body {
            background: #F5F5F5;
        }

        .mui-content {
            background: #f5f5f5;
        }

        .mui-input-row {
            margin-top: 10px;
            background: #fff;
            padding: 10px;
        }

        textarea {
            border: none;
        }

        .chuan {
            width: 50px;
            height: 50px;
        }

        .del {
            position: relative;
            top: -39px;
            left: -14px;
            width: 15px;
        }

        footer {
            position: fixed;
            bottom: 0px;
            color: #fff;
            background: #01BF3A;
            width: 100%;
            padding: 12px 0px;
            text-align: center;
            font-size: 17px;
            border-top: 1px solid #eee;
        }
    </style>
</head>

<body>

    <div class="mui-input-row">
        <textarea id="textarea" rows="5" placeholder="一起聊聊生活把"></textarea>
    </div>
    <div class="mui-input-row" id="ImgList">
        <img class="chuan" id="imgUpload" src="../../Resource/img/shangcaun.png" />
        <input type="file" id="fileToUpload" name="upfile" style=" display:none;" />
    </div>
    <footer id="Add">发送</footer>

    <script type="text/javascript">
        var browser = {
            versions: function () {
                var a = navigator.userAgent, b = navigator.appVersion;
                return {
                    trident: a.indexOf("Trident") > -1, presto: a.indexOf("Presto") > -1,
                    webKit: a.indexOf("AppleWebKit") > -1, gecko: a.indexOf("Gecko") > -1 && a.indexOf("KHTML") == -1,
                    mobile: !!a.match(/AppleWebKit.*Mobile.*/), ios: !!a.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/),
                    android: a.indexOf("Android") > -1 || a.indexOf("Linux") > -1, iPhone: a.indexOf("iPhone") > -1,
                    iPad: a.indexOf("iPad") > -1, webApp: a.indexOf("Safari") == -1
                }
            }(),
            language: (navigator.browserLanguage || navigator.language).toLowerCase()
        };

        var openid = getCookie("openid");
        var fileUrl = "";
        $(function () {
            initJSAPI();
            if (openid == undefined || openid == "" || openid == null || openid == "null") {
                var code = getQueryString("code");
                if (code == null) {
                    JumpToOauth();
                } else {
                    initOpenid(code);
                }
            } else {
                GetUserInfo(openid);

            }

        });
        function initOpenid(code) {
            $.ajax({
                type: "POST",
                timeout: 80000,
                url: "../../GetJsApi/GetOpenid",
                dataType: "text",
                data: { "code": code },
                beforeSend: function (XMLHttpRequest) {

                },
                success: function (msg) {
                    //获取相应的值,并存放到cookie中去
                    if (msg == undefined || msg.length == 0) {
                    } else {
                        openid = msg;
                        setCookie("openid", msg, 1);
                        GetUserInfo(openid);
                    }
                },
                complete: function (XMLHttpRequest, textStatus) {
                    if (textStatus == ‘timeout‘) {
                        //超时,status还有success,error等值的情况
                        alert("请求超时,请返回重试");
                    }
                },
                error: function (err) {
                    console.log("数据加载失败");
                }
            });
        }
        //获取微信用户是否关注公众号
        function GetUserInfo(openid) {
            $.ajax({
                type: "POST",
                timeout: 80000,
                url: "../../GetJsApi/WeChatIsFollow",
                dataType: "json",
                data: { "openid": openid },
                beforeSend: function (XMLHttpRequest) {

                },
                success: function (msg) {
                    if (msg[0].STATUS != "Y") {
                        ShowQrcode();
                    }
                },
                complete: function (XMLHttpRequest, textStatus) {
                    if (textStatus == ‘timeout‘) {
                        //超时,status还有success,error等值的情况
                        layerPrompt("请求超时,请返回重试");
                    }
                },
                error: function (err) {
                    layerPrompt("数据加载失败");
                }
            });
        }

        //上传图片
        $("#imgUpload").click(function () {
            $("#fileToUpload").click();

        })
        window.setInterval(function () {
            console.log($("#fileToUpload").val());
            if (fileUrl != $("#fileToUpload").val()) {
                fileUrl = $("#fileToUpload").val();
                if (fileUrl != "" && fileUrl != undefined) {
                    layer.open({
                        type: 2,
                        shadeClose: false,
                        content: ‘正在上传...‘
                    });
                    uploadBtnClick();
                }
            }
        }, 1000)

        /********************************
        上传图片
        */
        function uploadBtnClick() {
            var scope = this;
            // change pic to base64
            if (window.File && window.FileReader && window.FileList && window.Blob) {
                var filefield = document.getElementById(‘fileToUpload‘),
                    file = filefield.files[0];
                //IOS
                if (browser.versions.ios) {
                    //获取图片的相关信息
                    EXIF.getData(file, function () {
                        EXIF.pretty(this);
                        var Orientation = EXIF.getTag(this, ‘Orientation‘);
                        processfile(file, uploadCompressFile, scope, Orientation);
                        alert(Orientation);
                    });
                    //Android
                } else if (browser.versions.android) {
                    processfile(file, uploadCompressFile, scope, 1);
                }
            } else {
                alert("此浏览器不完全支持上载图片");
            }
        }
        function processfile(file, uploadCompressFile, scope, Orientation) {
            var reader = new FileReader();
            reader.onload = function (event) {
                var blob = new Blob([event.target.result]);
                window.URL = window.URL || window.webkitURL;
                var blobURL = window.URL.createObjectURL(blob);

                var image = new Image();
                image.src = blobURL;
                image.onload = function () {
                    var resized = resizeMe(image, Orientation);
                    newImg = resized;
                    uploadCompressFile.apply(scope);
                }
            };
            reader.readAsArrayBuffer(file);
        }
        //压缩图片
        function resizeMe(img, Orientation) {
            //压缩的大小
            var max_width = 500;
            var max_height = 375;
            var iSxz = true;
            var canvas = document.createElement(‘canvas‘);
            var width = img.width;
            var height = img.height;
            if (width > height) {
                if (width > max_width) {
                    height = Math.round(height *= max_width / width);
                    width = max_width;
                }
            }
            else {
                if (height > max_height) {
                    width = Math.round(width *= max_height / height);
                    height = max_height;
                }
            }
            //跳转图片大小
            if (Orientation == 6) {
                canvas.width = height;
                canvas.height = width;
            } else {
                canvas.width = width;
                canvas.height = height;
            }
            var ctx = canvas.getContext("2d");
            //旋转图片
            if (Orientation == 6) {
                ctx.save();
                ctx.translate(height / 2, width / 2);
                ctx.rotate(90 * Math.PI / 180.0);
                ctx.drawImage(img, -width / 2, -height / 2, width, height);
            } else {
                ctx.drawImage(img, 0, 0, width, height);
            }
            //压缩率(返回base64编码)
            return canvas.toDataURL("image/png", 0.1);
        }

        //上传图片
        function uploadCompressFile() {
            $.ajax({
                type: "post",
                timeout: 80000,
                async: false,
                url: "../../FileUpload/FileImg",
                dataType: "text",
                data: { "BaseImg": newImg },
                beforeSend: function (XMLHttpRequest) {
                },
                success: function (msg) {
                    if (msg != "") {

                        var Image = "http://www.huiyexing.cn" + msg; //"http://hz.huiyexing.cn" +

                        layer.closeAll();
                        $("#imgUpload").before("<img class=‘chuan NewImg‘ src=‘" + Image + "‘  /><img class=‘del‘ src=‘../../Resource/img/deldeldel.png‘  />")
                        var Imglist = $("#ImgList").find(".NewImg");
                        if (Imglist.length == 9) {
                            $("#imgUpload").hide();
                        } else {
                            $("#imgUpload").show();
                        }
                        $(".del").click(function () {
                            $(this).prev().remove();
                            $(this).remove();
                            $("#imgUpload").show();
                        });
                        $("#fileToUpload").val("");
                    }
                },
                complete: function (XMLHttpRequest, textStatus) {
                    if (textStatus == ‘timeout‘) {
                        //超时,status还有success,error等值的情况
                        layerPrompt("请求超时,请返回重试");
                    }
                },
                error: function (err) {
                    layerPrompt("数据加载失败");
                }
            });
        }

        //Add
        function AddCircle() {
            var Content = escape($("#textarea").val());
            if (Content == "") {
                layerPrompt("请输入");
                return;
            }
            var imgListUrl = "";
            var imgList = $("#ImgList").find(".NewImg");
            $(imgList).each(function () {
                imgListUrl += $(this).attr("src") + ",";
            })
            if (imgListUrl != "") {
                imgListUrl = imgListUrl.substring(0, imgListUrl.length - 1);
            }
            $.ajax({
                type: "POST",
                timeout: 80000,
                url: "../../Circle/AddCircle",
                dataType: "text",
                data: { "openid": openid, "Content": Content, "ContentImges": imgListUrl },
                beforeSend: function (XMLHttpRequest) {

                },
                success: function (msg) {
                    if (msg == "true") {
                        window.location.href = "Index.html";
                    } else {
                        layerPrompt("操作失败");
                    }
                },
                complete: function (XMLHttpRequest, textStatus) {
                    if (textStatus == ‘timeout‘) {
                        //超时,status还有success,error等值的情况
                        layerPrompt("请求超时,请返回重试");
                    }
                },
                error: function (err) {
                    layerPrompt("数据加载失败");
                }
            });
        }
        $("#Add").click(function () {
            AddCircle();
        })
    </script>
</body>

</html>
时间: 2024-08-11 01:25:36

微信网页IOS上传图片旋转解决方案的相关文章

IOS 中微信 网页授权报 key[也就是code]失效 解决办法

枪魂微信平台ios手机点击返回 网页授权失败,报key失效.已经解决,原因是授权key只能使用一次,再次使用就会失效. 解决办法:第一次从菜单中进行授权时,用session记录key和open_id.当点击返回时,判断session中key是否为空当不为空时,直接获取open_id,就不用通过key进行验证并获取open_id . $open_id = ""; if(!empty($_GET["code"])&&empty($_SESSION[&qu

ios微信网页出现底部返回导航栏

现在,苹果手机的微信网页会出现底部返回导航栏,导致页面的部分内容会被导航栏遮挡住. 试验了很多次,最后是通过每次加载页面的时候获取页面的实际高度,来控制网页body体的高度. 只要在加载完成之后,添加这一句就可以避免出现内容被遮挡的情况了. $("body").css("height",document.body.clientHeight+"px"); 原文地址:https://www.cnblogs.com/Andrea-Li/p/984425

手把手实现微信网页授权和微信支付,附源代码(VUE and thinkPHP)

wechat github 概述 公众号开发是痛苦的,痛苦在好多问题开发者文档是没有提到的,是需要你猜的. 在开发过程中翻了好多的文档,都是说明其中的一部分问题的,很费时间,所以在此总结大体过程.我们模拟的是一个支付的商城,在实现购买过程中基本是把微信公众号最主要模块实现了,其余的功能我们没有涉及,但应该是触类旁通的. 我们叙述的过程是按开发流程进行叙述的,不会是按照开发文档的形式叙述,希望您能结合微信的开发文档一起阅读,当然在流程中我们会提醒你阅读的部分 目录 概述 [解决的问题] [前端技术

微信网页中上传文件时“只能选择SD卡中的文件”问题

需求描述:在微信网页中上传文件到我们自己的服务器,使用 input  type=file标准控件: <input type="file" name="upfile" id="upfile" > 问题来了:苹果可以顺利上传图片文件的,安卓下每次选择完图片文件之后会弹出提示"只能选择SD卡中的文件"不能上传图片.手机已经设置默认存储为SD卡. 百度搜了一个下午没有结果,微信公众平台更没有答案shiiiit. 最后在cs

关于微信网页/H5游戏开发中二维码无法识别的解决方法

我接触微信H5开发已经有一年多了,做过很多案例也遇到很多的问题.今天我把困扰我半年之久的问题分享出来,也就是关于微信网页/H5游戏开发中二维码无法识别的解决方法. 我在百度搜索了许久,关于微信H5网页/游戏开发问题以及解决方案的相关文章少之又少,在相关前端交流群中问别人,一问三不知,平时这些群就会上班吹牛逼,真正交流问题的人少又少,真是揪心啊,最后还是得靠自己解决了. [这里,我先举个栗子] 上面这个页面,相信从事微信H5开发的人应该很熟悉,就是常见的分享到朋友圈的页面,通常以[引导分享图标]+

【腾讯Bugly干货分享】微信读书iOS性能优化

本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/578c93ca9644bd524bfcabe8 "8小时内拼工作,8小时外拼成长"这是大家共同的理想.除了每天忙于工作外,我们都希望能更多地区吸收领域内的新知识与新技能,从而走向人生巅峰. Dev Club 是一个交流移动开发技术,结交朋友,扩展人脉的社群,成员都是经过审核的移动开发工程师.每周都会举行嘉宾分享,话题讨论等活动. 上一期我们邀请了腾讯SNG工程师&qu

微信网页分享标题图片自定义设置(最新)

1 前言 刚好有微信网页分享标题图片自定义设置这个需求,然后查找文档,发现有两种方案[1],但是第一种方案已经失效了,只能走第二种方案,然后根据实战配置好了,本文会写上配置中遇到的问题和解决方案,也作为记录使用,方便自己也方便他人. 2 步骤 2.1 准备工作 2.1.1 微信JS-SDK说明文档:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115 2.1.2 附录6-DEMO页面和示例代码:在JS-SDK说明文档中

微信网页授权认证获取用户的详细信息,实现自动登陆-微信公众号开发干货

原创声明:本文为本人原创作品,绝非他处转账,转载请联系博主 从接触公众号到现在,开发维护了2个公众号,开发过程中遇到很多问题,现在把部分模块功能在这备案一下,做个总结也希望能给其他人帮助 工欲善其事,必先利其器,先看看开发公众号需要准备或了解什么 web开发工具:官方提供的开发工具,使用自己的微信号来调试微信网页授权.调试.检验页面的 JS-SDK 相关功能与权限,模拟大部分 SDK 的输入和输出.下载地址:web开发工具下载 开发文档:https://mp.weixin.qq.com/wiki

OAuth 2.0及微信网页授权

理解OAuth 2.0 by 阮一峰 微信网页授权