Bootstrap 缩略图裁剪和定位

Bootstrap Thumbnail Cropping and Positioning

我有这 3 张不同尺寸的图片。当我像这样在 3 个容器中显示它们时:

@foreach (var item in Model) {   
    <div class="avatar-container">
        <img class="avatar img-thumbnail" src="@Href("~/Content/Avatars/",item.Name+".jpg")" />  
    </div>
}

这是我的 CSS 文件:

div.avatar-container {
    display: inline-block;
    max-height: 50px;
    overflow: hidden;
    width: 70px;
}

.avatar {
    width: 100%;
    height:auto;
    overflow: hidden;
}

还有 img-thumbnail 来自 bootstrap.css(第 368 行)。一段时间后,我设法裁剪图像(使用 overflow 属性),以便每个头像显示为 70x50 缩略图。

看看这 3 个返回的缩略图:image
[问题]

image1. 它还裁剪了我缩略图的漂亮底部。
image2. 好吧,我认为缩略图是正方形,而不是矩形。
image3. 如何裁剪到图像的中间(垂直和水平方式)?

这是我想出的,使用 JavaScript 和另一个缩略图容器。

@*HTML-----------------------------------------------*@
@foreach (var item in Model) {   
    <div class="avatar-container">
        <div class="img-thumbnail">
            <img class="avatar img-thumbnail" src="@Href("~/Content/Avatars/",item.Name+".jpg")" />  
        </div>
    </div>
}
@*JavaScript-----------------------------------------*@
//If the image has greater Width => display as landscape
<script>
        $(".avatar").each(function () {
            if (this.naturalWidth > this.naturalHeight) {
                $(this).addClass("landscape");
            }
        });
</script>
@*CSS------------------------------------------------*@
//That big container outside
div.avatar-container {
    display: inline-block;
    position: relative;
    width: 70px;
    height:70px;
    overflow: hidden;
}
//And here's the extra thumbnail container
div.img-thumbnail{
    width:100% !important;
    height:100%;
    background-color:#e1dada;
}
//This is for the actual image
.img-thumbnail{
    padding:1px !important;
}
//This one sets the image as a portrait
.avatar {
    position: absolute;
    top: 50%;
    left: 50%;
    height: 100%;
    width: auto;
    -webkit-transform: translate(-50%,-50%);
    -ms-transform: translate(-50%,-50%);
    -moz-transform: translate(-50%,-50%);
    -o-transform: translate(-50%,-50%);
    transform: translate(-50%,-50%);
}
//This one helps setting up the image horizontally
.landscape{
    width:100%;
    height:auto;
}

最终结果看起来像this