-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathImageWidth.php
More file actions
39 lines (31 loc) · 1.14 KB
/
ImageWidth.php
File metadata and controls
39 lines (31 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
declare(strict_types=1);
namespace Sirius\Validation\Rule\Upload;
use Sirius\Validation\Rule\AbstractRule;
class ImageWidth extends AbstractRule
{
const OPTION_MAX = 'max';
const OPTION_MIN = 'min';
const MESSAGE = 'The image should be at least {min} pixels wide';
const LABELED_MESSAGE = '{label} should be at least {min} pixels wide';
protected array $options = [
self::OPTION_MAX => 1000000,
self::OPTION_MIN => 0,
];
public function validate(mixed $value, ?string $valueIdentifier = null): bool
{
$this->value = $value;
if (!is_array($value) || !isset($value['tmp_name'])) {
$this->success = false;
} elseif (!file_exists($value['tmp_name'])) {
$this->success = $value['error'] === UPLOAD_ERR_NO_FILE;
} else {
$imageInfo = getimagesize($value['tmp_name']);
$width = isset($imageInfo[0]) ? $imageInfo[0] : 0;
$this->success = $width &&
$width <= $this->options[self::OPTION_MAX] &&
$width >= $this->options[self::OPTION_MIN];
}
return $this->success;
}
}