我正在处理图像,我遇到了纵横比问题。

<img src="big_image.jpg" width="900" height="600" alt="" />

如您所见,高度和宽度已经指定。我为图像添加了CSS规则:

img {
  max-width: 500px;
}

但对于big_image.jpg,我得到的宽度=500,高度=600。如何设置图像的大小,同时保持其纵横比。


当前回答

如果图像对于指定区域太大,这将使图像收缩(作为缺点,它不会放大图像)。

setec的解决方案适用于自动模式下的“收缩到适合”。但是,为了最佳地扩展以适应“自动”模式,您需要首先将接收到的图像放入临时id,检查它是否可以在高度或宽度上扩展(取决于其纵横比v/s,即显示块的纵横比),

$(".temp_image").attr("src","str.jpg" ).load(function() { 
    // callback to get actual size of received image 

    // define to expand image in Height 
    if(($(".temp_image").height() / $(".temp_image").width()) > display_aspect_ratio ) {
        $(".image").css('height', max_height_of_box);
        $(".image").css('width',' auto');
    } else { 
        // define to expand image in Width
        $(".image").css('width' ,max_width_of_box);
        $(".image").css('height','auto');
    }
    //Finally put the image to Completely Fill the display area while maintaining aspect ratio.
    $(".image").attr("src","str.jpg");
});

当接收到的图像小于显示框时,这种方法很有用。您必须将它们保存在服务器上的原始小尺寸,而不是其扩展版本,以填充更大的显示框,以节省大小和带宽。

其他回答

如果图像对于指定区域太大,这将使图像收缩(作为缺点,它不会放大图像)。

setec的解决方案适用于自动模式下的“收缩到适合”。但是,为了最佳地扩展以适应“自动”模式,您需要首先将接收到的图像放入临时id,检查它是否可以在高度或宽度上扩展(取决于其纵横比v/s,即显示块的纵横比),

$(".temp_image").attr("src","str.jpg" ).load(function() { 
    // callback to get actual size of received image 

    // define to expand image in Height 
    if(($(".temp_image").height() / $(".temp_image").width()) > display_aspect_ratio ) {
        $(".image").css('height', max_height_of_box);
        $(".image").css('width',' auto');
    } else { 
        // define to expand image in Width
        $(".image").css('width' ,max_width_of_box);
        $(".image").css('height','auto');
    }
    //Finally put the image to Completely Fill the display area while maintaining aspect ratio.
    $(".image").attr("src","str.jpg");
});

当接收到的图像小于显示框时,这种方法很有用。您必须将它们保存在服务器上的原始小尺寸,而不是其扩展版本,以填充更大的显示框,以节省大小和带宽。

使用伪元素进行垂直对齐怎么样?这更少的代码用于旋转木马,但我想它适用于每个固定大小的容器。它将保持纵横比,并在顶部/底部或左侧/写入最短尺寸时插入@灰色暗条。同时,图像通过文本对齐水平居中,通过伪元素垂直居中。

    > li {
      float: left;
      overflow: hidden;
      background-color: @gray-dark;
      text-align: center;

      > a img,
      > img {
        display: inline-block;
        max-height: 100%;
        max-width: 100%;
        width: auto;
        height: auto;
        margin: auto;
        text-align: center;
      }

      // Add pseudo element for vertical alignment of inline (img)
      &:before {
        content: "";
        height: 100%;
        display: inline-block;
        vertical-align: middle;
      }
    }

全屏演示:

img[data-attribute] {height: 100vh;}

请记住,如果观察端口高度大于图像,则图像将相对于差异自然降级。

下面的解决方案将允许图像的放大和缩小,具体取决于父框的宽度。

所有图像都有一个固定宽度的父容器,仅用于演示。在生产中,这将是父框的宽度。

最佳实践(2018):

此解决方案告诉浏览器以最大可用宽度渲染图像,并以该宽度的百分比调整高度。

.父级{宽度:100px;}国际货币基金组织{显示:块;宽度:100%;高度:自动;}<p>此图像最初为400x400像素,但应通过CSS调整大小:</p><div class=“parent”><img width=“400”height=“400”src=“https://placehold.it/400x400"></div>

更新颖的解决方案:

使用更高级的解决方案,无论图像大小如何,您都可以裁剪图像,并添加背景色来补偿裁剪。

.父级{宽度:100px;}.容器{显示:块;宽度:100%;高度:自动;位置:相对;溢出:隐藏;填充:34.37%0 0;/*34.37%=100/(w/w)=100/*/}.容器img{显示:块;最大宽度:100%;最大高度:100%;位置:绝对;顶部:0;底部:0;左:0;右:0;}<p>此图像最初为640x220,但应通过CSS调整大小:</p><div class=“parent”><div class=“container”><img width=“640”height=“220”src=“https://placehold.it/640x220"></div></div>

对于指定填充的行,需要计算图像的纵横比,例如:

640px (w) = 100%
220px (h) = ?

640/220 = 2.909
100/2.909 = 34.37%

因此,顶部填充=34.37%。

您可以创建如下div:

<div class="image" style="background-image:url('/to/your/image')"></div>

并使用此css设置其样式:

height: 100%;
width: 100%;
background-position: center center;
background-repeat: no-repeat;
background-size: contain; // this can also be cover