1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Manage upload |
5
|
|
|
* |
6
|
|
|
* @category lib |
7
|
|
|
* @author Judicaël Paquet <[email protected]> |
8
|
|
|
* @copyright Copyright (c) 2013-2014 PAQUET Judicaël FR Inc. (https://github.com/las93) |
9
|
|
|
* @license https://github.com/las93/venus2/blob/master/LICENSE.md Tout droit réservé à PAQUET Judicaël |
10
|
|
|
* @version Release: 1.0.0 |
11
|
|
|
* @filesource https://github.com/las93/venus2 |
12
|
|
|
* @link https://github.com/las93 |
13
|
|
|
* @since 1.0 |
14
|
|
|
*/ |
15
|
|
|
namespace Venus\lib; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* This class manage the upload |
19
|
|
|
* |
20
|
|
|
* @category lib |
21
|
|
|
* @author Judicaël Paquet <[email protected]> |
22
|
|
|
* @copyright Copyright (c) 2013-2014 PAQUET Judicaël FR Inc. (https://github.com/las93) |
23
|
|
|
* @license https://github.com/las93/venus2/blob/master/LICENSE.md Tout droit réservé à PAQUET Judicaël |
24
|
|
|
* @version Release: 1.0.0 |
25
|
|
|
* @filesource https://github.com/las93/venus2 |
26
|
|
|
* @link https://github.com/las93 |
27
|
|
|
* @since 1.0 |
28
|
|
|
*/ |
29
|
|
|
class Upload |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* max size of upload |
33
|
|
|
* @var int |
34
|
|
|
*/ |
35
|
|
|
private $_iMaxFile = 100000; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* error while upload |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
private $_sError = ''; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* array of extensions allowed |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
private $_aAllowExtension = array(); |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* final extension |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
private $_sExtension = null; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* height of image |
57
|
|
|
* @var int |
58
|
|
|
*/ |
59
|
|
|
private $_iHeight = null; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* width of image |
63
|
|
|
* @var int |
64
|
|
|
*/ |
65
|
|
|
private $_iWidth = null; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* image name |
69
|
|
|
* @var string |
70
|
|
|
*/ |
71
|
|
|
private $_sName = null; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* if the crop or resize |
75
|
|
|
* @var bool |
76
|
|
|
*/ |
77
|
|
|
private $_bProportion = true; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* get an upload file |
81
|
|
|
* |
82
|
|
|
* @access public |
83
|
|
|
* @param string $sFile |
84
|
|
|
* @return bool|object |
85
|
|
|
*/ |
86
|
|
|
public function upload(string $sFile) |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
if ($_FILES[$sFile]['error'] > 0) { |
89
|
|
|
|
90
|
|
|
$this->_sError = "Error while the upload"; |
91
|
|
|
return false; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if ($_FILES[$sFile]['size'] > $this->_iMaxFile) { |
95
|
|
|
|
96
|
|
|
$this->_sError = "The file is too big"; |
97
|
|
|
return false; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$sExtension = strtolower(substr(strrchr($_FILES[$sFile]['name'], '.'), 1)); |
101
|
|
|
|
102
|
|
|
if (count($this->_aAllowExtension) > 0 && !in_array($sExtension ,$this->_aAllowExtension)) { |
103
|
|
|
|
104
|
|
|
$this->_sError = "The extension is not good"; |
105
|
|
|
return false; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$sPath = str_replace('bundles'.DIRECTORY_SEPARATOR.'lib', |
109
|
|
|
'data'.DIRECTORY_SEPARATOR.'upload'.DIRECTORY_SEPARATOR, __DIR__); |
110
|
|
|
|
111
|
|
|
if ($this->_sExtension === null) { $this->setExtension($sExtension); } |
112
|
|
|
|
113
|
|
|
if ($this->_sName) { $sName = $sPath.$this->_sName.'.'.$this->_sExtension; } |
114
|
|
|
else { $sName = $sPath.md5(uniqid(rand(), true)).'.'.$this->_sExtension;} |
115
|
|
|
|
116
|
|
|
if ($this->_bProportion === true && ($this->_iWidth || $this->_iHeight)) { |
117
|
|
|
|
118
|
|
|
$aImageSizes = getimagesize($_FILES[$sFile]['tmp_name']); |
119
|
|
|
|
120
|
|
|
$fRatio = min($aImageSizes[0] / $this->_iWidth, $aImageSizes[1] / $this->_iHeight); |
121
|
|
|
$iHeight = $aImageSizes[1] / $fRatio; |
122
|
|
|
$iWidth = $aImageSizes[0] / $fRatio; |
123
|
|
|
$fY = ($iHeight - $this->_iHeight) / 2 * $fRatio; |
124
|
|
|
$fX = ($iWidth - $this->_iWidth) / 2 * $fRatio; |
125
|
|
|
|
126
|
|
|
$rNewImage = imagecreatefromjpeg($_FILES[$sFile]['tmp_name']); |
127
|
|
|
|
128
|
|
|
$rNewImgTrueColor = imagecreatetruecolor($this->_iWidth , $this->_iHeight); |
129
|
|
|
imagecopyresampled($rNewImgTrueColor , $rNewImage, 0, 0, $fX, $fY, $this->_iWidth, $this->_iHeight, $iWidth * $fRatio - $fX * 2, $iHeight * $fRatio - $fY * 2); |
130
|
|
|
|
131
|
|
|
imagejpeg($rNewImgTrueColor , $sName, 100); |
132
|
|
|
} |
133
|
|
|
else { |
134
|
|
|
|
135
|
|
|
$bResultat = move_uploaded_file($_FILES[$sFile]['tmp_name'], $sName); |
136
|
|
|
|
137
|
|
|
if ($bResultat) { return true; } |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
|
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* set max size of upload |
145
|
|
|
* |
146
|
|
|
* @access public |
147
|
|
|
* @param int $iMaxFile |
148
|
|
|
* @return \Venus\lib\Upload |
149
|
|
|
*/ |
150
|
|
|
public function setMaxSize(int $iMaxFile) : Upload |
151
|
|
|
{ |
152
|
|
|
$this->_iMaxFile = $iMaxFile; |
153
|
|
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* set allow extensions to upload |
158
|
|
|
* |
159
|
|
|
* @access public |
160
|
|
|
* @param array $aAllowExtension |
161
|
|
|
* @return \Venus\lib\Upload |
162
|
|
|
*/ |
163
|
|
|
public function setAllowExtension(array $aAllowExtension) : Upload |
164
|
|
|
{ |
165
|
|
|
$this->_aAllowExtension = $aAllowExtension; |
166
|
|
|
return $this; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* set extension of final file |
171
|
|
|
* |
172
|
|
|
* @access public |
173
|
|
|
* @param string $sExtension |
174
|
|
|
* @return \Venus\lib\Upload |
175
|
|
|
*/ |
176
|
|
|
public function setExtension(string $sExtension) : Upload |
177
|
|
|
{ |
178
|
|
|
$this->_sExtension = $sExtension; |
179
|
|
|
return $this; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* set height of image |
184
|
|
|
* |
185
|
|
|
* @access public |
186
|
|
|
* @param int $iHeight |
187
|
|
|
* @return \Venus\lib\Upload |
188
|
|
|
*/ |
189
|
|
|
public function setHeight(int $iHeight) : Upload |
190
|
|
|
{ |
191
|
|
|
$this->_iHeight = $iHeight; |
192
|
|
|
return $this; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* set width of image |
197
|
|
|
* |
198
|
|
|
* @access public |
199
|
|
|
* @param int $iWidth |
200
|
|
|
* @return \Venus\lib\Upload |
201
|
|
|
*/ |
202
|
|
|
public function setWidth(int $iWidth) : Upload |
203
|
|
|
{ |
204
|
|
|
$this->_iWidth = $iWidth; |
205
|
|
|
return $this; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* set name of image |
210
|
|
|
* |
211
|
|
|
* @access public |
212
|
|
|
* @param string $sName |
213
|
|
|
* @return \Venus\lib\Upload |
214
|
|
|
*/ |
215
|
|
|
public function setName(string $sName) : Upload |
216
|
|
|
{ |
217
|
|
|
$this->_sName = $sName; |
218
|
|
|
return $this; |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|