-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgetthumb.php
137 lines (117 loc) · 4.1 KB
/
getthumb.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
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
131
132
133
134
135
136
137
<?php
/**
* @copyright 2005 Alessandro Pasotti
* @license GPL
* @author Alessandro Pasotti (some source grabbed somewhere)
*
* Read an image file and send a scaled thumbnail to the browser
* default to 150 px width.
*
* This file can also be included, in this case, define vars in the calling script
*
* @param string $img_path image file path, relative to this script, if not
* set, the next is checked on GET
* @param string $img urlencoded image file path, relative to this script
* (in vncms should be 2 level depth)
* @param integer $width max width
* @param integer $height max height
* @param string $jpgfilename optional filename to store image in, if
* not given, image is outputted to the browser
*
*
* Example calls:
*
* getthumb.php?img=../../images/logo.jpg --> default to max 150 (w or h)
* getthumb.php?img=../../images/logo.jpg&height=200 --> resize to 200px height
*
*
*/
// Constants
define('DEF_WIDTH', 150); // default
define('DEF_HEIGHT', 150); // default
define('ALLOWED_WIDTH', 1024); // no tampering: set up a max
define('ALLOWED_HEIGHT',768); // no tampering: set up a max
define('DEF_QUALITY', 90); // default quality
define('DEBUG', false);
// I put this here to avoid any external dependency
if(!function_exists('__clean')) {
function __clean($s, $len){
if(!isset($s)) return null;
$s = substr($s, 0, $len);
if(ini_get('gpc_magic_quotes') != 1) $s = addslashes($s);
$s = escapeshellcmd($s);
return $s;
}
}
// Get image location if not defined elsewhere
if(!isset($image_path)) $image_path = urldecode(stripslashes(__clean($_GET['img'], 256)));
if(!isset($width)) $width = __clean($_GET['width'], 4);
if(!isset($height)) $height = __clean($_GET['height'], 4);
// if both are not defined:
if(!$width && !$height) {
$width = DEF_WIDTH;
$height= DEF_HEIGHT;
}
if($width) $width = min(ALLOWED_WIDTH, $width);
else $width = ALLOWED_WIDTH;
if($height) $height = min(ALLOWED_HEIGHT, $height);
else $height = ALLOWED_HEIGHT;
define('MAX_HEIGHT', $height);
define('MAX_WIDTH' , $width);
// Load image
$img = null;
$imginfo = getimagesize($image_path);
$imgtype = $imginfo[2];
$ext = strtolower(end(explode('.', $image_path)));
if ($imgtype == IMAGETYPE_JPEG) {
$img = @imagecreatefromjpeg($image_path);
} else if ($imgtype == IMAGETYPE_PNG) {
$img = @imagecreatefrompng($image_path);
//echo "IMGEBASE " . IMAGE_BASE . ' path: ' . $image_path; exit;
// Only if your version of GD includes GIF support
} else if ($imgtype == IMAGETYPE_GIF) {
$img = @imagecreatefromgif($image_path);
}
// If an image was successfully loaded, test the image for size
if ($img) {
// Get image size and scale ratio
$width = imagesx($img);
$height = imagesy($img);
$scale = min(MAX_WIDTH/$width, MAX_HEIGHT/$height);
if(defined('DEBUG') && DEBUG) {
user_error("thumbnails.php - IMAGE: $image_path WIDTH: $width HEIGHT: $height SCALE: $scale<hr />");
exit;
}
// If the image is larger than max shrink it
if ($scale < 1) {
$new_width = floor($scale*$width);
$new_height = floor($scale*$height);
// Create a new temporary image
$tmp_img = imagecreatetruecolor($new_width, $new_height);
// Copy and resize old image into new image
imagecopyresized($tmp_img, $img, 0, 0, 0, 0,
$new_width, $new_height, $width, $height);
imagedestroy($img);
$img = $tmp_img;
}
}
// Create error image if necessary
if (!$img) {
$img = imagecreate(DEF_WIDTH, DEF_HEIGHT);
imagecolorallocate($img,255,255,255);
$c2 = imagecolorallocate($img,255,0,0);
imageline($img,0,0,DEF_WIDTH,DEF_HEIGHT,$c2);
imageline($img,DEF_WIDTH,0,0,DEF_HEIGHT,$c2);
}
if(!$jpgfilename) {
// Display the image
header("Content-type: image/jpg");
imagejpeg($img, null, DEF_QUALITY);
exit;
} else {
if(defined('DEBUG') && DEBUG) {
echo "thumbnails.php - saving to $jpgfilename<hr>";
}
imagejpeg($img, $jpgfilename, DEF_QUALITY);
}
?>