-
Notifications
You must be signed in to change notification settings - Fork 0
/
product_thumb.php
408 lines (375 loc) · 14.1 KB
/
product_thumb.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
<?php
// This is "On the Fly Thumbnailer with Caching Option" by Pallieter Koopmans.
// Based on Marcello Colaruotolo (1.5.1) which builds upon Nathan Welch (1.5)
// and Roberto Ghizzi. With improvements by @Quest WebDesign, http://atQuest.nl/,
// Martijn Loeffen (browser-caching and thumbnail-cache-dir) and Richard Burford
// aka psynaptic (admin control and documentation)
//
// Scales product images dynamically, resulting in smaller file sizes, and keeps
// proper image ratio.
//
// Used in conjunction with modified tep_image in html_output.php (see: readme.html).
// get config values from the database
require('includes/configure.php'); // include server parameters
$server = DB_SERVER;
$username = DB_SERVER_USERNAME;
$password = DB_SERVER_PASSWORD;
$database = DB_DATABASE;
$connect=@mysql_connect($server,$username,$password) or http_headers('','Error,Database Connection');
@mysql_select_db($database,$connect) or http_headers('','Error,Database Connection');
$query="select configuration_key as cfgKey, configuration_value as cfgValue from configuration where configuration_group_id='100'";
$output=@mysql_query($query,$connect) or http_headers('','Error,Database Connection');
while ($row = @mysql_fetch_array($output)) {define($row['cfgKey'], $row['cfgValue']);}
// CONFIGURATION SETTINGS
//
// Server cache directory. Set the value below to true to generate resampled thumbnails
// resulting in smoother-looking images. Not supported in GD ver. < 2.01
$use_resampling = true; if (THUMBNAIL_USE_RESAMPLING === 'false') {$use_resampling = false;}
//
// Create True Color Thumbnails? Better quality overall but set to false if you
// have GD version < 2.01 or if creating transparent thumbnails.
$use_truecolor = true; if (THUMBNAIL_TRUE_COLOUR === 'false') {$use_truecolor = false;}
//
// Output GIFs as JPEGS? Set this option to true if you have GD version > 1.6
// and want to output GIF thumbnails as JPGs instead of GIFs or PNGs. Note that your
// GIF transparencies will not be retained in the thumbnail if you output them
// as JPGs. If you have GD Library < 1.6 with GIF create support, GIFs will
// be output as GIFs. Set the "matte" color below if setting this option to true.
$gif_as_jpeg = false; if (THUMBNAIL_OUTPUT_JPEG === 'true') {$gif_as_jpeg = true;}
//
// Cache Images on the server? Set to true if you want to save requested thumbnails
// on disk. This will add to disk space but will save your processor from having to
// create the thumbnail for every visitor.
$tn_server_cache = true; if (THUMBNAIL_CACHE_SERVER === 'false') {$tn_server_cache = false;}
//
// Thumbnail Path. If server-caching is enabled, specify a sub-directory
// where the thumbnails should be kept. Use '' for the default images-directory,
// which is /catalog/images/
// Note: Make sure this path actually exists as a subdirectory and is writeable!
$tn_path = THUMBNAIL_PATH; // The default is 'thumbnails/', should be chmod 777
//
// Cache Images in Browser-Cache? Set to true if you want browsers to be able to
// cache viewed thumbnails in their own cache. This will save bandwidth for every
// visitor that views the same thumbnail again. The default is true
$tn_browser_cache = true; if (THUMBNAIL_CACHE_BROWSER === 'false') {$tn_browser_cache = false;}
//
// Send a 404 http response when an image is not found
// If set to false, will show a small error-image (as in version < 2.0.0)
$use404 = true; // The default is true
//
// Define RGB Color Value for background matte color if outputting GIFs as JPEGs
// Example: white is r=255, b=255, g=255; black is r=0, b=0, g=0; red is r=255, b=0, g=0;
$tn_colours = explode(",", THUMBNAIL_BACKGROUND_MATTE);
$r = $tn_colours[0]; $g = $tn_colours[1]; $b = $tn_colours[2];
//
// Allow the creation of thumbnail images that are larger than the original images:
$allow_larger = false; if (THUMBNAIL_ALLOW_LARGER === 'true') {$allow_larger = true;}
//
// If allow_larger is set to false, you can opt to output the original image:
// Better leave it true if you want pixel_trans_* to work as expected
$show_original = true; if (THUMBNAIL_SHOW_ORIGINAL == 'false') {$show_original = false;}
//
// Set jpeg compression quality for resampled images.
$jpeg_quality=THUMBNAIL_JPEG_COMPRESSION; // The default is 100.
// END CONFIGURATION SETTINGS
// Note: In order to manually debug this script, you might want to comment
// some header() lines -- otherwise no output is shown.
// reverse strrchr(), taken from http://nl2.php.net/manual/en/function.strrchr.php
function reverse_strrchr($haystack, $needle)
{
return strrpos($haystack, $needle) ? substr($haystack, 0, strrpos($haystack, $needle) +1 ) : false;
}
function modify_tn_path($file)
{
global $tn_path;
if ($tn_path=='') return $file;
else{
// append the thumbnail-path to the path
$pathSep = "/"; //$pathSep = strstr(PHP_OS, "WIN") ? "\\" : "/";;
$path = reverse_strrchr($file,$pathSep);
error_reporting(E_ALL);
if ($path===false) return $tn_path . $file;
else return str_replace($path,$path . $tn_path,$file);
}
}
function http_headers($file='')
{
//
// This function supports the use of browser-caching (optional)
//
// A 304 (Not Modified) will be sent when the thumbnail has not changed
// since the time it was last cached by the client
// A 200 (OK) will be sent when the thumbnail was not cached by the client
// or when the thumbnail was cached but changed afterwards
// A 404 (Not Found) will be sent when the thumbnail is not found (optional)
global $use404, $tn_browser_cache;
if (isset($_SERVER["SERVER_PROTOCOL"]) && $_SERVER["SERVER_PROTOCOL"] == "HTTP/1.1")
$httpProtocol = "HTTP/1.1";
else
$httpProtocol = "HTTP/1.0";
if (file_exists($file))
{
if (isset ($_SERVER["HTTP_CACHE_CONTROL"])) {
$tn_browser_cache = strtolower($_SERVER["HTTP_CACHE_CONTROL"]) == "no-cache" ? false : $tn_browser_cache ;
}
//Build our entity tag, which is "inode-lastmodtime-filesize"
$lastModified = filemtime($file);
$lastModifiedGMT = $lastModified - date('Z');
$lastModifiedHttpFormat = gmstrftime("%a, %d %b %Y %T %Z", $lastModified);
// Don't use inode in eTag when you have multiple webservers, instead I use a dummy value (1fa44b7)
$eTag = '"1fa44b7-' . dechex(filesize($file)) . "-" . dechex($lastModifiedGMT) . '"';
if ($tn_browser_cache){
$lastModifiedFromHttp = "xxx";
if (isset ($_SERVER["HTTP_IF_MODIFIED_SINCE"])) {
$lastModifiedFromHttp = ($_SERVER["HTTP_IF_MODIFIED_SINCE"] === "") ? "xxx" : $_SERVER["HTTP_IF_MODIFIED_SINCE"] ;
}
// Read sent eTag by browser
$foundETag = "";
if (isset ($_SERVER["HTTP_IF_NONE_MATCH"])) {
$foundETag = stripslashes($_SERVER["HTTP_IF_NONE_MATCH"]);
}
// Last Modification Time
if ($lastModifiedFromHttp == $lastModifiedHttpFormat) {
$sameLastModified = true;
}
elseif (strpos($lastModifiedFromHttp,$lastModifiedHttpFormat) !== false){
$sameLastModified = true;
}
else {
$sameLastModified = false;
}
if (($eTag == $foundETag) && $sameLastModified){
// same eTag and Last Modification Time (e.g. with Firefox)
$is304 = true;
}
else
// no eTag supplied, but Last Modification Time is unchanged (e.g. with IE 6.0)
$is304 = (($foundETag == "") && $sameLastModified);
if ($is304)
{
//
// They already have an up to date copy so tell them
if ($lastModifiedGMT > 946080000) { // 946080000 = Dec 24, 1999 4PM
// only send if valid eTag
header("ETag: " . $eTag);
}
header("Status: 304 Not Modified");
header($httpProtocol . " 304 Not Modified");
header("Connection: close");
exit();
}
}
//
// We have to send them the whole page
header('Pragma: ');
header('Expires: ');
if ($tn_browser_cache){
if ($lastModifiedGMT > 946080000) { // 946080000 = Dec 24, 1999 4PM
header('ETag: ' . $eTag);
}
header('Last-Modified: ' . $lastModifiedHttpFormat);
header('Cache-Control: private');
}
else {
header('Cache-Control: no-cache');
}
}
else
{
if ($use404)
{
//
// send them a 404 http response header
header("TEST404: TEST404");
header("Status: 404 Not Found");
header($httpProtocol . " 404 Not Found");
exit();
}
else
{
//
// show a custom error-image (non-cacheable by the browser)
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // Always modified
header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1
header("Cache-Control: post-check=0, pre-check=0", false); // HTTP/1.1
header("Pragma: no-cache"); // HTTP/1.0
header('Content-type: image/jpeg');
$src = imagecreate($_GET['w'], $_GET['h']); // Create a blank image.
$bgc = imagecolorallocate($src, 255, 255, 255);
$tc = imagecolorallocate($src, 0, 0, 0);
imagefilledrectangle($src, 0, 0, $_GET['w'], $_GET['h'], $bgc);
imagestring($src, 1, 5, 5, 'Error', $tc);
imagejpeg($src, '', $jpeg_quality);
imagedestroy($src);
exit();
}
}
}
// Get the size of the image:
$image = @getimagesize($_GET['img']);
// Check the input variables and decide what to do:
if (empty($image) || empty($_GET['w']) || empty($_GET['h']) || (empty($allow_larger) && ($_GET['w'] > $image[0] || $_GET['h'] > $image[1])))
{
if (empty($image) || empty($show_original))
{
http_headers();
}
else
{
// 2Do: Return the original image w/o making a copy (as that is what we currently do):
// $_GET['w'] = $image[0];
// $_GET['h'] = $image[1];
}
}
// Create appropriate image header:
if ($image[2] == 2 || ($image[2] == 1 && $gif_as_jpeg))
{
header('Content-type: image/jpeg');
if ($tn_server_cache) $filename = modify_tn_path($_GET['img'] .'.thumb_'.$_GET['w'].'x'.$_GET['h'].'.jpg');
}
elseif ($image[2] == 1 && function_exists('imagegif'))
{
header('Content-type: image/gif');
if ($tn_server_cache) $filename = modify_tn_path($_GET['img'] .'.thumb_'.$_GET['w'].'x'.$_GET['h'].'.gif');
}
elseif ($image[2] == 3 || $image[2] == 1)
{
header('Content-type: image/png');
if ($tn_server_cache) $filename = modify_tn_path($_GET['img'] .'.thumb_'.$_GET['w'].'x'.$_GET['h'].'.png');
}
// If you are required to set the full path for file_exists(), set this:
// $filename = '/your/path/to/catalog/'.$filename;
if (file_exists($filename) && $tn_server_cache && filemtime($filename) > filemtime($_GET['img']))
{
// Output Cache Headers
http_headers($filename);
if ($image[2] == 2 || ($image[2] == 1 && $gif_as_jpeg))
{
$src = imagecreatefromjpeg($filename);
imagejpeg($src, '', $jpeg_quality);
}
elseif ($image[2] == 1 && function_exists('imagegif'))
{
$src = imagecreatefromgif($filename);
imagegif($src);
}
elseif ($image[2] == 3 || $image[2] == 1)
{
$src = imagecreatefrompng($filename);
imagepng($src);
}
else
{
// Not an image or imagecreatefrom...-function does not exits.
// Let's output an error
http_headers();
}
}
else
{
// create thumbnail directory if not exist.
$tmp_path = str_replace(basename($filename), '', $filename);
if (is_dir($tmp_path) == false) mkdir($tmp_path);
// Create a new, empty image based on settings:
if (function_exists('imagecreatetruecolor') && $use_truecolor && ($image[2] == 2 || $image[2] == 3))
{
$tmp_img = imagecreatetruecolor($_GET['w'],$_GET['h']);
}
else
{
$tmp_img = imagecreate($_GET['w'],$_GET['h']);
}
$th_bg_color = imagecolorallocate($tmp_img, $r, $g, $b);
imagefill($tmp_img, 0, 0, $th_bg_color);
imagecolortransparent($tmp_img, $th_bg_color);
// Create the image to be scaled:
if ($image[2] == 2 && function_exists('imagecreatefromjpeg'))
{
$src = imagecreatefromjpeg($_GET['img']);
}
elseif ($image[2] == 1 && function_exists('imagecreatefromgif'))
{
$src = imagecreatefromgif($_GET['img']);
}
elseif (($image[2] == 3 || $image[2] == 1) && function_exists('imagecreatefrompng'))
{
$src = imagecreatefrompng($_GET['img']);
}
else
{
// Not an image or imagecreatefrom...-function does not exits.
// Let's output an error
http_headers();
}
// Scale the image based on settings:
if (empty($allow_larger) && ($_GET['w'] > $image[0] || $_GET['h'] > $image[1]))
{
// existing image is smaller than thumbnail so create the thumbnail
// with the smaller original image centred on it
$dx = ($_GET['w'] - $image[0]) / 2;
$dy = ($_GET['h'] - $image[1]) / 2;
imagecopy($tmp_img, $src, $dx, $dy, 0, 0, $image[0], $image[1]);
} else
if (function_exists('imagecopyresampled') && $use_resampling)
{
imagecopyresampled($tmp_img, $src, 0, 0, 0, 0, $_GET['w'], $_GET['h'], $image[0], $image[1]);
}
else
{
imagecopyresized($tmp_img, $src, 0, 0, 0, 0, $_GET['w'], $_GET['h'], $image[0], $image[1]);
}
// Output the image:
if ($image[2] == 2 || ($image[2] == 1 && $gif_as_jpeg))
{
if ($tn_server_cache)
{
$thumbnail = modify_tn_path($_GET['img'].'.thumb_'.$_GET['w'].'x'.$_GET['h'].'.jpg');
imagejpeg($tmp_img,$thumbnail, $jpeg_quality);
http_headers($thumbnail);
}
else
{
http_headers($_GET['img']);
}
imagejpeg($tmp_img, '', $jpeg_quality);
}
elseif ($image[2] == 1 && function_exists('imagegif'))
{
if ($tn_server_cache)
{
$thumbnail = modify_tn_path($_GET['img'].'.thumb_'.$_GET['w'].'x'.$_GET['h'].'.gif');
imagegif($tmp_img,$thumbnail);
http_headers($thumbnail);
}
else
{
http_headers($_GET['img']);
}
imagegif($tmp_img);
}
elseif ($image[2] == 3 || $image[2] == 1)
{
if ($tn_server_cache)
{
$thumbnail = modify_tn_path($_GET['img'].'.thumb_'.$_GET['w'].'x'.$_GET['h'].'.png');
imagepng($tmp_img,$thumbnail);
http_headers($thumbnail);
}
else
{
http_headers($_GET['img']);
}
imagepng($tmp_img);
}
else
{
// Not an image or image...-function not supported
// Let's output an error:
http_headers();
}
// Clear the image from memory:
imagedestroy($src);
imagedestroy($tmp_img);
}
?>