-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathImageWidth.php
More file actions
37 lines (29 loc) · 1001 Bytes
/
ImageWidth.php
File metadata and controls
37 lines (29 loc) · 1001 Bytes
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
<?php
declare(strict_types=1);
namespace Sirius\Validation\Rule\File;
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 (!file_exists($value)) {
$this->success = false;
} else {
$imageInfo = getimagesize($value);
$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;
}
}