summaryrefslogtreecommitdiff
path: root/utils/imagecache.php
blob: 347e9461de6a1c6cc7e63d99e631fbccba89e5a6 (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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php /* -*- Mode: php; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */

global $UTIL_DIR, $MODULES_DIR;

include_once($UTIL_DIR . "/modules.php");

class ImageSize {
	public $width;
	public $height;

	public function ImageSize($w, $h)
	{
		$this->width = $w;
		$this->height = $h;
	}
};

function rescale($image, $maxwidth, $maxheight)
{
	$width = imagesx($image);
	$height = imagesy($image);

	if($width <= $maxwidth && $height <= $maxheight) return $image;

	$scale = 1;
	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;
}

function errorImage($message)
{
	header("Content-type: image/png");
	$im = @imagecreate(8 + strlen($message) * 5, 20)
		or die("Cannot Initialize new GD image stream");
	$background_color = imagecolorallocate($im, 0, 0, 0);
	$text_color = imagecolorallocate($im, 233, 14, 91);
	imagestring($im, 1, 5, 5,  $message, $text_color);
	imagepng($im);
	imagedestroy($im);
}

function getCachedImage($filename, $mod, $cat)
{
	global $modules;
	loadModule($mod);
	if(!$modules[$mod]) die(404);
	if(!method_exists($modules[$mod], "getImageSize")) die(404);

	$size = $modules[$mod]->getImageSize($cat);
	$maxwidth = $size->width;
	$maxheight = $size->height;

	/*
	if($mod == "discography") {
		if($cat == "cover") { $maxwidth = 100; $maxheight = 100; }
	}
	if($mod == "gallery") {
		if($cat == "randomimage") { $maxwidth = 100; $maxheight = 100; }
		if($cat == "photo") { $maxwidth = 100; $maxheight = 100; }
		if($cat == "albumicon") { $maxwidth = 100; $maxheight = 100; }
	}
	*/
	global $IMAGECACHE, $JPEG_CACHE_QUALITY;
	$fullfilename = $IMAGECACHE . "/" . $maxwidth . ":" . $maxheight . ":". urlencode($filename);

	// Test the storage dir
	if(!file_exists($IMAGECACHE)) {
		if(!mkdir($IMAGECACHE)) errorImage("Could not create directory: " . $IMAGECACHE);
	}
	if(!is_dir($IMAGECACHE)) errorImage($IMAGECACHE . " exists but is not a directory");
	if(!is_readable($IMAGECACHE) || !is_writeable($IMAGECACHE) || !is_executable($IMAGECACHE)) {
		errorImage($IMAGECACHE . " exists but does not have the correct permissions. (r/w/x)");
	}
	// end of dir test

	if(!file_exists($fullfilename)) {

		$url = parse_url($filename);
		$filetype = strrchr($url["path"], '.');
		
		if( true || ping($url["hostname"], 1000) != -1) {
			
			error_reporting(E_ERROR | E_PARSE); 

			switch(strtolower($filetype)) {
			case ".jpeg":
			case ".jpg":
				$image = imagecreatefromjpeg(urldecode($filename));
				if(!$image) errorImage("Could not read: ". $filename);
				$image = rescale($image, $maxwidth, $maxheight);
				imagejpeg($image, $fullfilename, $JPEG_CACHE_QUALITY);
				break;

			case ".gif":
				$image = imagecreatefromgif(urldecode($filename));
				if(!$image) errorImage("Could not read: ". $filename);
				$image = rescale($image, $maxwidth, $maxheight);
				imagegif($image, $fullfilename);
				break;

			case ".png":
				$image = imagecreatefrompng(urldecode($filename));
				if(!$image) errorImage("Could not read: ". $filename);
				$image = rescale($image, $maxwidth, $maxheight);
				imagepng($image, $fullfilename);
				break;

			default:
				if(!$image) errorImage("Unknown image type " . $filetype);
				break;
			}
			
			error_reporting(E_ALL ^ E_NOTICE);

		}
	}
	header('Content-Description: File Transfer');
	header('Content-Type: image/jpeg');
	header('Content-Length: ' . filesize($fullfilename));
	header('Content-Disposition: inline; filename=' . basename($filename));
	header('Last-Modified: ' . date("r", filemtime($fullfilename)));
	readfile($fullfilename); 
}

?>