zoukankan      html  css  js  c++  java
  • 原生js压缩图片

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="{CHARSET}">
            <title>图片本地压缩</title>
        </head>
        <body>
            <input type="file" id="file"/>
            <img src="" id="img"/>
            <script>
                var oInput = document.getElementById ( 'file' )
                var oImg = document.getElementById("img");
                oInput.onchange = function(){
                    var fileData = oInput.files[ 0 ];
                    base64(fileData,function(base64Data){
                        //base64Data:处理成功返回的图片base64
                        oImg.setAttribute("src",base64Data);
                    });
                }
                
                function base64(file,backData){
                    /*
                     * file:input上传图片
                     * backData:处理完成回调函数
                     * */
                    var reader = new FileReader();
                    var image = new Image();
                    var canvas = createCanvas();
                    var ctx = canvas.getContext("2d");
                    reader.onload = function(){ // 文件加载完处理
                        var result = this.result;
                        image.onload = function(){ // 图片加载完处理
                            var imgScale = imgScaleW(1000,this.width,this.height);
                            canvas.width = imgScale.width;
                            canvas.height = imgScale.height;
                            ctx.drawImage(image,0,0,imgScale.width,imgScale.height);
                            var dataURL = canvas.toDataURL('image/jpeg'); // 图片base64
                            ctx.clearRect(0,0,imgScale.width,imgScale.height); // 清除画布
                            backData (dataURL); //dataURL:处理成功返回的图片base64
                        }
                        image.src = result;
                    };
                    reader.readAsDataURL(file);
                }
                
                function createCanvas(){ // 创建画布
                    var canvas = document.getElementById('canvas');
                    if(!canvas){
                        var canvasTag = document.createElement('canvas');
                        canvasTag.setAttribute('id','canvas');
                        canvasTag.setAttribute('style','display:none;');//隐藏画布
                        document.body.appendChild(canvasTag);
                        canvas = document.getElementById('canvas');
                    }
                    return canvas;
                }
                
                function imgScaleW(maxWidth,width,height){
                    /* maxWidth:宽度或者高度最大值
                     * width:宽度
                     * height:高度
                     * */
                    var imgScale = {};
                    var w = 0;
                    var h = 0;
                    if(width <= maxWidth && height <= maxWidth){ // 如果图片宽高都小于限制的最大值,不用缩放
                        imgScale = {
                            width,
                            height:height
                        }
                    }else{
                        if(width >= height){ // 如果图片宽大于高
                            w = maxWidth;
                            h = Math.ceil(maxWidth * height / width);
                           }else{     // 如果图片高大于宽
                            h = maxWidth;
                            w = Math.ceil(maxWidth * width / height);
                        }
                        imgScale = {
                            w,
                            height:h
                        }
                    }
                    return imgScale;
                }
            </script>
            
        </body>
    </html>
    不问前程凶吉,但求落幕无悔
  • 相关阅读:
    git push 出现 you are not allowed to upload merges 错误提示
    构建React-app应用时create-react-app卡住超慢的解决办法<转>
    防抖与节流函数<转>
    this全面解析<转>
    正确的安装和使用nvm(mac)<转>
    TypeScript如何添加自定义d.ts文件(转)
    为什么angular library的build不能将assets静态资源打包进去(转)
    Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
    linux命令新建文件
    mac系统终端sudo免输入密码技能get
  • 原文地址:https://www.cnblogs.com/qiao915/p/7754741.html
Copyright © 2011-2022 走看看