/
image.php
254 lines (172 loc) · 6.63 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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<?php
/**
* Image Generator
*
* This file automatically generates the correctly sized image,
* caches it for future use, and displays it.
*
* Supports jpg, gif, and png
*
* Nathan Gardner <nathan@factory8.com>
* September 15th, 2010
*
* ----------------
*
* Usage:
* image.php?f=full/path.jpg&w=width&h=height[&effect=(bestfit,crop,stretch)]
*
* f = filename of the image, including directories (relative path to current script, should not begin with /)
* w = the width of the image
* h = the height of the image
* effect = the resize effect. This is optional, defaults to bestfit. Other possible values are crop and stretch
*
* w and h are both required if the effect is crop or stretch, otherwise only one of them is required
*
* Example:
* <img src="/image.php?f=images/myimage.jpg&w=180&h=135&effect=crop"/>
*
* Limitations:
* Although the input file can be jpg, gif, or png - the output file
* will always be a jpg, so you cannot have transparencies or animations
*
*/
// SETTINGS
ini_set('display_errors',0);
$cacheDir = $_SERVER['DOCUMENT_ROOT'].'/cache/';
$imageQuality = 95; // 0 (bad quality, small file) to 100 (high quality, big file)
##############################################################################
##############################################################################
// INSTALL
if(!is_dir($cacheDir)) { mkdir($cacheDir,0777,true); }
##############################################################################
##############################################################################
// SETUP
$orignalImage = !empty($_GET['f'])?$_GET['f']:0;
if(substr($orignalImage,0,1) == '/') {
$orignalImage = substr($orignalImage,1);
}
$maxWidth = !empty($_GET['w'])?intval($_GET['w']):0;
$maxHeight = !empty($_GET['h'])?intval($_GET['h']):0;
$effect = !empty($_GET['effect'])?$_GET['effect']:'bestfit';
##############################################################################
##############################################################################
// PROCESS
if(file_exists($orignalImage) && is_file($orignalImage)) {
$cacheFile = md5($orignalImage.$maxWidth.$maxHeight.$effect).'.jpg';
// if width and height arent set, effect gets set to bestfit
if(empty($maxWidth) || empty($maxHeight)) {
$effect = 'bestfit';
}
// see if we have a cached version, and that the orignal image has not been updated
if(!file_exists($cacheDir.$cacheFile) || filemtime($orignalImage) > filemtime($cacheDir.$cacheFile)) {
$imageInfo = getimagesize($orignalImage);
$orignalWidth = intval($imageInfo[0]);
$orignalHeight = intval($imageInfo[1]);
$orignalType = intval($imageInfo[2]);
$orignalRatio = $orignalWidth/$orignalHeight;
// determine output width and height
switch($effect) {
case "crop":
case "stretch":
$width = $maxWidth;
$height = $maxHeight;
break;
case "bestfit":
default:
if($maxWidth && $maxHeight) {
if($orignalWidth > $orignalHeight) {
$width = $maxWidth;
$height = $maxWidth * ($orignalHeight / $orignalWidth);
} else {
$height = $maxHeight;
$width = $maxHeight * ($orignalWidth / $orignalHeight);
}
if($height > $maxHeight) {
$height = $maxHeight;
$width = $maxHeight * ($orignalWidth / $orignalHeight);
}
} else {
if($maxWidth) {
$width = $maxWidth;
$height = $maxWidth * ($orignalHeight / $orignalWidth);
} else {
$height = $maxHeight;
$width = $maxHeight * ($orignalWidth / $orignalHeight);
}
}
break;
}
$newRatio = $width/$height;
// load in the orignal image
switch($orignalType) {
case 1: $loadedImage = imagecreatefromgif($orignalImage); break;
case 2: $loadedImage = imagecreatefromjpeg($orignalImage); break;
case 3: $loadedImage = imagecreatefrompng($orignalImage); break;
}
if($loadedImage) {
// create new image
$newImage = imagecreatetruecolor($width,$height);
// put orignal image in new image
switch($effect) {
case 'bestfit':
case 'stretch':
imagecopyresampled($newImage,$loadedImage,0,0,0,0,$width,$height,$orignalWidth,$orignalHeight);
break;
case 'crop':
if($newRatio > $orignalRatio) {
$start_x = 0;
$crop_width = $orignalWidth;
$crop_height = $crop_width * ($height/$width);
$start_y = ($orignalHeight - $crop_height)/2;
} else {
$start_y = 0;
$crop_height = $orignalHeight;
$crop_width = $crop_height * $newRatio;
$start_x = ($orignalWidth - $crop_width)/2;
}
imagecopyresampled($newImage,$loadedImage,0,0,$start_x,$start_y,$width,$height,$crop_width,$crop_height);
break;
}
// save to cache folder
imagejpeg($newImage,$cacheDir.$cacheFile,$imageQuality);
// display it
outputImage($cacheDir.$cacheFile);
} else {
outputError($maxWidth,$maxHeight,'Image format not supported.');
}
} else {
// have cached version, display it!
outputImage($cacheDir.$cacheFile);
}
} else {
outputError($maxWidth,$maxHeight,'No Image Available');
}
##############################################################################
##############################################################################
function outputError($width,$height,$errorMsg) {
if(empty($width)) {
$width = $height;
}
if(empty($height)) {
$height = $width;
}
if(empty($width)) {
$width = 160;
$height = 53;
}
$errorImage = imagecreate($width,$height);
$background = imagecolorallocate($errorImage, 230, 230, 230);
$black = imagecolorallocate($errorImage, 0, 0, 0);
header('Content-type: image/jpeg');
imagestring($errorImage,2,10,10,$errorMsg,$black);
imagejpeg($errorImage);
imagedestroy($errorImage);
exit();
}
function outputImage($fileName) {
header('Content-type: image/jpeg');
header('Content-Disposition: attachment; filename='.$fileName);
echo file_get_contents($fileName);
exit();
}
?>