There has been quite some discussion in Slack about a change in resize/crop behavior introduced by me. Fact is, I didn’t see that you cannot give width and height to the ImageViewHelper but just maximum width and height.
Let me explain how the calculation of the ResizeAdjustment (that is also responsible for the ImageViewHelper in the end) works now and what use cases I thought about and what definitely needs to be done.
First of all, the ResizeAdjustment has the following parameter:
- width
- height
- maximumWidth
- maximumHeight
- allowUpScaling
- ratioMode (which can be ‘inset’ or ‘outbound’ and basically maps to allowCropping false/true)
I will list scenarios and results below, maybe someone with more talent in image manipulation can add images that visually show the bahavior?
- Only one dimension is set (lets take width) and allowUpScaling is not set, ratioMode will play no role in this scenario so it doesn’t matter if set or not.
Result: Width is checked against the original image width and if bigger reduced to the original image width, after that the image is scaled to the resulting width according to the original aspect ratio.
- Same as above but now allowUpScaling is set.
Result: Width is directly used to scale according to the original aspect ratio.
- Same as 1 but maximumWidth is set.
Result: After checking against the original image width, the maximumWidth is checked and if width is heigher it will be restrained to maximumWidth, after that the image is again scaled according to the original aspect ratio.
- Now the more complicated scenarios, width AND height are set to something. First with allowUpScaling false and ratiomode “inset” (cropping false).
Result: We calculate the ratios between requested and original for width and height and scale to the one with the smaller ratio (but still with the original aspect ratio) so that we can be sure that both sides are less than or equal the requested width/height. As allowUpScaling is false if the result would be larger than the original image we just deliver the original image instead.
- Same as 4 but with max width and/or height set.
Result: Same as above, but after we know the size that fullfills the requested width/height we check if the result would fit in the given max and otherwise scale further down to stay inside the max values.
Note that is scenario is useless, you would either set width/height or maxWidth/maxHeight
- Same as 4 but with ratiomode “outbound” (cropping true)
Result: First we take a box that is exactly the given width/height as the expected outcome, as allowUpScaling is false we shrink the box according to the box aspect ratio until it fits the original image so that no upscaling is needed. Then we scale the image according to the original aspect ratio so that the smaller side fits the calculated box (means the other dimension will have content outside the box). We center the box on the image and crop to the box.
- Same as 4 but with allowUpScaling true and ratiomode “outbound” (cropping true)
Result: First we take a box that is exactly the given width/height as the expected outcome. Then we scale the image according to the original aspect ratio so that the smaller side fits the calculated box (means the other dimension will have content outside the box). We center the box on the image and crop to the box.
- Same as 6 but with max width/height
Result: First we take a box that is exactly the given width/height as the expected outcome. Then the box is scaled to the given maximum according tot he box aspect ratio. Then we scale the image according to the original aspect ratio so that the smaller side fits the calculated box (means the other dimension will have content outside the box). We center the box on the image and crop to the box.
Ok I left out some scenarios but I think you get the big picture. All in all this doesn’t allow for distorted images currently (a scenario I don’t think we should support out of the box).
The big point is, you might not know what aspect ratio a given image has. So setting a width and height to something but without ratiomode outbound would just allow you to have an image with the original aspect ratio fitting into an area. Now here it seems this is maximum width/height. But what if you set only a width but at the same time want to constrain really tall images, you can use a combination of width and max height. Same vice versa. IMHO a valid use case.
Obivously there are a few scenarios where arguments will be useless but that cannot be avoided I guess.
If you set width AND height with ratiomode “inset” then max width/height should usually never affect the result unless they are smaller than width/height.
If you set only width OR height the ratiomode makes no difference at all.
So for me, we need to expose width/height to the Image VH and TS image object. We could set width/height to max width height if not given for now and deprecate this at the same time (roughly do what the hotfix https://bitbucket.org/snippets/vivomedia-neos/89pkx does with additionally allowing width/height directly).