-
Notifications
You must be signed in to change notification settings - Fork 3
/
thumbnailer.php
129 lines (99 loc) · 3.57 KB
/
thumbnailer.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
<?php
//include ('./config.php');
if ($_SERVER['HTTP_USER_AGENT']==''){
http_response_code(403);
exit(0);
}
//We've nothing to say to most impolite bots
$options = array(
'http'=>array(
'method'=>"GET",
'header'=> "User-Agent: CreRo Thumbnailer\r\n")
);
$context = stream_context_create($options);
$thumbz=array_diff(scandir('./thumbcache'), Array ('..', '.'));
foreach ($thumbz as $thumb){
if (floatval(filemtime('./thumbcache/'.$thumb)+(4*2419200))<=microtime(true)){//2419200 = 4*60*60*24*7
unlink ('./thumbcache/'.$thumb);
}
}
if (!isset($_GET['target'])&&!isset($_GET['viewportwidth'])&&!isset($_GET['ratio'])){
die('no opt');
}
$file = str_replace('./','',$_GET['target']);
$viewportwidth= intval($_GET['viewportwidth']);
$ratio=floatval($_GET['ratio']);
if ($ratio>1){
$ratio=1;
}
if ((file_exists('./'.$file) && strpos(mime_content_type('./'.$file),'image/')==0&&(dirname(realpath($file))===realpath('./covers')))||$file=='favicon.png'){
/* if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) &&
strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= filemtime('./'.$file)
&&$ratio<0.5)
{
header('HTTP/1.0 304 Not Modified');
exit;
}
*/
header('Content-type: application/x-png');
list($width, $height) = getimagesize($file);
$modwidth=intval(floatval($ratio)*(floatval($viewportwidth)));
$modheight = $modwidth;
if (!isset($_GET['hook'])){
if (file_exists('./thumbcache/'.$modwidth.'-'.$modheight.'-'.str_replace('/','',$file).'.png')){
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) &&
strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) <= filemtime('./thumbcache/'.$modwidth.'-'.$modheight.'-'.str_replace('/','',$file).'.png')
)
{
header(null);
header('HTTP/1.0 304 Not Modified');
exit();
}
/*
$handle=fopen('./thumbcache/'.$modwidth.'-'.$modheight.'-'.str_replace('/','',$file).'.png', 'rb');
fpassthru($handle);
fclose($handle);*/
header('Last-Modified: '.date(DATE_RFC822, filemtime('./thumbcache/'.$modwidth.'-'.$modheight.'-'.str_replace('/','',$file).'.png')));
readfile('./thumbcache/'.$modwidth.'-'.$modheight.'-'.str_replace('/','',$file).'.png');
exit();
}
else {
$content=false;
$sleeper=0;
while ($content===false){
sleep($sleeper);
$content=file_get_contents(
'http://'.$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'].'?hook=hook&ratio='.$ratio.'&target='.urlencode($file).'&viewportwidth='.$viewportwidth
, false, $context
);
$sleeper++;
}
file_put_contents('./thumbcache/'.$modwidth.'-'.$modheight.'-'.str_replace('/','',$file).'.png',
$content
);
header('Last-Modified: '.date(DATE_RFC822, filemtime('./thumbcache/'.$modwidth.'-'.$modheight.'-'.str_replace('/','',$file).'.png')));
header('Cache-Control:', 'public, max-age='.intval(3*864000)); //30 days
readfile('./thumbcache/'.$modwidth.'-'.$modheight.'-'.str_replace('/','',$file).'.png');
die();
}
}
$output= imagecreatetruecolor($modwidth, $modheight);
$type=mime_content_type('./'.$file);
if (strstr($type, 'gif')){
$source = imagecreatefromgif($file);
}
if (strstr($type, 'png')){
$source = imagecreatefrompng($file);
}
if (strstr($type, 'jpeg')){
$source = imagecreatefromjpeg($file);
}
imagecopyresized($output, $source, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);
if (isset($_GET['hook'])){
header('Content-type: application/x-png');
//header('Cache-Control', 'public, max-age='.intval(3*864000)); //30 days
imagepng($output);
}
}
die();
?>