-
Notifications
You must be signed in to change notification settings - Fork 1
/
imthumb-http.class.php
113 lines (96 loc) · 3.4 KB
/
imthumb-http.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
<?php
/**
* HTTP response handler
*
* @package ImThumb
* @author Sam Pospischil <[email protected]>
* @since 2014-04-03
*/
class ImThumbHTTP
{
private static $defaults = array(
'allowAllExternal' => false,
'uriWhitelist' => array(),
'externalRequestTimeout' => 20,
'externalRequestRetry' => 3600,
);
protected $image;
protected $params;
public function __construct(ImThumb $image, Array $params = null)
{
$this->image = $image;
$this->params = array_merge(self::$defaults, isset($params) ? $params : array());
}
// Image output
//--------------------------------------------------------------------------
public function sendHeaders($serverErrorString = null)
{
// check we can send these first
if (headers_sent()) {
throw new ImThumbException("Could not set image headers, output already started", ImThumb::ERR_OUTPUTTING);
}
$modifiedDate = gmdate('D, d M Y H:i:s') . ' GMT';
// get image size. Have to workaround bugs in Imagick that return 0 for size by counting ourselves.
$byteSize = $this->image->getImageSize();
if ($this->image->hasBrowserCache()) {
header('HTTP/1.0 304 Not Modified');
} else {
if ($serverErrorString) {
header('HTTP/1.0 500 Internal Server Error');
header('X-ImThumb-Error: ' . $serverErrorString);
} else if (!$this->image->isValid()) {
header('HTTP/1.0 404 Not Found');
} else {
header('HTTP/1.0 200 OK');
}
header('Content-Type: ' . $this->image->getMime());
header('Accept-Ranges: none');
header('Last-Modified: ' . $modifiedDate);
header('Content-Length: ' . $byteSize);
if (!$this->image->param('browserCache')) {
header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
header("Pragma: no-cache");
header('Expires: ' . gmdate('D, d M Y H:i:s', time()));
} else {
$maxAge = $this->image->param('browserCacheMaxAge');
$expiryDate = gmdate('D, d M Y H:i:s', strtotime('now +' . $maxAge . ' seconds')) . ' GMT';
header('Cache-Control: max-age=' . $maxAge . ', must-revalidate');
header('Expires: ' . $expiryDate);
}
}
// informational headers
if (!$this->image->param('silent')) {
if (!$imHandle = $this->image->getImagick()) {
$imHandle = new Imagick();
}
$imVer = $imHandle->getVersion();
$imVer = $imVer['versionString'];
header('X-Generator: ImThumb v' . ImThumb::VERSION . '; ' . $imVer);
if ($this->image->cache && $this->image->cache->isCached()) {
header('X-Img-Cache: HIT');
} else {
header('X-Img-Cache: MISS');
}
if ($this->image->param('debug')) {
list($startTime, $startCPU_u, $startCPU_s) = $this->image->getTimingStats();
header('X-Generated-In: ' . number_format(microtime(true) - $startTime, 6) . 's');
header('X-Memory-Peak: ' . number_format((memory_get_peak_usage() / 1024), 3, '.', '') . 'KB');
$data = getrusage();
$memusage = array((double)($data['ru_utime.tv_sec'] + $data['ru_utime.tv_usec'] / 1000000), (double)($data['ru_stime.tv_sec'] + $data['ru_stime.tv_usec'] / 1000000));
header('X-CPU-Utilisation:'
. ' Usr ' . number_format($memusage[0] - $startCPU_u, 6)
. ', Sys ' . number_format($memusage[1] - $startCPU_s, 6)
. '; Base ' . number_format($startCPU_u, 6) . ', ' . number_format($startCPU_s, 6));
}
}
}
public function display()
{
$str = $this->image->getImage();
if ($str) {
$this->sendHeaders();
echo $str;
}
return $str ? true : false;
}
}