Jquery中压缩图像


<!DOCTYPE html>

<html>

<head>

<script src="/jquery/jquery-1.11.1.min.js">

</script>

<script>

function fixImage(img, w, h) {

var newImg = new Image(); //获得图片的原始尺寸

newImg.src = img.src;

var lh; //用于保存img.height,IE下隐藏的图片读取不到,需currentStyle解决

if (newImg.width/newImg.height >= w/h) {

if (newImg.width > w) {

img.width = w;

img.height = w * newImg.height / newImg.width;

lh = window.ActiveXObject ? parseInt(img.currentStyle['height']) : img.height;

img.style.marginTop = (h - lh)/2 + 'px'; //顺手垂直居中

} else {

img.width = newImg.width;

img.height = newImg.height;

lh = window.ActiveXObject ? parseInt(img.currentStyle['height']) : img.height;

img.style.marginTop = (h - lh)/2 + 'px'; //顺手垂直居中

}

} else {

if (newImg.height > h) {

img.height = h;

img.width = newImg.width * h / newImg.height;

} else {

img.width = newImg.width;

img.height = newImg.height;

}

}

}

</script>

<body>


<p>

一幅图像:

<img src="/i/eg_mouse.jpg" 40, 40)" ;/>

</body>

</html>