-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.php
218 lines (184 loc) · 6.63 KB
/
index.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
<?php
header("Access-Control-Allow-Origin: *");
require 'vendor/autoload.php';
use \Gumlet\ImageResize;
use \Gumlet\ImageResizeException;
$router = new \Bramus\Router\Router();
/* Get Method */
$router->get('/', function(){
$image_url = isset($_GET['imageUrl']) ? $_GET['imageUrl'] : null;
$width = isset($_GET['width']) ? $_GET['width'] : null;
$height = isset($_GET['height']) ? $_GET['height'] : null;
$quality = isset($_GET['quality']) ? $_GET['quality'] : null;
//Check the input for GET.
if(!$image_url){
show_error('Please provide the url of image.');
return;
}
if(!$width && !$height){
show_error('Please input width or height which you want to resize to.');
return;
}
if($width && !is_numeric($width) || $height && !is_numeric($height) || $quality && !is_numeric($quality)){
show_error('Width, Height, and Quality should be number.');
return;
}
$image_res = @file_get_contents($image_url);
$headers = parse_header($http_response_header);
//Check file size and type, if everything is OK, download it.
if(!check_file_ok($image_res, $headers)){
show_error('Input file should be an image, and the size should not larger than 500MB.');
return;
}
$folder_path = './temp-files/';
if (!is_dir($folder_path)) mkdir($folder_path, 0777, true);
$image_info = pathinfo(parse_url($image_url, PHP_URL_PATH));
$image_path = $folder_path . $image_info['basename'];
file_put_contents($image_path, $image_res);
try{
$image = new ImageResize($image_path);
if($width && $height){
$image->resizeToBestFit((int)$width, (int)$height, $allow_enlarge = TRUE);
} else {
if($width) $image->resizeToWidth((int)$width, $allow_enlarge = TRUE);
if($height) $image->resizeToHeight((int)$height, $allow_enlarge = TRUE);
}
if(preg_match('/jpe?g|webp/', $image_info['extension']) && $quality){
$image->save($image_path, null, (int)$quality);
} else {
$image->save($image_path);
}
$type = pathinfo($image_path, PATHINFO_EXTENSION);
$image_content = file_get_contents($image_path);
header("Content-Type: image/{$type}");
header('Content-disposition: inline; filename=' . $image_info['filename'] . ".$type");
header("Content-Length: " . strlen($image_content));
header("Cache-Control: public", true);
header("Pragma: public", true);
echo $image_content;
unlink($image_path);
} catch (ImageResizeException $e) {
unlink($image_path);
show_error($e->getMessage());
}
});
/* Post method */
$router->post('/', function() {
header('Content-Type: application/json');
$post_data = file_get_contents('php://input');
$image_data = json_decode($post_data, true);
//Check image url is existed.
if(!isset($image_data['imageUrl'])){
show_error('Please provide the url of image.');
return;
}
$image_url = $image_data['imageUrl'];
$image_res = @file_get_contents($image_url);
$headers = parse_header($http_response_header);
//Check file size and type, if everything is OK, download it.
if(!check_file_ok($image_res, $headers)){
show_error('Input file should be an image, and the size should not larger than 500MB.');
return;
}
$folder_path = './temp_files/';
if (!is_dir($folder_path)) mkdir($folder_path, 0777, true);
$image_info = pathinfo(parse_url($image_url, PHP_URL_PATH));
$image_path = $folder_path . pathinfo(parse_url($image_url, PHP_URL_PATH), PATHINFO_BASENAME);
file_put_contents($image_path, $image_res);
//Resize image to fit size.
try{
$width = isset($image_data['width']) ? $image_data['width'] : null;
$height = isset($image_data['height']) ? $image_data['height'] : null;
$quality = isset($image_data['quality']) ? $image_data['quality'] : null;
if(!$width && !$height){
show_error('Please input width or height which you want to resize to.');
return;
}
if($width && !is_numeric($width) || $height && !is_numeric($height) || $quality && !is_numeric($quality)){
unlink($image_path);
show_error('Width, Height, and Quality should be number.');
return;
}
$image = new ImageResize($image_path);
if($width && $height){
$image->resizeToBestFit((int)$width, (int)$height, $allow_enlarge = TRUE);
} else {
if($width) $image->resizeToWidth((int)$width, $allow_enlarge = TRUE);
if($height) $image->resizeToHeight((int)$height, $allow_enlarge = TRUE);
}
if(preg_match('/jpe?g|webp/', $image_info['extension']) && $quality){
$image->save($image_path, null, (int)$quality);
} else {
$image->save($image_path);
}
$type = pathinfo($image_path, PATHINFO_EXTENSION);
$result = [
'status' => 'Success',
'filename' => $image_info['filename'] . ".$type",
'cropped_image_data' => 'data:image/' . $type . ';base64,' . base64_encode(file_get_contents($image_path)),
];
unlink($image_path);
echo json_encode($result);
} catch (ImageResizeException $e) {
unlink($image_path);
show_error($e->getMessage());
}
});
$router->run();
/**
* Function to get formatted headers (with response code).
*
* @param array $headers The php headers to be parsed
* @link https://www.php.net/manual/en/reserved.variables.httpresponseheader.php#117203
*/
function parse_header($headers){
$head = array();
foreach( $headers as $k=>$v ) {
$t = explode( ':', $v, 2 );
if ( isset($t[1]) ) {
$head[ strtolower(trim($t[0])) ] = trim( $t[1] );
} else {
$head[] = $v;
if( preg_match("#HTTP/[0-9\.]+\s+([0-9]+)#",$v, $out) ) $head['reponse_code'] = intval( $out[1] );
}
}
return $head;
}
/**
* Function for show error mesage in JSON format.
*
* @param string $message The error message which should be displayed.
*/
function show_error($message){
$result = [
'status' => 'Failed',
'error_message' => $message,
];
echo json_encode($result);
}
/**
* Function for check received file before download it.
*
* @param string $image_url The url of image.
* @param array $headers List of response headers.
*/
function check_file_ok($image_res, $headers){
//if response code not 200, return RESPONSE CODE
if($headers['reponse_code'] != '200'){
show_error($headers[0]);
exit;
}
$finfo = new finfo(FILEINFO_MIME_TYPE);
$mime_type = $finfo->buffer($image_res);
$file_size = isset($headers['content-length']) ? $headers['content-length'] : -1;
$file_type = $mime_type !== false ? $mime_type : $headers['content-type'];
//If the file more than 500MB, return FALSE
if(!$file_size || $file_size > 500000000){
return FALSE;
}
//If not image, return FALSE
if(!preg_match('/image\/(png|jpe?g|gif|webp)/', $file_type)){
return FALSE;
}
return TRUE;
}