如何设置图片的最大高度或宽度:
$img_attributes = ' height=100 width=100 max-height=500 max-width=500 '. 'alt="'.$product['product_name'].'"';
$img_attributes = ' height=100 width=100 max-height=500 max-width=500 '. 'alt="'.$product['product_name'].'"';
嗯,有 max-height
和 max-width
CSS 属性对吧?它们在除了IE 6和IE 7以外的所有主流浏览器中都可以使用。
style='max-height: 100px; max-width: 100px'
。 - Pekkafunction scaleSize(maxW, maxH, currW, currH){
var ratio = currH / currW;
if(currW >= maxW){
currW = maxW;
currH = currW * ratio;
} else >if(currH >= maxH){
currH = maxH;
currW = currH / ratio;
}
return [currW, currH];
}
使用此函数,您可以设置图像的宽度和高度:
img.width = newW;
img.height = newH;
但是,最好的方法是在服务器端完成。这将防止对客户端产生奇怪的影响。
<style>
IMG.MaxSized
{
max-width: 100px;
max-height: 100px;
}
</style>
正如Pekka所提到的,为了在现代版本的IE中使其工作,您必须拥有XHTML 1.0 Strict DTD,但我个人认为这是适当的方法。
max-height
或max-width
CSS属性。但是这些属性的行为不同。为保持图像的比例,您必须将高度和宽度设置为100%,然后设置max-width
。如果您设置了max-height
,则比例不会被保留。
因此:
<img src="image.png" style="height: 100%; width: 100%; max-width: 400px" alt=" "/>
<img src="image.png" style="height: 100%; width: 100%; max-height: 400px" alt=" "/>
不会。这是因为(据我所知)HTML首先设置宽度,然后再设置高度。因此,在第二种情况下,宽度被设置为100%,但高度受限,这可能导致图像失真。在第一种情况下,宽度设置了最大限制,高度相应地调整,从而保留了图像尺寸。