注册 登录
编程论坛 ASP技术论坛

【请教】关于压缩图片长宽显示的问题

enjoy535 发布于 2009-10-18 11:46, 1023 次点击
问题是这样的,我用ASP做了一个简单的相册浏览功能,没有生成压缩图片的模块,我直接将上传的相片保存在服务器文件夹中,然后引用其地址显示在相册中,在相册浏览时,对一些图片的长宽肯定要缩小,以下是我直接通过<img>属性设定其长宽限制:

<img class="photo" src="<%= rs_list("type_img") %>"  onLoad="javascript:if(this.width>200 && this.width>this.height){this.width=200;}if(this.height>200 && this.width<this.height){this.height=200;}" >
(即将图片的长宽限制在200x200以内)

以上代码,似乎没有什么漏洞,但就是有时运行有效,有时无效(直接显示成了图片的原始大小,屏幕都快撑爆了...),很是不解,请高手赐教。
谢谢!
2 回复
#2
aspic2009-10-18 15:11
程序代码:
<script>
//图片按比例缩放
var flag=false;
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;
        }
    }
}
</script>
 
调用方法:
<img src="图片名称" onload="DrawImage(this,最大图片宽度,最大图片高度)" border="0">
你的代码 在图片比较大的情况下容易出问题
上面的代码多了个预加载
#3
enjoy5352009-10-18 16:08
好东东啊,比我编的好多了,谢谢斑竹!
1