1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* This file is part of the O2System Framework package.
|
4
|
|
|
*
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE
|
6
|
|
|
* file that was distributed with this source code.
|
7
|
|
|
*
|
8
|
|
|
* @author Steeve Andrian Salim
|
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim
|
10
|
|
|
*/
|
11
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------
|
13
|
|
|
|
14
|
|
|
namespace O2System\Image;
|
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------
|
17
|
|
|
|
18
|
|
|
use O2System\Kernel\Http\Message\UploadFile;
|
19
|
|
|
|
20
|
|
|
/**
|
21
|
|
|
* Class Upload
|
22
|
|
|
*
|
23
|
|
|
* @package O2System\Image
|
24
|
|
|
*/
|
25
|
|
|
class Uploader extends \O2System\Filesystem\Handlers\Uploader
|
26
|
|
|
{
|
27
|
|
|
/**
|
28
|
|
|
* Uploader::$allowedMimes
|
29
|
|
|
*
|
30
|
|
|
* Allowed uploaded file mime types.
|
31
|
|
|
*
|
32
|
|
|
* @var array
|
33
|
|
|
*/
|
34
|
|
|
protected $allowedMimes = [
|
35
|
|
|
IMAGETYPE_GIF => 'image/gif',
|
36
|
|
|
IMAGETYPE_JPEG => 'image/jpeg',
|
37
|
|
|
IMAGETYPE_PNG => 'image/png',
|
38
|
|
|
];
|
39
|
|
|
|
40
|
|
|
/**
|
41
|
|
|
* Uploader::$allowedExtensions
|
42
|
|
|
*
|
43
|
|
|
* Allowed uploaded file extensions.
|
44
|
|
|
*
|
45
|
|
|
* @var array
|
46
|
|
|
*/
|
47
|
|
|
protected $allowedExtensions = ['.gif', '.jpg', '.jpeg', '.png'];
|
48
|
|
|
|
49
|
|
|
/**
|
50
|
|
|
* Uploader::$allowedImageSize
|
51
|
|
|
*
|
52
|
|
|
* Allowed uploaded image size.
|
53
|
|
|
*
|
54
|
|
|
* @var array
|
55
|
|
|
*/
|
56
|
|
|
protected $allowedImageSize = [
|
57
|
|
|
'width' => [
|
58
|
|
|
'min' => 0,
|
59
|
|
|
'max' => 0,
|
60
|
|
|
],
|
61
|
|
|
'height' => [
|
62
|
|
|
'min' => 0,
|
63
|
|
|
'max' => 0,
|
64
|
|
|
],
|
65
|
|
|
];
|
66
|
|
|
|
67
|
|
|
// ------------------------------------------------------------------------
|
68
|
|
|
|
69
|
|
|
/**
|
70
|
|
|
* Uploader::__construct
|
71
|
|
|
*
|
72
|
|
|
* @param array $config
|
73
|
|
|
*
|
74
|
|
|
* @throws \O2System\Spl\Exceptions\Logic\BadFunctionCall\BadDependencyCallException
|
75
|
|
|
*/
|
76
|
|
|
public function __construct(array $config = [])
|
77
|
|
|
{
|
78
|
|
|
language()
|
79
|
|
|
->addFilePath(__DIR__ . DIRECTORY_SEPARATOR)
|
80
|
|
|
->loadFile('image');
|
81
|
|
|
|
82
|
|
|
parent::__construct($config);
|
83
|
|
|
}
|
84
|
|
|
|
85
|
|
|
// ------------------------------------------------------------------------
|
86
|
|
|
|
87
|
|
|
/**
|
88
|
|
|
* Uploader::setMinWidthSize
|
89
|
|
|
*
|
90
|
|
|
* @param int $size
|
91
|
|
|
*
|
92
|
|
|
* @return static
|
93
|
|
|
*/
|
94
|
|
|
public function setMinWidthSize($size)
|
95
|
|
|
{
|
96
|
|
|
$this->allowedImageSize[ 'width' ][ 'min' ] = (int)$size;
|
97
|
|
|
|
98
|
|
|
return $this;
|
99
|
|
|
}
|
100
|
|
|
|
101
|
|
|
// ------------------------------------------------------------------------
|
102
|
|
|
|
103
|
|
|
/**
|
104
|
|
|
* Uploader::setMaxWidthSize
|
105
|
|
|
*
|
106
|
|
|
* @param int $size
|
107
|
|
|
*
|
108
|
|
|
* @return static
|
109
|
|
|
*/
|
110
|
|
|
public function setMaxWidthSize($size)
|
111
|
|
|
{
|
112
|
|
|
$this->allowedImageSize[ 'width' ][ 'max' ] = (int)$size;
|
113
|
|
|
|
114
|
|
|
return $this;
|
115
|
|
|
}
|
116
|
|
|
|
117
|
|
|
// ------------------------------------------------------------------------
|
118
|
|
|
|
119
|
|
|
/**
|
120
|
|
|
* Uploader::setMinHeightSize
|
121
|
|
|
*
|
122
|
|
|
* @param int $size
|
123
|
|
|
*
|
124
|
|
|
* @return static
|
125
|
|
|
*/
|
126
|
|
|
public function setMinHeightSize($size)
|
127
|
|
|
{
|
128
|
|
|
$this->allowedImageSize[ 'height' ][ 'min' ] = (int)$size;
|
129
|
|
|
|
130
|
|
|
return $this;
|
131
|
|
|
}
|
132
|
|
|
|
133
|
|
|
// ------------------------------------------------------------------------
|
134
|
|
|
|
135
|
|
|
/**
|
136
|
|
|
* Uploader::setMaxHeightSize
|
137
|
|
|
*
|
138
|
|
|
* @param int $size
|
139
|
|
|
*
|
140
|
|
|
* @return static
|
141
|
|
|
*/
|
142
|
|
|
public function setMaxHeightSize($size)
|
143
|
|
|
{
|
144
|
|
|
$this->allowedImageSize[ 'height' ][ 'max' ] = (int)$size;
|
145
|
|
|
|
146
|
|
|
return $this;
|
147
|
|
|
}
|
148
|
|
|
|
149
|
|
|
// ------------------------------------------------------------------------
|
150
|
|
|
|
151
|
|
|
/**
|
152
|
|
|
* Uploader::validate
|
153
|
|
|
*
|
154
|
|
|
* @param \O2System\Kernel\Http\Message\UploadFile $file
|
155
|
|
|
*
|
156
|
|
|
* @return bool
|
157
|
|
|
*/
|
158
|
|
|
protected function validate(UploadFile $file)
|
159
|
|
|
{
|
160
|
|
|
if (parent::validate($file)) {
|
161
|
|
|
|
162
|
|
|
$info = getimagesize($file->getFileTemp());
|
163
|
|
|
$width = $info[ 0 ];
|
164
|
|
|
$height = $info[ 1 ];
|
165
|
|
|
|
166
|
|
|
/* Validate width min size */
|
167
|
|
|
if ($this->allowedImageSize[ 'width' ][ 'min' ] > 0) {
|
168
|
|
|
if ($width < $this->allowedImageSize[ 'width' ][ 'min' ]) {
|
169
|
|
|
$this->errors[] = language()->getLine(
|
170
|
|
|
'IMAGE_E_ALLOWED_MIN_WIDTH_SIZE',
|
171
|
|
|
[$this->allowedFileSize[ 'min' ], $width]
|
172
|
|
|
);
|
173
|
|
|
}
|
174
|
|
|
}
|
175
|
|
|
|
176
|
|
|
/* Validate width max size */
|
177
|
|
|
if ($this->allowedImageSize[ 'width' ][ 'max' ] > 0) {
|
178
|
|
|
if ($width > $this->allowedImageSize[ 'width' ][ 'max' ]) {
|
179
|
|
|
$this->errors[] = language()->getLine(
|
180
|
|
|
'IMAGE_E_ALLOWED_MAX_WIDTH_SIZE',
|
181
|
|
|
[$this->allowedFileSize[ 'max' ], $width]
|
182
|
|
|
);
|
183
|
|
|
}
|
184
|
|
|
}
|
185
|
|
|
|
186
|
|
|
/* Validate height min size */
|
187
|
|
|
if ($this->allowedImageSize[ 'height' ][ 'min' ] > 0) {
|
188
|
|
|
if ($height < $this->allowedImageSize[ 'width' ][ 'min' ]) {
|
189
|
|
|
$this->errors[] = language()->getLine(
|
190
|
|
|
'IMAGE_E_ALLOWED_MIN_HEIGHT_SIZE',
|
191
|
|
|
[$this->allowedFileSize[ 'min' ], $height]
|
192
|
|
|
);
|
193
|
|
|
}
|
194
|
|
|
}
|
195
|
|
|
|
196
|
|
|
/* Validate height max size */
|
197
|
|
|
if ($this->allowedImageSize[ 'height' ][ 'max' ] > 0) {
|
198
|
|
|
if ($height > $this->allowedImageSize[ 'width' ][ 'max' ]) {
|
199
|
|
|
$this->errors[] = language()->getLine(
|
200
|
|
|
'IMAGE_E_ALLOWED_MAX_HEIGHT_SIZE',
|
201
|
|
|
[$this->allowedFileSize[ 'max' ], $height]
|
202
|
|
|
);
|
203
|
|
|
}
|
204
|
|
|
}
|
205
|
|
|
}
|
206
|
|
|
|
207
|
|
|
if (count($this->errors) == 0) {
|
208
|
|
|
return true;
|
209
|
|
|
}
|
210
|
|
|
|
211
|
|
|
return false;
|
212
|
|
|
}
|
213
|
|
|
} |