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

[5.2] Validate Uploaded Image Dimensions #13428

Merged
merged 1 commit into from
May 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions src/Illuminate/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -1551,6 +1551,46 @@ protected function validateImage($attribute, $value)
return $this->validateMimes($attribute, $value, ['jpeg', 'png', 'gif', 'bmp', 'svg']);
}

/**
* Validate the dimensions of an image matches the given values.
*
* @param string $attribute
* @param mixed $value
* @param array $parameters
* @return bool
*/
protected function validateImageDimensions($attribute, $value, $parameters)
{
if (! $sizeDetails = getimagesize($value->getRealPath())) {
return false;
}

$this->requireParameterCount(1, $parameters, 'image_dimensions');

list($width, $height) = $sizeDetails;

$parameters = $this->parseNamedParameters($parameters);

if (
isset($parameters['width']) && $parameters['width'] != $width ||
isset($parameters['min_width']) && $parameters['min_width'] > $width ||
isset($parameters['max_width']) && $parameters['max_width'] < $width ||
isset($parameters['height']) && $parameters['height'] != $height ||
isset($parameters['min_height']) && $parameters['min_height'] > $height ||
isset($parameters['max_height']) && $parameters['max_height'] < $height
) {
return false;
}

if (isset($parameters['ratio'])) {
list($numerator, $denominator) = array_pad(sscanf($parameters['ratio'], '%d/%d'), 2, 1);

return $numerator / $denominator == $width / $height;
}

return true;
}

/**
* Validate the guessed extension of a file upload is in a set of file extensions.
*
Expand Down Expand Up @@ -2572,6 +2612,23 @@ protected function parseParameters($rule, $parameter)
return str_getcsv($parameter);
}

/**
* Parse named parameters to $key => $value items.
*
* @param array $parameters
* @return array
*/
protected function parseNamedParameters($parameters)
{
return array_reduce($parameters, function ($result, $item) {
list($key, $value) = array_pad(explode('=', $item, 2), 2, null);

$result[$key] = $value;

return $result;
});
}

/**
* Normalizes a rule so that we can accept short types.
*
Expand Down
55 changes: 55 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1656,6 +1656,61 @@ public function testValidateImage()
$this->assertTrue($v->passes());
}

public function testValidateImageDimensions()
{
// Knowing that demo image.gif has width = 3 and height = 2
$uploadedFile = new \Symfony\Component\HttpFoundation\File\UploadedFile(__DIR__.'/fixtures/image.gif', '');
$trans = $this->getRealTranslator();

$v = new Validator($trans, [], ['x' => 'image_dimensions:min_width=1']);
$v->setFiles(['x' => $uploadedFile]);
$this->assertTrue($v->passes());

$v = new Validator($trans, [], ['x' => 'image_dimensions:min_width=5']);
$v->setFiles(['x' => $uploadedFile]);
$this->assertTrue($v->fails());

$v = new Validator($trans, [], ['x' => 'image_dimensions:max_width=10']);
$v->setFiles(['x' => $uploadedFile]);
$this->assertTrue($v->passes());

$v = new Validator($trans, [], ['x' => 'image_dimensions:max_width=1']);
$v->setFiles(['x' => $uploadedFile]);
$this->assertTrue($v->fails());

$v = new Validator($trans, [], ['x' => 'image_dimensions:min_height=1']);
$v->setFiles(['x' => $uploadedFile]);
$this->assertTrue($v->passes());

$v = new Validator($trans, [], ['x' => 'image_dimensions:min_height=5']);
$v->setFiles(['x' => $uploadedFile]);
$this->assertTrue($v->fails());

$v = new Validator($trans, [], ['x' => 'image_dimensions:max_height=10']);
$v->setFiles(['x' => $uploadedFile]);
$this->assertTrue($v->passes());

$v = new Validator($trans, [], ['x' => 'image_dimensions:max_height=1']);
$v->setFiles(['x' => $uploadedFile]);
$this->assertTrue($v->fails());

$v = new Validator($trans, [], ['x' => 'image_dimensions:width=3']);
$v->setFiles(['x' => $uploadedFile]);
$this->assertTrue($v->passes());

$v = new Validator($trans, [], ['x' => 'image_dimensions:height=2']);
$v->setFiles(['x' => $uploadedFile]);
$this->assertTrue($v->passes());

$v = new Validator($trans, [], ['x' => 'image_dimensions:min_height=2,ratio=3/2']);
$v->setFiles(['x' => $uploadedFile]);
$this->assertTrue($v->passes());

$v = new Validator($trans, [], ['x' => 'image_dimensions:ratio=1/1']);
$v->setFiles(['x' => $uploadedFile]);
$this->assertTrue($v->fails());
}

/**
* @requires extension fileinfo
*/
Expand Down
Binary file added tests/Validation/fixtures/image.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.