summaryrefslogtreecommitdiff
path: root/forum/htdocs/imagecache.php
blob: f23a51d4735cd16dc7de3c3e0ae1c5968992dcf3 (plain)
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
<?php

include_once("config.php");
include_once($UTIL_DIR . "/ping.php");


function rescale($image) {

	$maxwidth = 300;
	$maxheight = 240;

	$width = imagesx($image);
	$height = imagesy($image);

	if($width > $maxwidth) $scale = $width / $maxwidth;
	if($height / $scale > $maxheight) $scale = $height / $maxheight;

	$image_p = imagecreatetruecolor($width / $scale, $height / $scale);
	imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width / $scale, $height / $scale, $width, $height);

	return $image_p;
}


$fullfilename = $IMAGECACHE . "/" . urlencode($filename);

if(!file_exists($fullfilename)) {

	$url = parse_url($filename);
	$filetype = strrchr($url["path"], '.');

	if( true || ping($url["hostname"], 1000) != -1) {

		if(strcasecmp($filetype, ".jpeg") == 0 || strcasecmp($filetype, ".jpg") == 0) {
			$image = imagecreatefromjpeg(urldecode($filename));
			if(!$image) die(404);
			$image = rescale($image);
			imagejpeg($image, $fullfilename, 90);
		} else if(strcasecmp($filetype, ".gif") == 0) {
			$image = imagecreatefromgif(urldecode($filename));
			if(!$image) die(404);
			$image = rescale($image);
			imagegif($image, $fullfilename);
		} else if(strcasecmp($filetype, ".png") == 0) {
			$image = imagecreatefrompng(urldecode($filename));
			if(!$image) die(404);
			$image = rescale($image);
			imagepng($image, $fullfilename);
		} else {
			echo "<p>Unknown image format " . $filetype . "</p>";
		}

	}
}

header('Content-Description: File Transfer');
header('Content-Type: image/jpeg');
header('Content-Length: ' . filesize($fullfilename));
header('Content-Disposition: inline; filename=' . basename($filename));
readfile($fullfilename); 

?>