This repository has been archived by the owner on Sep 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
File.php
296 lines (267 loc) · 8.01 KB
/
File.php
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<?php
/**
* This file is part of the Upload Manipulation package.
*
* @link http://github.com/fernandozueet/upload-and-image-manipulation
* @copyright 2018
* @license MIT License
* @author Fernando Zueet <[email protected]>
*/
namespace Upload;
use \Upload\Core;
class File
{
/*-------------------------------------------------------------------------------------
* Attributes
*-------------------------------------------------------------------------------------*/
/**
* Mime type permitted
*
* @var array
*/
private $mimeTypesPermited = ['image/gif',
'image/jpeg',
'image/png',
'application/msword',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'application/json',
'application/pdf',
'application/vnd.ms-powerpoint',
'application/vnd.openxmlformats-officedocument.presentationml.presentation',
'text/plain',
'application/vnd.ms-excel',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'text/csv',
'image/webp'
];
/*-------------------------------------------------------------------------------------
* GET and SET methods
*-------------------------------------------------------------------------------------*/
/**
* Get mime type permitted
*
* @return array
*/
public function getMimeTypesPermited() : array
{
return $this->mimeTypesPermited;
}
/*-------------------------------------------------------------------------------------
* Other Methods
*-------------------------------------------------------------------------------------*/
/**
* Reorganized file array
*
* @param array $file
* @return void
*/
public function reorganizedFile(array $file)
{
$newArray = [];
foreach ($file as $key => $all) {
foreach ($all as $i => $val) {
$newArray[$i][$key] = $val;
}
}
if (!$newArray) {
$fileNew[0] = $file;
return $fileNew;
}
return $newArray;
}
/**
* Get new name file
*
* @return string
*/
public function newNameFile() : string
{
return md5(uniqid(rand(), true));
}
/**
* Get name file
*
* @param array $file
* @return string
*/
public function getNameFile(array $file) : string
{
if (!$file['name']) {
return $file['tmp_name'];
}
return $file['name'];
}
/**
* Get type file
*
* @param array $file
* @return string
*/
public function getTypeFile(array $file) : string
{
if ($file['type']) {
$type = $file['type'];
} else {
$size = getimagesize($file['tmp_name']);
$type = $size['mime'] ?? "";
}
return $type;
}
/**
* Get path file
*
* @param array $file
* @return string
*/
public function getTmpFile(array $file) : string
{
return $file['tmp_name'] ?? "";
}
/**
* Get size file
*
* @param array $file
* @return int
*/
public function getSizeFile(array $file) : int
{
if (!$file['size']) {
$ch = curl_init($this->getTmpFile($file));
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$data = curl_exec($ch);
curl_close($ch);
if (preg_match('/Content-Length: (\d+)/', $data, $matches)) {
return (int) $matches[1] ?? 0;
}
return 0;
}
return $file['size'];
}
/**
* Get extension file
*
* @param string $file
* @return string
*/
public function getExtFile(string $file) : string
{
return pathinfo($file, PATHINFO_EXTENSION) ?? "";
}
/**
* Get dimension image
*
* @param array $file
* @return array
*/
public function getDimensionImage(array $file) : array
{
$ext = $this->getExtFile($this->getTmpFile($file));
//I deal with webp format
if($ext == 'webp') {
$img = imagecreatefromwebp($this->getTmpFile($file));
return [
'width' => imagesx($img),
'height' => imagesy($img),
];
//Other cases
}else{
@list($width, $height, $type, $attr) = getimagesize($this->getTmpFile($file));
if ($width && $height) {
return [
'width' => $width,
'height' => $height,
'type' => $type,
'attr' => $attr
];
}
}
return [];
}
/**
* Get info file exif
*
* @param array $file
* @return array
*/
public function getInfoFile(array $file) : array
{
$ext = $this->getExtFile($this->getTmpFile($file));
//Other cases
if($ext != 'webp') {
$exif = exif_read_data($this->getTmpFile($file), 0, true);
if ($exif) {
if (isset($exif['IFD0']['Artist'])) {
$array['Artist'] = explode(';', $exif['IFD0']['Artist']);
}
if (isset($exif['COMPUTED']['Copyright'])) {
$array['Copyright'] = $exif['COMPUTED']['Copyright'];
}
if (isset($exif['FILE']['FileDateTime'])) {
$array['DateTime'] = date('m/d/Y H:m:s', $exif['FILE']['FileDateTime']);
}
return $array ?? [];
}
}
return [];
}
/**
* File update
*
* @param Core $container
* @param int $pos
* @param string $indice
* @param mixed $newValue
* @return void
*/
public function updateFile(Core $container, int $pos, string $indice, $newValue)
{
$file = $container->getFile();
$file[$pos][$indice] = $newValue;
$container->setFile($file);
return $file;
}
/**
* Prepare new file
*
* @return void
*/
public function prepareFile(Core $container)
{
//if empty file return
if (!$container->getFile()) {
return;
}
//reorganized file
$fileNew = $this->reorganizedFile($container->getFile());
//for file
for ($i=0; $i < count($fileNew); $i++) {
//size
$fileNew[$i]['size'] = $this->getSizeFile($fileNew[$i]);
//name
$fileNew[$i]['name'] = $this->getNameFile($fileNew[$i]);
//mime type
$fileNew[$i]['type'] = $this->getTypeFile($fileNew[$i]);
//width, height image dimension
$isImage = $this->getDimensionImage($fileNew[$i]);
if ($isImage) {
$fileNew[$i]['width'] = $isImage['width'];
$fileNew[$i]['height'] = $isImage['height'];
}
//get extension file
$ext = $this->getExtFile($this->getNameFile($fileNew[$i], PATHINFO_EXTENSION));
$fileNew[$i]['new_name'] = $this->newNameFile().'.'.$ext;
//get extension
$fileNew[$i]['extension'] = $ext;
//get info exif
$infoFile = $this->getInfoFile($fileNew[$i]);
if ($infoFile) {
$fileNew[$i]['info_exif'] = $infoFile;
}
}
//set new file
$container->setFile($fileNew);
}
}