zoukankan      html  css  js  c++  java
  • JS等比例缩放图片

    function DrawImage(ImgD, iwidth, iheight) {
                
    var image = new Image();
                image.src 
    = ImgD.src;
                
    if (image.width > 0 && image.height > 0) {
                    flag 
    = true;
                    
    if (image.width / image.height >= iwidth / iheight) {
                        
    if (image.width > iwidth) {
                            ImgD.width 
    = iwidth;
                            ImgD.height 
    = image.height * iwidth / image.width;
                        } 
    else {
                            ImgD.width 
    = image.width;
                            ImgD.height 
    = image.height;
                        }
                        ImgD.alt 
    = image.width + "×" + image.height;
                    }
                    
    else {
                        
    if (image.height > iheight) {
                            ImgD.height 
    = iheight;
                            ImgD.width 
    = image.width * iheight / image.height;
                        } 
    else {
                            ImgD.width 
    = image.width;
                            ImgD.height 
    = image.height;
                        }
                        ImgD.alt 
    = image.width + "×" + image.height;
                    }
                }
            }

     优化以后

    function DrawImage(ImgD, iwidth, iheight) {
                var image = new Image();
                image.src = ImgD.src;
                if (image.width > 0 && image.height > 0) 
                {             
                    if (image.width / iwidth  > image.height / iheight) {
                        if (image.width > iwidth) 
                        {
                            ImgD.width = iwidth;
                            ImgD.height = image.height * iwidth / image.width;
                        } else 
                        {
                            ImgD.width =  iwidth;
                            ImgD.height = image.height * image.width / iwidth;
                        }                    
                    }
                    else if(image.width / iwidth  < image.height / iheight)
                    {
                        if (image.height > iheight) 
                        {
                            ImgD.height = iheight;
                            ImgD.width = image.width * iheight / image.height;
                        } else 
                        {
                            ImgD.height= image.width;
                            ImgD.width = image.width * image.height / iheight
                        }                   
                    }else
                    {
                        ImgD.width = iwidth;
                        ImgD.height = iheight;
                    }
                }
            }

     

  • 相关阅读:
    [JavaWeb基础] 001.简单的JavaWeb代码和Tomcat配置部署
    [程序员短壁纸]2015年05月
    [注]什么是用户?估计90%人不知道
    [注]排行榜相关知识
    [注]微信公众号的运营推广总结方案(持续更新)
    [注]6W运营法则教你盘活社区内容运营
    [注]一条牛B的游戏推送要具备哪些条件?
    [微信营销企划之路]001.环境搭建(XAMPP+WeiPHP)
    [Python基础]005.语法(4)
    Java多线程设计模式
  • 原文地址:https://www.cnblogs.com/mxw09/p/2054802.html
Copyright © 2011-2022 走看看