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;
                    }
                }
            }

     

  • 相关阅读:
    队列的链式存储结构实现
    堆栈的链式存储实现
    使用C#改变windows系统本地时间
    oracle 多数值录入校验(分隔符“/”)
    oracle中in和exists的区别
    redis安装 windows版(图形化安装)
    Oracle 返回结果集
    饿了么4年 + 阿里2年:研发路上的一些总结与思考
    Oracle 获取各类时间
    Oracle表中已有数据,修改字段长度
  • 原文地址:https://www.cnblogs.com/mxw09/p/2054802.html
Copyright © 2011-2022 走看看