JS 等比缩放图片函数

1.

<script   type="text/javascript">
    function pic_reset(drawImage, thumbs_size) {
        var max = thumbs_size.split(',');
        var fixwidth = max[0];
        var fixheight = max[1];
        w = drawImage.width; h = drawImage.height;
        if (w > fixwidth) { drawImage.width = fixwidth; drawImage.height = h / (w / fixwidth); }
        if (h > fixheight) { drawImage.height = fixheight; drawImage.width = w / (h / fixheight); }
        drawImage.style.cursor = "pointer";
    }
</script>

2.

<script   type="text/javascript"> 
    var flag = false;
    function DrawImage(ImgD) {
        var image = new Image();
        image.src = ImgD.src;

        var width = 600;
        var height = 500;
        if (image.width > 0 && image.height > 0) {
            flag = true;
            if (image.width / image.height >= width / height) {
                if (image.width > width) {
                    ImgD.width = width;
                    ImgD.height = (image.height * width) / image.width;
                } else {
                    ImgD.width = image.width;
                    ImgD.height = image.height;
                }
                //ImgD.alt = image.width + "×" + image.height;
            }
            else {
                if (image.height > height) {
                    ImgD.height = height;
                    ImgD.width = (image.width * height) / image.height;
                } else {
                    ImgD.width = image.width;
                    ImgD.height = image.height;
                }
            }
        }
    }  
</script> 

 

posted @ 2011-09-16 18:05  徐桀  阅读(325)  评论(0编辑  收藏  举报