简体   繁体   English

裁剪图像保持纵横比

[英]Crop images keeping aspect ratio

I have a list of images with text wrapped around but images I receive come in different sizes (all larger than 150px). 我有一个包含文字的图像列表,但我收到的图像有不同的尺寸(全部大于150像素)。 I need to crop them all to 150x100px but keep correct aspect ratio. 我需要将它们全部裁剪为150x100px但保持正确的宽高比。 Could anyone please suggest the best way of solving this problem? 有人可以建议解决这个问题的最佳方法吗? Thanks! 谢谢!

HTML: HTML:

<ul>        
    <div class="post" id="post">
    <div>
        <li>
        <img class="TextWrap" src="{{ picture }}">
        <a href="{{ link }}">{{ message }}</a><p>
        {{ time }}
        </li>   
    </div>
</ul> 

CSS: CSS:

.post {
    width: 500px;
    margin-top: 15px;
    float: left;
}

.post img {
    width: 150px;
    height: 100px;
    margin-bottom: 15px;
    margin-right: 20px;
}

.TextWrap {
    float: left;
}

As long as you do not want server side cropping, you can add following css: 只要您不想要服务器端裁剪,您可以添加以下css:

.post img {
    object-fit: cover;
}

Please check more possible values for object-fit 请检查更多可能的对象适合

作为Ray的答案的替代方案,您可以将图像设置为背景并使用:

background-size: cover;

The best answer to scaling (as opposed to cropping) is Javascript driving a server-side process and then setting the width and height in the HTML/CSS programatically. 缩放(与裁剪相反)的最佳答案是Javascript驱动服务器端进程,然后以编程方式设置HTML / CSS中的宽度和高度。 When you have varying heights and widths, the newly scaled width has to be calculated relative to the height, or vice-versa, and sometimes both, and CSS doesn't provide a mechanism for this. 当您具有不同的高度和宽度时,必须相对于高度计算新缩放的宽度,反之亦然,有时两者都计算,并且CSS不提供此机制。

The math is pretty simple. 数学很简单。 To match to a 150 horizontal requirement: 要符合150水平要求:

  • newImageY = 150 / incomingImageX * incomingImageY newImageY = 150 / incomingImageX * incomingImageY
  • newimageX = 150 newimageX = 150

To match to a 100 vertical requirement: 要符合100垂直要求:

  • newImageX = 100 / incomingImageY * incomingImageX newImageX = 100 / incomingImageY * incomingImageX
  • newimageY = 100 newimageY = 100

Then, if the scaled axis is too large for any remaining maximum constraints, you scale the entire image (both X and Y) down by the required amount. 然后,如果缩放的轴对于任何剩余的最大约束而言太大,则将整个图像(X和Y)缩小所需的量。

For cropping (as opposed to scaling), you face several issues that you have to decide how to handle. 对于裁剪(与缩放相反),您需要面对几个需要决定如何处理的问题。 Ideally, you (or the submitter) would decide what part of the image you want to show, and then crop intelligently. 理想情况下,您(或提交者)将决定您要显示的图像的哪个部分,然后智能裁剪。 So we're talking about both active code and an associated UI. 所以我们讨论的是活动代码和相关的UI。

For instance, there can be cases where images won't fill horizontally, but will vertically; 例如,可能存在图像不会水平填充但会垂直填充的情况; cases where it won't fill vertically if scaled to 150 horizontally, etc. 如果水平缩放到150,它将不会垂直填充的情况等。

The bottom line is pretty much "you have to examine and process the image" if you want to do this well. 如果你想做得好的话,最重要的是“你必须检查和处理图像”。 CSS just isn't up to doing a good job here. CSS在这里做得不好。

When no height and width are specified, or only one of them the picture will keep it's expected ration. 如果没有指定高度和宽度,或者只有其中一个图片将保持预期的比例。

max-width and max-height can still be used to limit the the size max-widthmax-height仍可用于限制尺寸

.post {
    width: 500px;
    margin-top: 15px;
    float: left;
}

.post img {
    max-width: 150px;
    max-height: 100px;
    margin-bottom: 15px;
    margin-right: 20px;
}

.TextWrap {
    float: left;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM