forked from juji/PHP-image-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
201 lines (146 loc) · 4.3 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
<?php
///settings
$_SET = array();
include 'Variables.php';
/////////////////////////////////////////////
//make cache dir of not exist
if(!file_exists(CACHEDIR)){mkdir(CACHEDIR);}
//set the root
$ROOT = str_replace($_SERVER['DOCUMENT_ROOT'],'',preg_replace('/\/[^\/]+$/','',$_SERVER['SCRIPT_FILENAME']));
$ROOT = preg_replace('/\/+/','/','/'.$ROOT.'/');
// see the path
$path = cleanPath(explode('/',preg_replace('`^'.preg_quote($ROOT).'`','',rawurldecode($_SERVER['REQUEST_URI']))));
if(!sizeof($path)){
send404('Bad syntax: you requested nothing');
}
//if maintenance
if($path[0]=='cleancache'){
include 'cleancache.php';
}
require_once 'process.php';
function pp($str){
die( "<pre>".print_r($str)."</pre>" );
}
function send404($str){
header('HTTP/1.1 404 Not Found');
header('Content-type: text/plain');
die($str);
}
function send400($str){
header('HTTP\1.1 400 Bad Request');
header('Content-type:text/plain');
die($str);
}
function dieOnNone($image){
if(file_exists($image) && is_file($image)) return;
send404("$image Not found");
}
function getFile($image){
dieOnNone($image);
header("Content-Type: image/jpg");
header('Cache-Control: private, max-age='.CACHETIME);
header('Expires: ' . gmdate('D, d M Y H:i:s', time() + CACHETIME) . ' GMT');
$r = file_get_contents($image);
header('Etag: ' . md5($r));
die($r);
}
function getFilename($uri){
$last = sizeof($uri) - 1;
$name = array();
foreach ($uri as $k=>$v)
{
$k = $k*1;
if($k==$last){
$filename = explode('.',$uri[sizeof($uri)-1]);
array_unshift($name,$filename[0]);
$name[] = 'jpg';
continue;
}
$name [] = $v;
}
return implode('.',$name);
}
function getCrop($par){
$c = explode('|','center|top|left|right|bottom|left-top|left-bottom|right-top|right-bottom');
if(in_array($par,$c) || preg_match('/\./',$par)){
return $par;
}
return false;
}
function getDim($par){
$regexSize = array(
'/\d+\-w/',
'/\d+\-h/',
'/\d+x\d+/'
);
if(preg_match($regexSize[0],$par)){
$w = preg_replace('/\-w/','',$par);
return array($par,$w*1,false);
}
if(preg_match($regexSize[1],$par)){
$h = preg_replace('/\-h/','',$par);
return array($par,false,$h*1);
}
if(preg_match($regexSize[2],$par)){
$h = explode('x',$par);
return array($par,$h[0]*1,$h[1]*1);
}
return false;
}
function cleanPath($p){
if(sizeof($p)&&!$p[0]) {
array_shift($p);
return cleanPath($p);
}
else return $p;
}
dieOnNone( IMAGEDIR . $path[sizeof($path)-1] );
$filename = CACHEDIR . getFilename($path);
if(file_exists($filename) && is_file($filename)) getFile($filename);
$filter = array('dreamy','velvet','chrome','lift','canvas','vintage','monopin','antique','blackwhite','boost','sepia','blur');
switch(sizeof($path)){
case 1:
getFile(IMAGEDIR . $path[0]);
case 2:
if(in_array($path[0],$filter)){
getFile(gd_filter_image(IMAGEDIR . $path[1],$filename,$path[0]));
}
if($r = getDim($path[0])){
getFile( gd_resize( IMAGEDIR . $path[1], $filename, $r[1], $r[2] ) );
}
send400('Bad syntax: you should use filter or size on first param');
case 3:
if(in_array($path[0],$filter)){
if($r = getDim($path[1])){
$n = gd_resize( IMAGEDIR . $path[2], $filename, $r[1], $r[2] );
getFile(gd_filter_image($n,$n,$path[0]));
}else{
send400('Bad syntax: unknown size on second param');
}
}
if($r = getDim($path[0])){
$n = gd_crop( IMAGEDIR . $path[2], $filename, $r[1], $r[2], $path[1] );
if(!$n){
send400('Bad syntax: unknown crop value on second param');
}
getFile($n);
}
send400('Bad syntax: you should use filter or size on first param');
default: // 4 or >4
if(!in_array($path[0],$filter)){
send400('Bad syntax: unknown filter on first param');
}
if(!getDim($path[1])){
send400('Bad syntax: unknown size on second param');
}
if(!getCrop($path[2])){
send400('Bad syntax: unknown crop value on third param');
}
$r = getDim($path[1]);
$n = gd_crop( IMAGEDIR . $path[3], $filename, $r[1], $r[2], $path[2] );
if(!$n){
send400('Bad syntax: unknown crop value on third param');
}
getFile(gd_filter_image($n,$n,$path[0]));
}
?>