|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @link http://www.yiiframework.com/ |
|
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
|
5
|
|
|
* @license http://www.yiiframework.com/license/ |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Yiisoft\Validator\Rule; |
|
9
|
|
|
|
|
10
|
|
|
use yii\helpers\Yii; |
|
|
|
|
|
|
11
|
|
|
use yii\http\UploadedFile; |
|
|
|
|
|
|
12
|
|
|
use Yiisoft\Validator\Result; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* ImageValidator verifies if an attribute is receiving a valid image. |
|
16
|
|
|
*/ |
|
17
|
|
|
class Image extends File |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var string the error message used when the uploaded file is not an image. |
|
21
|
|
|
* You may use the following tokens in the message: |
|
22
|
|
|
* |
|
23
|
|
|
* - {attribute}: the attribute name |
|
24
|
|
|
* - {file}: the uploaded file name |
|
25
|
|
|
*/ |
|
26
|
|
|
public $notImage; |
|
27
|
|
|
/** |
|
28
|
|
|
* @var int the minimum width in pixels. |
|
29
|
|
|
* Defaults to null, meaning no limit. |
|
30
|
|
|
* @see underWidth for the customized message used when image width is too small. |
|
31
|
|
|
*/ |
|
32
|
|
|
public $minWidth; |
|
33
|
|
|
/** |
|
34
|
|
|
* @var int the maximum width in pixels. |
|
35
|
|
|
* Defaults to null, meaning no limit. |
|
36
|
|
|
* @see overWidth for the customized message used when image width is too big. |
|
37
|
|
|
*/ |
|
38
|
|
|
public $maxWidth; |
|
39
|
|
|
/** |
|
40
|
|
|
* @var int the minimum height in pixels. |
|
41
|
|
|
* Defaults to null, meaning no limit. |
|
42
|
|
|
* @see underHeight for the customized message used when image height is too small. |
|
43
|
|
|
*/ |
|
44
|
|
|
public $minHeight; |
|
45
|
|
|
/** |
|
46
|
|
|
* @var int the maximum width in pixels. |
|
47
|
|
|
* Defaults to null, meaning no limit. |
|
48
|
|
|
* @see overWidth for the customized message used when image height is too big. |
|
49
|
|
|
*/ |
|
50
|
|
|
public $maxHeight; |
|
51
|
|
|
/** |
|
52
|
|
|
* @var string the error message used when the image is under [[minWidth]]. |
|
53
|
|
|
* You may use the following tokens in the message: |
|
54
|
|
|
* |
|
55
|
|
|
* - {attribute}: the attribute name |
|
56
|
|
|
* - {file}: the uploaded file name |
|
57
|
|
|
* - {limit}: the value of [[minWidth]] |
|
58
|
|
|
*/ |
|
59
|
|
|
public $underWidth; |
|
60
|
|
|
/** |
|
61
|
|
|
* @var string the error message used when the image is over [[maxWidth]]. |
|
62
|
|
|
* You may use the following tokens in the message: |
|
63
|
|
|
* |
|
64
|
|
|
* - {attribute}: the attribute name |
|
65
|
|
|
* - {file}: the uploaded file name |
|
66
|
|
|
* - {limit}: the value of [[maxWidth]] |
|
67
|
|
|
*/ |
|
68
|
|
|
public $overWidth; |
|
69
|
|
|
/** |
|
70
|
|
|
* @var string the error message used when the image is under [[minHeight]]. |
|
71
|
|
|
* You may use the following tokens in the message: |
|
72
|
|
|
* |
|
73
|
|
|
* - {attribute}: the attribute name |
|
74
|
|
|
* - {file}: the uploaded file name |
|
75
|
|
|
* - {limit}: the value of [[minHeight]] |
|
76
|
|
|
*/ |
|
77
|
|
|
public $underHeight; |
|
78
|
|
|
/** |
|
79
|
|
|
* @var string the error message used when the image is over [[maxHeight]]. |
|
80
|
|
|
* You may use the following tokens in the message: |
|
81
|
|
|
* |
|
82
|
|
|
* - {attribute}: the attribute name |
|
83
|
|
|
* - {file}: the uploaded file name |
|
84
|
|
|
* - {limit}: the value of [[maxHeight]] |
|
85
|
|
|
*/ |
|
86
|
|
|
public $overHeight; |
|
87
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
public function init(): void |
|
90
|
|
|
{ |
|
91
|
|
|
parent::init(); |
|
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
if ($this->notImage === null) { |
|
94
|
|
|
$this->notImage = $this->formatMessage('The file "{file}" is not an image.'); |
|
95
|
|
|
} |
|
96
|
|
|
if ($this->underWidth === null) { |
|
97
|
|
|
$this->underWidth = $this->formatMessage('The image "{file}" is too small. The width cannot be smaller than {limit, number} {limit, plural, one{pixel} other{pixels}}.'); |
|
98
|
|
|
} |
|
99
|
|
|
if ($this->underHeight === null) { |
|
100
|
|
|
$this->underHeight = $this->formatMessage('The image "{file}" is too small. The height cannot be smaller than {limit, number} {limit, plural, one{pixel} other{pixels}}.'); |
|
101
|
|
|
} |
|
102
|
|
|
if ($this->overWidth === null) { |
|
103
|
|
|
$this->overWidth = $this->formatMessage('The image "{file}" is too large. The width cannot be larger than {limit, number} {limit, plural, one{pixel} other{pixels}}.'); |
|
104
|
|
|
} |
|
105
|
|
|
if ($this->overHeight === null) { |
|
106
|
|
|
$this->overHeight = $this->formatMessage('The image "{file}" is too large. The height cannot be larger than {limit, number} {limit, plural, one{pixel} other{pixels}}.'); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function validateValue($value): Result |
|
111
|
|
|
{ |
|
112
|
|
|
$result = parent::validateValue($value); |
|
113
|
|
|
|
|
114
|
|
|
return empty($result) ? $this->validateImage($value) : $result; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Validates an image file. |
|
119
|
|
|
* @param UploadedFile $image uploaded file passed to check against a set of rules |
|
120
|
|
|
* @return array|null the error message and the parameters to be inserted into the error message. |
|
121
|
|
|
* Null should be returned if the data is valid. |
|
122
|
|
|
*/ |
|
123
|
|
|
protected function validateImage($image) |
|
124
|
|
|
{ |
|
125
|
|
|
if (false === ($imageInfo = getimagesize($image->tempFilename))) { |
|
126
|
|
|
return [$this->notImage, ['file' => $image->name]]; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
[$width, $height] = $imageInfo; |
|
130
|
|
|
|
|
131
|
|
|
if ($width == 0 || $height == 0) { |
|
132
|
|
|
return [$this->notImage, ['file' => $image->getClientFilename()]]; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
if ($this->minWidth !== null && $width < $this->minWidth) { |
|
136
|
|
|
return [$this->underWidth, ['file' => $image->getClientFilename(), 'limit' => $this->minWidth]]; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
if ($this->minHeight !== null && $height < $this->minHeight) { |
|
140
|
|
|
return [$this->underHeight, ['file' => $image->getClientFilename(), 'limit' => $this->minHeight]]; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
if ($this->maxWidth !== null && $width > $this->maxWidth) { |
|
144
|
|
|
return [$this->overWidth, ['file' => $image->getClientFilename(), 'limit' => $this->maxWidth]]; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
if ($this->maxHeight !== null && $height > $this->maxHeight) { |
|
148
|
|
|
return [$this->overHeight, ['file' => $image->getClientFilename(), 'limit' => $this->maxHeight]]; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
return null; |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths