<input id="picture1" type="hidden" name="picture1" value="http://t1.baidu.com/it/u=4118736635,299908387&fm=25&gp=0.jpg" /> <img id="img" src="" alt="" />
我要把hidden里的图片先100*100等比例缩放到img元素里,然后又要按64*64显示在另外一个img元素里。。。想问下如何实现
function imgResize(maxWidth, maxHeight, imgObj) { var img = new Image(); img.src = imgObj.src; if (img.width > 0 && img.height > 0) { if (img.width / img.height >= maxWidth / maxHeight) { if (img.width > maxWidth) { imgObj.width = maxWidth; imgObj.height = (img.height * maxWidth) / img.width; } else { imgObj.width = img.width; imgObj.height = img.height; } } else { if (img.height > maxHeight) { imgObj.height = maxHeight; imgObj.width = (img.width * maxHeight) / img.height; } else { imgObj.width = img.width; imgObj.height = img.height; } } } else { imgObj.width = maxWidth; imgObj.height = maxHeight; } }
调用方法:
<img src="images/x.jpg" onload="imgResize(100,100,this);" />
只是等比例绽放的话,你服务器得存三张图,一张原始大小,一张100*100,一张60*60的,这才能算得上是等比例缩放
如果你只是想显示不同的大小,那你只需要给img加属性,width=100,height=100
$("#img1").attr("src",$("#picture1").val()).css({"height":"100","width":"100"});
$("#img2").attr("src",$("#picture1").val()).css({"height":"60","width":"60"});
就这样
原始大小 <img src="img.jpg">
100*100 <img src="img.jpg" width="100" heigth="100">
64*64 <img src="img.jpg" width="64" heigth="64">