-
Notifications
You must be signed in to change notification settings - Fork 0
/
cover.php
160 lines (141 loc) · 4.25 KB
/
cover.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
<?php
if(isset($_GET['mylogin'])) $mylogin=$_GET['mylogin'];
else $mylogin="";
if(isset($_GET['mypass'])) $mypass=$_GET['mypass'];
else $mypass="";
require_once('./config/config.php');
if($protect==false||($account[$mylogin]&&$account[$mylogin][0]==$mypass)) {
//OK
} else {
erreur("login error");
}
if($limited) $calibre=array_merge ($calibre, $limited);
if(isset($_GET['path'])) $path=$_GET['path'];
else $path="";
if(isset($_GET['base'])) $base=$_GET['base'];
else $base="";
if(isset($_GET['id'])) $id=$_GET['id'];
else $id="1";
if(isset($_GET['forced'])) $forced=$_GET['forced'];
else $forced="false";
if($_GET['testxsendfile']=="true") $fetchmode="no resize";
$cover=$calibre[$base].$path."/cover.jpg";
$thumb_path="./thumb/";
//vignettes ipad:
//width: 142px
//height: 211px et 2X pour du retina
$height=422;
if(!file_exists ($cover)) {
erreur('not available');
}
if($fetchmode=="resize") {
writehead();
//test si resize
if(!makeThumbnail($cover, false, NULL, $height)) {
//envoi l'image d'origine sinon
header('Content-Disposition: attachement; filename="cover.jpg"');
if($XSendfile) header('X-Sendfile: '.realpath($cover));
else {
header("Content-Length: " . filesize($cover));
readfile($cover);
}
}
} else if($fetchmode=="resize_and_cache") {
$outputfile=$thumb_path.$base."_".$id.".jpg";
if ($forced=="true"||!file_exists($outputfile)) {
$result = makeThumbnail($cover, true, NULL, $height, $outputfile);
//Refait la miniature, le retour est un json qui indique que c'est fait
if($forced=="true") {
if(!$result) $success=false;
else $success=true;
$json='{"success":'.$success.'}';
if (isset($_GET['callback'])) {
echo $_GET['callback'].'('.$json.');';
} else {
echo $json;
}
die;
}
if(!$result) $outputfile="./resources/images/no_image_available.jpg";
}
writehead();
if($XSendfile) header('X-Sendfile: '.$outputfile);
else {
header("Content-Length: " . filesize($outputfile));
readfile($outputfile);
}
} else { //noresize no cache
writehead();
header('Content-Disposition: attachement; filename="cover.jpg"');
if($XSendfile) header('X-Sendfile: '.realpath($cover));
else {
header("Content-Length: " . filesize($cover));
readfile($cover);
}
}
function makeThumbnail($file, $cache, $width, $height, $outputfile = NULL) {
if (is_null ($width) && is_null ($height)) {
erreur("error resize");
}
// In case something bad happen below set a default size
$nw = "284";
$nh = "422";
// get image size
if ($size = GetImageSize($file)) {
$w = $size[0];
$h = $size[1];
//set new size
if (!is_null ($width)) {
$nw = $width;
if ($nw >= $w) {
if($cache) {
copy($file, $outputfile);
return true;
} else return false;
}
$nh = floor(($nw*$h)/$w);
} else {
$nh = $height;
if ($nh >= $h) {
if($cache) {
copy($file, $outputfile);
return true;
} else return false;
}
$nw = floor(($nh*$w)/$h);
}
}
//draw the image
$src_img = imagecreatefromjpeg($file);
$dst_img = imagecreatetruecolor($nw,$nh);
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $nw, $nh, $w, $h);//resizing the image
if($cache) $created = imagejpeg($dst_img,$outputfile,80);
else imagejpeg($dst_img,$outputfile,80);
imagedestroy($src_img);
imagedestroy($dst_img);
if($cache) return $created;
else return true;
}
function writehead() {
$expires = 60*60*24*14;
//Est-ce utile ?
//header("Pragma: public");
//header("Cache-Control: maxage=".$expires);
//header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$expires) . ' GMT');
header("Content-Type: image/jpeg");
}
function erreur($msg) {
if($msg=="login error") {
$outputfile="./resources/images/no_login.jpg";
} else if($msg=="error resize") {
$outputfile="./resources/images/errorresize.jpg";
} else {
$outputfile="./resources/images/no_image_available.jpg";
}
header("Content-Type: image/jpeg");
header('Content-Disposition: attachement; filename="no_image.jpg"');
header("Content-Length: " . filesize($outputfile));
readfile($outputfile);
die();
}
?>