Skip to content

Commit

Permalink
Merge pull request #8966 from christianberkman/feat-min-dims
Browse files Browse the repository at this point in the history
feat: [Validation] add `min_dims` rule in FileRules
  • Loading branch information
kenjis authored Jun 20, 2024
2 parents 11d0631 + 5942103 commit 7604c25
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 0 deletions.
48 changes: 48 additions & 0 deletions system/Validation/FileRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,4 +260,52 @@ public function max_dims(?string $blank, string $params): bool

return true;
}

/**
* Checks an uploaded file to verify that the dimensions are greater than
* a specified dimension.
*/
public function min_dims(?string $blank, string $params): bool
{
// Grab the file name off the top of the $params
// after we split it.
$params = explode(',', $params);
$name = array_shift($params);

$files = $this->request->getFileMultiple($name);
if ($files === null) {
$files = [$this->request->getFile($name)];
}

foreach ($files as $file) {
if ($file === null) {
return false;
}

if ($file->getError() === UPLOAD_ERR_NO_FILE) {
return true;
}

// Get Parameter sizes
$minimumWidth = $params[0] ?? 0;
$minimumHeight = $params[1] ?? 0;

// Get uploaded image size
$info = getimagesize($file->getTempName());

if ($info === false) {
// Cannot get the image size.
return false;
}

$fileWidth = $info[0];
$fileHeight = $info[1];

if ($fileWidth < $minimumWidth || $fileHeight < $minimumHeight) {
return false;
}
}

return true;
}
}
18 changes: 18 additions & 0 deletions tests/system/Validation/FileRulesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,24 @@ public function testMaxDimsBad(): void
$this->assertFalse($this->validation->run([]));
}

public function testMinDims(): void
{
$this->validation->setRules(['avatar' => 'min_dims[avatar,320,240]']);
$this->assertTrue($this->validation->run([]));
}

public function testMinDimsFail(): void
{
$this->validation->setRules(['avatar' => 'min_dims[avatar,800,600]']);
$this->assertFalse($this->validation->run([]));
}

public function testMinDimsBad(): void
{
$this->validation->setRules(['avatar' => 'min_dims[unknown,640,480]']);
$this->assertFalse($this->validation->run([]));
}

public function testIsImage(): void
{
$this->validation->setRules(['avatar' => 'is_image[avatar]']);
Expand Down
18 changes: 18 additions & 0 deletions tests/system/Validation/StrictRules/FileRulesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,24 @@ public function testMaxDimsBad(): void
$this->assertFalse($this->validation->run([]));
}

public function testMinDims(): void
{
$this->validation->setRules(['avatar' => 'min_dims[avatar,320,240]']);
$this->assertTrue($this->validation->run([]));
}

public function testMinDimsFail(): void
{
$this->validation->setRules(['avatar' => 'min_dims[avatar,800,600]']);
$this->assertFalse($this->validation->run([]));
}

public function testMinDimsBad(): void
{
$this->validation->setRules(['avatar' => 'min_dims[unknown,640,480]']);
$this->assertFalse($this->validation->run([]));
}

public function testIsImage(): void
{
$this->validation->setRules(['avatar' => 'is_image[avatar]']);
Expand Down
1 change: 1 addition & 0 deletions user_guide_src/source/changelogs/v4.6.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ Libraries
=========

- Added `retainMultiplePatterns()` to `FileCollection` class. See :ref:`FileCollection::retainMultiplePatterns() <file-collections-retain-multiple-patterns>`.
- Added `min_dims` validation rule to `FileRules` class. See :ref`Validation <rules-for-file-uploads>`.

Helpers and Functions
=====================
Expand Down
7 changes: 7 additions & 0 deletions user_guide_src/source/libraries/validation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,13 @@ max_dims Yes Fails if the maximum width and height of an
the width, and the third is the height. Will
also fail if the file cannot be determined
to be an image.
min_dims Yes Fails if the minimum width and height of an ``min_dims[field_name,300,150]``
uploaded image not meet values. The first
parameter is the field name. The second is
the width, and the third is the height. Will
also fail if the file cannot be determined
to be an image. (This rule was added in
v4.6.0.)
mime_in Yes Fails if the file's mime type is not one ``mime_in[field_name,image/png,image/jpeg]``
listed in the parameters.
ext_in Yes Fails if the file's extension is not one ``ext_in[field_name,png,jpg,gif]``
Expand Down

0 comments on commit 7604c25

Please sign in to comment.