前端 - 图片减少失真放大

        //图片大小控制ImgD图片路径,ImgW是宽或高
        function DrawImage(ImgD,ImgW){
            var image=new Image();
            image.src=ImgD.src;

            if(image.width>0 && image.height>0){

                if(image.width/image.height>= 1){
                    if(image.width>ImgW){
                        ImgD.width=ImgW;
                        ImgD.height=(image.height*ImgW)/image.width;
                    }else{
                        ImgD.width=image.width;
                        ImgD.height=image.height;
                    }
                    ImgD.alt=image.width+"×"+image.height;
                }
                else{
                    if(image.height>ImgW){
                        ImgD.height=ImgW;
                        ImgD.width=(image.width*ImgW)/image.height;
                    }else{
//ImgD.width=(image.width*ImgW)/image.height;
                        ImgD.width=image.width;
                        ImgD.height=image.height;
                    }
                    ImgD.alt=image.width+"×"+image.height;
                }
            }
       
            console.log(ImgD); // 里面有图片的信息,拿出来用

        }

 

posted @ 2019-11-20 19:38  一粒小米-博客  阅读(1047)  评论(0编辑  收藏  举报