-
Notifications
You must be signed in to change notification settings - Fork 154
/
image-server.php
75 lines (64 loc) · 1.8 KB
/
image-server.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
<?
require('config.php');
function my_urlencode($string) {
return str_replace(' ', '%20', $string);
}
try {
$ok = true;
// Check if the URL is set
if($ok && isset($_GET["url"])) {
// Get the URL and decode to remove any %20, etc
$url = urldecode($_GET["url"]);
// Hash the url
$filename = get_filename($url);
} else {
// No URL set so error
header('HTTP/1.0 400 Bad Request');
echo "No URL was specified";
$ok = false;
}
if($ok) {
if(file_exists($filename)) {
// Send cached file
$file = file_get_contents($filename, false, NULL, strlen($garbage));
} else {
// Load the image and save the file if valid image if found
// Get the contents of the URL
$file = file_get_contents(my_urlencode($url));
// Check if it is an image
$img = @imagecreatefromstring($file);
if($img) {
$max = 800;
$w = imagesx($img);
$h = imagesy($img);
if ($w > $max || $h > $max) {
// Resize the image if needed
$scale = min($max / $w, $max / $h);
$new_w = intval($scale * $w);
$new_h = intval($scale * $h);
$new_img = imagecreatetruecolor($new_w, $new_h);
imagecopyresampled($new_img, $img, 0, 0, 0, 0, $new_w, $new_h, $w, $h);
imagedestroy($img);
imagejpeg($new_img, $filename);
$file = file_get_contents($filename);
}
file_put_contents($filename, $garbage);
file_put_contents($filename, $file, FILE_APPEND);
} else {
// The requested file is not an image
header('HTTP/1.0 400 Bad Request');
print "Invalid image specified";
$ok = false;
}
}
}
if($ok) {
header('Content-Type: image');
print($file);
}
} catch (Exception $e) {
header('HTTP/1.0 500 Internal Server Error');
echo "Internal Server Error";
$ok = false;
}
?>