forked from ziggi/zimg-host
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.class.php
184 lines (136 loc) · 5.28 KB
/
upload.class.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
<?php
class Upload {
private $_allowed_types = array(
IMAGETYPE_GIF => array('function_postfix' => 'gif', 'file_format' => 'gif'),
IMAGETYPE_JPEG => array('function_postfix' => 'jpeg', 'file_format' => 'jpg'),
IMAGETYPE_PNG => array('function_postfix' => 'png', 'file_format' => 'png'),
IMAGETYPE_BMP => array('function_postfix' => 'wbmp', 'file_format' => 'bmp'),
);
const MAX_FILE_SIZE = '2M';
public function upload_urls($urls_array) {
$files_count = count($urls_array);
$array_result = array();
for ($i = 0; $i < $files_count; $i++) {
$array_result[$i]['name'] = $urls_array[$i];
$array_result[$i]['error']['upload'] = 0;
$array_result[$i]['error']['type'] = 0;
$array_result[$i]['error']['size'] = 0;
$temp_name = tempnam("/tmp", "zmg");
$is_copied = copy($urls_array[$i], $temp_name);
if (!$is_copied) {
$array_result[$i]['error']['upload'] = 1;
}
list($width, $height, $type) = getimagesize($temp_name);
$array_result[$i]['type'] = $type;
$array_result[$i]['size']['width'] = $width;
$array_result[$i]['size']['height'] = $height;
if (!$this->is_support_type($array_result[$i]['type'])) {
$array_result[$i]['error']['upload'] = 1;
$array_result[$i]['error']['type'] = 1;
}
$array_result[$i]['size']['filesize'] = filesize($temp_name);
if (!$this->is_support_size($array_result[$i]['size']['filesize'])) {
$array_result[$i]['error']['upload'] = 1;
$array_result[$i]['error']['size'] = 1;
}
if ($array_result[$i]['error']['upload'] == 1) {
continue;
}
$new_name = md5(microtime() . $urls_array[$i] . rand(0, 9999)) . '.' . $this->_allowed_types[ $array_result[$i]['type'] ]['file_format'];
copy($temp_name, __DIR__ . '/file/' . $new_name);
unlink($temp_name);
$this->create_thumbnail_image(__DIR__ . '/file/' . $new_name, __DIR__ . '/file/thumbnail/' . $new_name, 420);
$array_result[$i]['url'] = $new_name;
}
echo json_encode($array_result);
}
public function upload_files($files_array) {
$files_count = count($files_array['name']);
$files = $this->restruct_input_array($files_array, $files_count);
$array_result = array();
for ($i = 0; $i < $files_count; $i++) {
$array_result[$i]['name'] = $files[$i]['name'];
list($width, $height, $type) = getimagesize($files[$i]['tmp_name']);
$array_result[$i]['type'] = $type;
$array_result[$i]['size']['width'] = $width;
$array_result[$i]['size']['height'] = $height;
$array_result[$i]['size']['filesize'] = $files[$i]['size'];
$array_result[$i]['error']['upload'] = 0;
$array_result[$i]['error']['type'] = 0;
$array_result[$i]['error']['size'] = 0;
if ($files[$i]['error'] === 1) {
$array_result[$i]['error']['upload'] = 1;
}
if (!$this->is_support_type($type)) {
$array_result[$i]['error']['upload'] = 1;
$array_result[$i]['error']['type'] = 1;
}
if (!$this->is_support_size($files[$i]['size'])) {
$array_result[$i]['error']['upload'] = 1;
$array_result[$i]['error']['size'] = 1;
}
if ($array_result[$i]['error']['upload'] == 1) {
continue;
}
$new_name = md5(microtime() . $files[$i]['name'] . $files[$i]['tmp_name'] . rand(0, 9999)) . '.' . $this->_allowed_types[$type]['file_format'];
move_uploaded_file($files[$i]['tmp_name'], __DIR__ . '/file/' . $new_name);
$this->create_thumbnail_image(__DIR__ . '/file/' . $new_name, __DIR__ . '/file/thumbnail/' . $new_name, 420);
$array_result[$i]['url'] = $new_name;
}
echo json_encode($array_result);
}
public function restruct_input_array($files_array, $files_count) {
$result_array = array();
$file_keys = array_keys($files_array);
for ($i = 0; $i < $files_count; $i++) {
foreach ($file_keys as $key) {
$result_array[$i][$key] = $files_array[$key][$i];
}
}
return $result_array;
}
public function is_support_size($size) {
$is_app_support_size = $size <= $this->return_bytes(self::MAX_FILE_SIZE);
$is_php_support_size = $size <= $this->return_bytes(ini_get('upload_max_filesize'));
$is_post_support_size = $size <= $this->return_bytes(ini_get('post_max_size'));
if ($size === false || !$is_app_support_size || !$is_php_support_size || !$is_post_support_size) {
return false;
}
return true;
}
public function is_support_type($type) {
if (!isset($this->_allowed_types[$type])) {
return false;
}
return true;
}
public function create_thumbnail_image($src_path, $dest_path, $newwidth) {
list($width, $height, $type) = getimagesize($src_path);
if ($newwidth > $width) {
copy($src_path, $dest_path);
return;
}
$newheight = round($newwidth * $height / $width);
$create_function = "imagecreatefrom" . $this->_allowed_types[$type]['function_postfix'];
$src_res = $create_function($src_path);
$dest_res = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($dest_res, $src_res, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
$create_function = "image" . $this->_allowed_types[$type]['function_postfix'];
$create_function($dest_res, $dest_path);
imagedestroy($dest_res);
imagedestroy($src_res);
}
public function return_bytes($val) {
$val = trim($val);
$last = strtolower($val[ strlen($val) - 1 ]);
switch ($last) {
case 'g':
$val *= 1024;
case 'm':
$val *= 1024;
case 'k':
$val *= 1024;
}
return $val;
}
}