Skip to content

Commit 7604c25

Browse files
authored
Merge pull request #8966 from christianberkman/feat-min-dims
feat: [Validation] add `min_dims` rule in FileRules
2 parents 11d0631 + 5942103 commit 7604c25

File tree

5 files changed

+92
-0
lines changed

5 files changed

+92
-0
lines changed

system/Validation/FileRules.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,4 +260,52 @@ public function max_dims(?string $blank, string $params): bool
260260

261261
return true;
262262
}
263+
264+
/**
265+
* Checks an uploaded file to verify that the dimensions are greater than
266+
* a specified dimension.
267+
*/
268+
public function min_dims(?string $blank, string $params): bool
269+
{
270+
// Grab the file name off the top of the $params
271+
// after we split it.
272+
$params = explode(',', $params);
273+
$name = array_shift($params);
274+
275+
$files = $this->request->getFileMultiple($name);
276+
if ($files === null) {
277+
$files = [$this->request->getFile($name)];
278+
}
279+
280+
foreach ($files as $file) {
281+
if ($file === null) {
282+
return false;
283+
}
284+
285+
if ($file->getError() === UPLOAD_ERR_NO_FILE) {
286+
return true;
287+
}
288+
289+
// Get Parameter sizes
290+
$minimumWidth = $params[0] ?? 0;
291+
$minimumHeight = $params[1] ?? 0;
292+
293+
// Get uploaded image size
294+
$info = getimagesize($file->getTempName());
295+
296+
if ($info === false) {
297+
// Cannot get the image size.
298+
return false;
299+
}
300+
301+
$fileWidth = $info[0];
302+
$fileHeight = $info[1];
303+
304+
if ($fileWidth < $minimumWidth || $fileHeight < $minimumHeight) {
305+
return false;
306+
}
307+
}
308+
309+
return true;
310+
}
263311
}

tests/system/Validation/FileRulesTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,24 @@ public function testMaxDimsBad(): void
230230
$this->assertFalse($this->validation->run([]));
231231
}
232232

233+
public function testMinDims(): void
234+
{
235+
$this->validation->setRules(['avatar' => 'min_dims[avatar,320,240]']);
236+
$this->assertTrue($this->validation->run([]));
237+
}
238+
239+
public function testMinDimsFail(): void
240+
{
241+
$this->validation->setRules(['avatar' => 'min_dims[avatar,800,600]']);
242+
$this->assertFalse($this->validation->run([]));
243+
}
244+
245+
public function testMinDimsBad(): void
246+
{
247+
$this->validation->setRules(['avatar' => 'min_dims[unknown,640,480]']);
248+
$this->assertFalse($this->validation->run([]));
249+
}
250+
233251
public function testIsImage(): void
234252
{
235253
$this->validation->setRules(['avatar' => 'is_image[avatar]']);

tests/system/Validation/StrictRules/FileRulesTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,24 @@ public function testMaxDimsBad(): void
231231
$this->assertFalse($this->validation->run([]));
232232
}
233233

234+
public function testMinDims(): void
235+
{
236+
$this->validation->setRules(['avatar' => 'min_dims[avatar,320,240]']);
237+
$this->assertTrue($this->validation->run([]));
238+
}
239+
240+
public function testMinDimsFail(): void
241+
{
242+
$this->validation->setRules(['avatar' => 'min_dims[avatar,800,600]']);
243+
$this->assertFalse($this->validation->run([]));
244+
}
245+
246+
public function testMinDimsBad(): void
247+
{
248+
$this->validation->setRules(['avatar' => 'min_dims[unknown,640,480]']);
249+
$this->assertFalse($this->validation->run([]));
250+
}
251+
234252
public function testIsImage(): void
235253
{
236254
$this->validation->setRules(['avatar' => 'is_image[avatar]']);

user_guide_src/source/changelogs/v4.6.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ Libraries
128128
=========
129129

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

132133
Helpers and Functions
133134
=====================

user_guide_src/source/libraries/validation.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,6 +1082,13 @@ max_dims Yes Fails if the maximum width and height of an
10821082
the width, and the third is the height. Will
10831083
also fail if the file cannot be determined
10841084
to be an image.
1085+
min_dims Yes Fails if the minimum width and height of an ``min_dims[field_name,300,150]``
1086+
uploaded image not meet values. The first
1087+
parameter is the field name. The second is
1088+
the width, and the third is the height. Will
1089+
also fail if the file cannot be determined
1090+
to be an image. (This rule was added in
1091+
v4.6.0.)
10851092
mime_in Yes Fails if the file's mime type is not one ``mime_in[field_name,image/png,image/jpeg]``
10861093
listed in the parameters.
10871094
ext_in Yes Fails if the file's extension is not one ``ext_in[field_name,png,jpg,gif]``

0 commit comments

Comments
 (0)