Skip to content
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

Dimensions rule fails to correctly check ratio 1/1 when height equals to width + 1 #33899

Closed
OrkhanAlikhanov opened this issue Aug 16, 2020 · 3 comments
Labels

Comments

@OrkhanAlikhanov
Copy link

OrkhanAlikhanov commented Aug 16, 2020

Description:

Dimensions rule fails to correctly check ratio 1/1 when height equals to width + 1. Following case passes validation while it should not:

width: n
height: n + 1
ratio: 1/1

Here is the code:

protected function failsRatioCheck($parameters, $width, $height)
{
if (! isset($parameters['ratio'])) {
return false;
}
[$numerator, $denominator] = array_replace(
[1, 1], array_filter(sscanf($parameters['ratio'], '%f/%d'))
);
$precision = 1 / max($width, $height);
return abs($numerator / $denominator - $width / $height) > $precision;
}

What it simplifies to:

  $precision = 1 / max($width, $height);
  $precision = 1 / (n + 1)
  $leftCalc = abs($numerator / $denominator - $width / $height)
  $leftCalc = 1 - n / (n + 1)
  $leftCalc = 1 / (n + 1) (became same as $precision)
check for fail:
  $leftCalc > $precision (which returns false, meaning it did not fail)

Steps To Reproduce:

use Illuminate\Http\UploadedFile;

$n = 64;

Validator::make([
	'image' => UploadedFile::fake()->image('image.png', $n, $n + 1),
], ['image' => 'dimensions:ratio=1'])
  ->errors()
  ->all()
@driesvints
Copy link
Member

This seems to be a bug specifically with the faking of images because when I write a test for this scenario in the framework with a real image it behaves as expected. I'm not sure what's going on here. Afaik the faked file's dimensions are passed correctly but the validator isn't behaving as expected.

@driesvints driesvints added the bug label Aug 24, 2020
@brettwhiteman
Copy link

It's a float comparison issue and it only happens with certain values of $n in the example above. For example validation passes with $n=64 but not with $n=72

I will submit a PR for this shortly.

@driesvints
Copy link
Member

Sent in #34003 for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants