• Jquery 关于图片的压缩


    var OneImgUpLoad = {
        OneImageType: 0,
        OneImageData: null,
        AddImg: function (id) {
            var MyFiles = $("#" + id).prop('files');
            var MyFile = MyFiles[0];
            //检验是否为图像文件  
            if (!(MyFile.type == 'image/jpeg' || MyFile.type == 'image/png')) {
                AlphaMS.JSToolkit.Dialog.MessageBox("请上传jpeg或png格式的图片文件!");
                return;
            }
            var imageUrl = OneImgUpLoad.getObjectURL(MyFile);
            OneImgUpLoad.convertImgToBase64(imageUrl, function (base64Img) {
              
                //图片数据
                var image_base64 = base64Img.split(",")[1];
                //图片数据
                OneImgUpLoad.OneImageType = MyFile.type == 'image/jpeg' ? 1 : 2;
                OneImgUpLoad.OneImageData = image_base64;
                if (OneImgUpLoad.OneImageData != "") {
                    $("#PhotoResultDiv").css("display", "block");
                    $("#PhotoResult").attr("src", base64Img);
                }
                $("#" + id).html("");
    
    
            });
            event.preventDefault();
    
        },
        convertImgToBase64(url, callback, outputFormat) {
            var canvas = document.createElement('CANVAS');
            var ctx = canvas.getContext('2d');
            var img = new Image;
            img.crossOrigin = 'Anonymous';
            img.onload = function () {
                var width = img.width;
                var height = img.height;
                // 按比例压缩4倍
                var rate = (width < height ? width / height : height / width) / 4;
                canvas.width = width * rate;
                canvas.height = height * rate;
                ctx.drawImage(img, 0, 0, width, height, 0, 0, width * rate, height * rate);
                var dataURL = canvas.toDataURL(outputFormat || 'image/png');
                callback.call(this, dataURL);
                canvas = null;
            };
            img.src = url;
        },
        getObjectURL(file) {
            var url = null;
            if (window.createObjectURL != undefined) {  // basic
                url = window.createObjectURL(file);
            } else if (window.URL != undefined) {       // mozilla(firefox)
                url = window.URL.createObjectURL(file);
            } else if (window.webkitURL != undefined) { // web_kit or chrome
                url = window.webkitURL.createObjectURL(file);
            }
            return url;
        }
    
    }
    再牛逼的梦想,也抵不住我傻逼似的坚持!别在该奋斗的年纪,贪图安逸。 今天多学一些知识,明天开发的速度就更快一下。后天你就会变得更好。
  • 相关阅读:
    Xamarin.Forms
    Docker Azure Kubernetes
    出现( linker command failed with exit code 1)错误总结(http://blog.csdn.net/hengshujiyi/article/details/21182813)
    UITextView学习笔记
    UIScrollView学习笔记
    如何学习ios(摘自知乎https://www.zhihu.com/question/20016551)
    iOS手势操作,拖动,轻击,捏合,旋转,长按,自定义(http://www.cnblogs.com/huangjianwu/p/4675648.html)
    触屏事件
    给UITextView添加链接
    IOS绘图详解(http://blog.163.com/wkyuyang_001/blog/static/10802122820133190545227/)
  • 原文地址:https://www.cnblogs.com/LowKeyCXY/p/9055978.html
Copyright © 2020-2023  润新知