The best answer to scaling (as opposed to cropping) is Javascript, which controls the process on the server side and then programmatically sets the width and height in HTML / CSS. When you have different heights and widths, the new scaled width should be calculated relative to the height or vice versa, and sometimes both, and CSS does not provide a mechanism for this.
The math is pretty simple. To meet 150 horizontal requirements:
- newImageY = 150 / incomingImageX * incomingImageY
- newimageX = 150
To meet 100 vertical requirements:
- newImageX = 100 / incomingImageY * incomingImageX
- newimageY = 100
Then, if the scaled axis is too large for any remaining maximum constraints, you scale the entire image (both X and Y) by the required amount.
For cropping (as opposed to scaling), you come across several problems that you need to solve how to handle. Ideally, you (or the submitter) should decide which part of the image you want to show, and then correctly crop it. Therefore, we are talking about the active code and the corresponding user interface.
For example, there may be cases where images will not fill horizontally, but vertically; cases when it will not be filled vertically, if it is scaled to 150 horizontally, etc.
The bottom line is that “you need to study and process the image” if you want to do it well. CSS is simply not able to do a good job here.
fyngyrz
source share