-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.php
101 lines (85 loc) · 2.99 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
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
<?php
include ("common.php");
//Handle more specific queries
$img = null;
$imgSize = 128;
if (isset($_GET["size"]))
$imgSize = $_GET["size"];
if (isset($_GET['img']) && $_GET['img'] != "") {
$img = $_GET['img'];
} else { //Accept a blanket query
if (isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING'] != "")
$img = $_SERVER['QUERY_STRING'];
}
if (!isset($img)) { //Deal with no usable request
header($_SERVER["SERVER_PROTOCOL"] . " 400 Bad Request");
die;
}
$cacheID = $img;
$url = base64url_decode($cacheID);
//Prepare the cache
$path = "cache";
if (!file_exists($path)) {
mkdir($path, 0755, true);
}
//Make sure our filename isn't too long
$fullWritePath = getcwd() . "/" . $path . "/";
$availLength = 250 - strlen($fullWritePath);
$startPos = strlen($cacheID) - $availLength;
if ($startPos < 0)
$startPos = 0;
$cacheID = substr($cacheID, $startPos);
//Fetch and cache the file if its not already cached
$path = $path . "/" . $cacheID . ".png";
if (!file_exists($path) || filesize($path) < 1) {
file_put_contents($path, fopen($url, 'r'));
}
//TODO: Determine if the image is empty and return something generic instead
//TODO: Handle invalid images
//Make image smaller so we don't crush tiny old devices
resize_img($imgSize, $path, $path);
// send the right headers
$info = getimagesize($path);
header("Content-Type: " . $info['mime']);
header("Content-Length: " . filesize($path));
// dump the file and stop the script
$fp = fopen($path, 'r');
fpassthru($fp);
exit;
//Function to resize common image formats
// Found on https://stackoverflow.com/questions/13596794/resize-images-with-php-support-png-jpg
function resize_img($newWidth, $targetFile, $originalFile) {
if (isset($newWidth) && isset($targetFile) && isset($originalFile)) {
$info = getimagesize($originalFile);
$mime = $info['mime'];
switch ($mime) {
case 'image/jpeg':
$image_create_func = 'imagecreatefromjpeg';
$image_save_func = 'imagejpeg';
$new_image_ext = 'jpg';
break;
case 'image/png':
$image_create_func = 'imagecreatefrompng';
$image_save_func = 'imagepng';
$new_image_ext = 'png';
break;
case 'image/gif':
$image_create_func = 'imagecreatefromgif';
$image_save_func = 'imagegif';
$new_image_ext = 'gif';
break;
default:
throw new Exception('Unknown image type.');
}
$img = $image_create_func($originalFile);
list($width, $height) = getimagesize($originalFile);
$newHeight = ($height / $width) * $newWidth;
$tmp = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($tmp, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
if (file_exists($targetFile)) {
unlink($targetFile);
}
$image_save_func($tmp, $targetFile);
}
}
?>