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

     

  • 相关阅读:
    PLSQL Developer使用技巧整理
    PLSQL DEVELOPER 使用的一些技巧【转】 .
    MYEclipse Available Memory is low 警告 解决方法
    myeclipse安装svn插件的多种方式
    MySql的存储过程和触发器
    springmvc学习及源码地址
    spring源码下载链接
    struts2源码下载链接
    个人总结的常用java,anroid网站
    Java生成扫描可以生成手机号名片的二维码
  • 原文地址:https://www.cnblogs.com/mxw09/p/2054802.html
Copyright © 2011-2022 走看看