-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathimage.php
69 lines (57 loc) · 2.24 KB
/
image.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
<?php
// (C) 2012 hush2 <[email protected]>
require 'lib/Robohash.php';
define('CACHE_IMAGE', true);
define('CACHE_DIR', 'cache/');
$text = isset($_GET['text']) ? $_GET['text'] : $_SERVER['REMOTE_ADDR'];
$gravatar = isset($_GET['gravatar']) ? $_GET['gravatar'] : false;
if ($gravatar) {
$gravatar_hash = null;
if ($gravatar === 'yes') {
$gravatar_hash = md5(strtolower(trim($text)));
} elseif ($gravatar === 'hashed') {
$gravatar_hash = $text;
}
if ($gravatar_hash) {
$gravatar_url = "http://www.gravatar.com/avatar/$gravatar_hash?s=80&default=404";
stream_context_set_default(array('http' => array('method' => 'HEAD')));
$headers = get_headers($gravatar_url);
if (strpos($headers[0], '200 OK') !== false) {
header("Location: $gravatar_url");
}
}
}
$color = isset($_GET['color']) ? $_GET['color'] : false;
$set = isset($_GET['set']) ? $_GET['set'] : false;
$bgset = isset($_GET['bgset']) ? $_GET['bgset'] : false;
$size = isset($_GET['size']) ? $_GET['size'] : false;
// File extension is ignored by default when computing the hash.
$ignoreext = isset($_GET['ignoreext']) && $_GET['ignoreext'] === 'false' ? false : true;
$ext = 'png';
$match = preg_match('/(.*)\.(jpe?g|gif|png|bmp)$/', $text, $matches);
if ($match > 0) {
$ext = $matches[2];
$text = $ignoreext ? $matches[1] : $matches[0];
}
header("Content-Type: image/$ext");
header("Content-Disposition: inline; filename=$text");
header("Cache-Control: maxage=" . 3600 * 24 * 7); // 7 Days
$dt = new DateTime();
$dt = $dt->add(new DateInterval('P7D'))->format('D, d M Y H:i:s');
header("Expires: $dt GMT");
$filename = CACHE_DIR . md5("{$text}_{$set}_{$bgset}_{$color}_{$size}") . ".$ext";
if (CACHE_IMAGE && file_exists($filename)) {
echo file_get_contents($filename);
} else {
$robohash = new Robohash(array(
'text' => $text,
'bgset' => $bgset,
'set' => $set,
'size' => $size,
'color' => $color,
'ext' => $ext,
'filename' => $filename,
'cache' => CACHE_IMAGE,
));
echo $robohash->generate_image();
}