1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lavibi\Popoya; |
4
|
|
|
|
5
|
|
|
use Psr\Http\Message\StreamInterface; |
6
|
|
|
use Psr\Http\Message\UploadedFileInterface; |
7
|
|
|
|
8
|
|
|
class Image extends AbstractValidator |
9
|
|
|
{ |
10
|
|
|
const MAX_WIDTH = 'max_width'; |
11
|
|
|
const MAX_HEIGHT = 'max_height'; |
12
|
|
|
const NOT_VALID_IMAGE = 'not_valid_image'; |
13
|
|
|
|
14
|
1 |
|
public function hasMaxWidth($width) |
15
|
|
|
{ |
16
|
1 |
|
$this->options['size']['width'] = (int) $width; |
17
|
|
|
|
18
|
1 |
|
return $this; |
19
|
|
|
} |
20
|
|
|
|
21
|
1 |
|
public function hasMaxHeight($height) |
22
|
|
|
{ |
23
|
1 |
|
$this->options['size']['height'] = (int) $height; |
24
|
|
|
|
25
|
1 |
|
return $this; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param array $value |
30
|
|
|
* |
31
|
|
|
* @return bool |
32
|
|
|
*/ |
33
|
5 |
|
public function isValid($value) |
34
|
|
|
{ |
35
|
5 |
|
$this->value = $value; |
36
|
|
|
|
37
|
5 |
|
$image = $this->isImage(); |
38
|
|
|
|
39
|
5 |
|
if (!$image) { |
40
|
2 |
|
return false; |
41
|
|
|
} |
42
|
|
|
|
43
|
3 |
|
if (!$this->isValidImageMaxSize($image['width'], $image['height'])) { |
44
|
1 |
|
return false; |
45
|
|
|
} |
46
|
|
|
|
47
|
3 |
|
$this->standardValue['image'] = $image; |
48
|
|
|
|
49
|
3 |
|
return true; |
50
|
|
|
} |
51
|
|
|
|
52
|
5 |
|
protected function isImage() |
53
|
|
|
{ |
54
|
5 |
|
$file = $this->getFile(); |
55
|
|
|
|
56
|
5 |
|
if ($file === false) { |
57
|
1 |
|
$this->setError(static::NOT_VALID_IMAGE); |
58
|
1 |
|
return false; |
59
|
|
|
} |
60
|
|
|
|
61
|
5 |
|
$size = @getimagesize($file); |
|
|
|
|
62
|
|
|
|
63
|
5 |
|
if ($size === false) { |
64
|
2 |
|
$this->setError(static::NOT_VALID_IMAGE); |
65
|
2 |
|
return false; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return [ |
69
|
3 |
|
'width' => $size[0], |
70
|
3 |
|
'height' => $size[1], |
71
|
|
|
]; |
72
|
|
|
} |
73
|
|
|
|
74
|
5 |
|
protected function getFile() |
75
|
|
|
{ |
76
|
5 |
|
$file = $this->value; |
77
|
|
|
|
78
|
5 |
|
if ($this->value instanceof UploadedFileInterface) { |
79
|
2 |
|
$file = $this->value->getStream(); |
80
|
|
|
} |
81
|
|
|
|
82
|
5 |
|
if (is_array($this->value) && isset($this->value['tmp_name'])) { |
83
|
3 |
|
$file = $this->value['tmp_name']; |
84
|
|
|
} |
85
|
|
|
|
86
|
5 |
|
if (is_string($file)) { |
87
|
3 |
|
return $file; |
88
|
|
|
} |
89
|
|
|
|
90
|
3 |
|
if ($file instanceof StreamInterface) { |
91
|
2 |
|
return $file->getMetadata('uri'); |
92
|
|
|
} |
93
|
|
|
|
94
|
1 |
|
return false; |
95
|
|
|
} |
96
|
|
|
|
97
|
3 |
|
protected function isValidImageMaxSize($width, $height) |
98
|
|
|
{ |
99
|
3 |
|
if ($this->options['size']['width'] !== 0 && $width > $this->options['size']['width']) { |
100
|
1 |
|
$this->setError(self::MAX_WIDTH); |
101
|
1 |
|
return false; |
102
|
|
|
} |
103
|
|
|
|
104
|
3 |
|
if ($this->options['size']['height'] !== 0 && $height > $this->options['size']['height']) { |
105
|
1 |
|
$this->setError(self::MAX_HEIGHT); |
106
|
1 |
|
return false; |
107
|
|
|
} |
108
|
|
|
|
109
|
3 |
|
return true; |
110
|
|
|
} |
111
|
|
|
|
112
|
5 |
|
protected function init() |
113
|
|
|
{ |
114
|
5 |
|
parent::init(); |
115
|
|
|
|
116
|
5 |
|
$this->defaultOptions['size'] = [ |
117
|
|
|
'width' => 0, |
118
|
|
|
'height' => 0, |
119
|
|
|
]; |
120
|
|
|
|
121
|
5 |
|
$this->messages[static::NOT_VALID_IMAGE] = 'Given data was not image type.'; |
122
|
5 |
|
$this->messages[static::MAX_WIDTH] = 'Image width is too large.'; |
123
|
5 |
|
$this->messages[static::MAX_HEIGHT] = 'Image height is too large.'; |
124
|
5 |
|
} |
125
|
|
|
} |
126
|
|
|
|