-
Notifications
You must be signed in to change notification settings - Fork 11k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[11.x] Fix ratio validation for high ratio images #51296
[11.x] Fix ratio validation for high ratio images #51296
Conversation
* fix image ration validation for high ratio images * fix style ci * enhance comments * enhance precision equation * enhance precision equation * enhance precision equation
I have a bit of trouble to understand this change, and to determine if it is really correct. To summarize, you have replaced: (and the larger the result is, the lowest the precision (i.e. tolerance) is, and it turn the "stricter" the aspect ratio validation is) The points I have trouble with:
Provided that the value of |
I'm thinking of the following approach: $desiredRatio = $numerator / $denominator;
$actualRatio = $width / $height;
$otherRatio1 = ($width + 1) / $height;
$otherRatio2 = ($width - 1) / $height;
$otherRatio3 = $width / ($height + 1);
$otherRatio4 = $width / ($height - 1); // FIXME: avoid division by 0 if $height === 1
$success = abs($desiredRatio - $actualRatio) === min(
abs($desiredRatio - $actualRatio),
abs($desiredRatio - $otherRatio1),
abs($desiredRatio - $otherRatio2),
abs($desiredRatio - $otherRatio3),
abs($desiredRatio - $otherRatio4),
); We test if the ratio of the image is the closest possible to the desired ratio. We try with width or height changed by 1 pixel, and if we were the closest possible, these other 4 cases will have ratios that are further to the desired ratio. This approach seems to be correct, but I hope there is some way to simplify the code. |
The above snippet fails with a 1366x768 image, because 1365x768 would be actually closer to the desired 16/9 ratio. So, I tried another approach: providing the actual width, or the height, calculate the other dimension using the desired ratio, and test if the result matches the actual other dimension. For instance: Desiring a 16/9 ratio:
Though, note how our 1366x768 image succeeds using the former calculation, but fails using the latter! A solution could be, when the other dimension is between two integers (i.e. the image can't perfectly achieve the desired ratio), to accept both the lower and the upper integer. But this would allow for too much imprecision with small images, for instance, desiring a 16/9 ratio with an image of 5 px width, we should really have a 3 px height, not 2 px. |
(still thinking of loud, hope it doesn't bother you) Considering the pitfalls I exposed in my above post, I figured out this approach: $calculatedHeight = round($width * $denominator / $numerator);
$calculatedWidth = round($height * $numerator / $denominator);
if ($height === $calculatedHeight || $width === $calculatedWidth) {
// Pass
} If at least one of the two calculations matches, we're good. As shown previously, a correct image could have only one calculation that matches. Of course, it is possible that both calculations match. And contrarily, if no calculation matches, we are certain there exists a more appropriate width-height pair. Nervertheless, I figured out another issue: if a calculated width (or height) has a ".5" decimal (e.g. 42.5), there is no particular reason to round it up, i.e. it should be equally acceptable to round it down. Sadly, supporting that case leads to a more complicated code: $calculatedHeight = $width * $denominator / $numerator;
$calculatedWidth = $height * $numerator / $denominator;
if (
$height === round($calculatedHeight, 0, PHP_ROUND_HALF_UP)
|| $height === round($calculatedHeight, 0, PHP_ROUND_HALF_DOWN)
|| $width === round($calculatedWidth, 0, PHP_ROUND_HALF_UP)
|| $width === round($calculatedWidth, 0, PHP_ROUND_HALF_DOWN)
) {
// Pass
} |
I see your concern but i tried to find an equation to solve almost of ratios, without breaking changes |
When uploading images with resolution of 1366x768 which has ratio 16/9 validation fails
so i go with solution to solve the issue provided with test case of the 16/9 ratio
its also increase the precise for other ratios