-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimthumb.php
More file actions
255 lines (188 loc) · 7.44 KB
/
timthumb.php
File metadata and controls
255 lines (188 loc) · 7.44 KB
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
<?php
// TimThumb script created by Tim McDaniels and Darren Hoyt with tweaks by Ben Gillbanks for the Mimbo Pro theme
// May be re-used pending permission of the authors, email cutout@gmail.com
// Copyright 2008
/* request parameters that can be sent to this script */
// src: location of file from doc root
// w: width
// h: height
// zc: zoom crop (0 or 1)
// q: quality (default is 75 and max is 100)
// either width or height can be used
// example: <img src="/resizeImage.php?src=images/image.jpg&h=150" alt="some image" />
if( !isset( $_REQUEST[ "src" ] ) ) { die( "no image specified" ); }
// clean params before use
$src = preg_replace( "/^(\.+(\/|))+/", "", $_REQUEST['src'] );
$src = preg_replace( '/^(s?f|ht)tps?:\/\/[^\/]+/i', '', $src );
$new_width = preg_replace( "/[^0-9]/", "", $_REQUEST[ 'w' ] );
$new_height = preg_replace( "/[^0-9]/", "", $_REQUEST[ 'h' ] );
$zoom_crop = preg_replace( "/[^0-9]/", "", (isset($_REQUEST[ 'zc' ]) && !empty($_REQUEST[ 'zc' ])) ? $_REQUEST[ 'zc' ] : null );
if( !isset( $_REQUEST['q'] ) ) {
$quality = 80;
} else {
$quality = preg_replace("/[^0-9]/", "", $_REQUEST['q'] );
}
// set path to cache directory (default is ./cache)
// this can be changed to a different location
$cache_dir = './cache';
// check to see if this image is in the cache already
check_cache( $cache_dir, $quality, $zoom_crop);
// get mime type of src
$mime_type = mime_type( $src );
// make sure that the src is gif/jpg/png
if( !valid_src_mime_type( $mime_type ) ) {
$error = "Invalid src mime type: $mime_type";
die( $error );
}
// check to see if GD function exist
if(!function_exists('imagecreatetruecolor')) {
$error = "GD Library Error: imagecreatetruecolor does not exist";
die( $error );
}
// get path to image on file system
$src = $_SERVER['DOCUMENT_ROOT'] . '/' . $src;
if(strlen($src) && file_exists( $src ) ) {
// open the existing image
$image = open_image($mime_type, $src);
if ($image === false) { die ('Unable to open image : ' . $src ); }
// Get original width and height
$width = imagesx($image);
$height = imagesy($image);
// generate new w/h if not provided
if($new_width && !$new_height) {
$new_height = $height * ($new_width/$width);
}
elseif($new_height && !$new_width) {
$new_width = $width * ($new_height/$height);
}
elseif(!$new_width && !$new_height) {
$new_width = $width;
$new_height = $height;
}
// create a new true color image
$canvas = imagecreatetruecolor($new_width, $new_height);
if( $zoom_crop ) {
$src_x = $src_y = 0;
$src_w = $width;
$src_h = $height;
$cmp_x = $width / $new_width;
$cmp_y = $height / $new_height;
// calculate x or y coordinate and width or height of source
if ( $cmp_x > $cmp_y ) {
$src_w = round( ( $width / $cmp_x * $cmp_y ) );
$src_x = round( ( $width - ( $width / $cmp_x * $cmp_y ) ) / 2 );
} elseif ( $cmp_y > $cmp_x ) {
$src_h = round( ( $height / $cmp_y * $cmp_x ) );
$src_y = round( ( $height - ( $height / $cmp_y * $cmp_x ) ) / 2 );
}
imagecopyresampled( $canvas, $image, 0, 0, $src_x, $src_y, $new_width, $new_height, $src_w, $src_h );
} else {
// copy and resize part of an image with resampling
imagecopyresampled( $canvas, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
}
// output image to browser based on mime type
show_image( $mime_type, $canvas, $quality, $cache_dir, $zoom_crop );
// remove image from memory
ImageDestroy( $canvas );
} else {
if( strlen( $src ) ) { echo $src . ' not found.'; } else { echo 'no source specified.'; }
}
function show_image ($mime_type, $image_resized, $quality, $cache_dir, $zoom_crop) {
// check to see if we can write to the cache directory
$is_writable = 0;
$cache_file_name = $cache_dir . '/' . get_cache_file($quality, $zoom_crop);
if( touch( $cache_file_name ) ) {
// give 666 permissions so that the developer
// can overwrite web server user
chmod( $cache_file_name, 0666 );
$is_writable = 1;
} else {
$cache_file_name = NULL;
header('Content-type: ' . $mime_type);
}
if(stristr( $mime_type, 'gif' ) ) {
imagegif( $image_resized, $cache_file_name );
} elseif( stristr( $mime_type, 'jpeg' ) ) {
imagejpeg( $image_resized, $cache_file_name, $quality );
} elseif( stristr( $mime_type, 'png' ) ) {
imagepng( $image_resized, $cache_file_name, ceil( $quality / 10 ) );
}
if( $is_writable ) { show_cache_file( $cache_dir, $quality, $zoom_crop ); }
exit;
}
function open_image ($mime_type, $src) {
if(stristr($mime_type, 'gif')) {
$image = imagecreatefromgif($src);
} elseif(stristr($mime_type, 'jpeg')) {
$image = imagecreatefromjpeg($src);
} elseif(stristr($mime_type, 'png')) {
$image = imagecreatefrompng($src);
}
return $image;
}
function mime_type ($file) {
$frags = explode(".", $file);
$ext = strtolower(end($frags));
$types = array(
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'png' => 'image/png',
'gif' => 'image/gif',
'bmp' => 'image/bmp',
'doc' => 'application/msword',
'xls' => 'application/msword',
'xml' => 'text/xml',
'html' => 'text/html'
);
if (array_key_exists($ext, $types)) {
$mime_type = $types[$ext];
} else {
$mime_type = 'unknown';
}
return $mime_type;
}
function valid_src_mime_type ($mime_type) {
if( preg_match( "/jpg|jpeg|gif|png/i", $mime_type ) ) { return 1; }
return 0;
}
function check_cache ($cache_dir, $quality, $zoom_crop) {
// make sure cache dir exists
if(!file_exists($cache_dir)) {
// give 777 permissions so that developer can overwrite
// files created by web server user
mkdir($cache_dir);
chmod($cache_dir, 0777);
}
show_cache_file($cache_dir, $quality, $zoom_crop);
}
function show_cache_file ( $cache_dir,$quality, $zoom_crop ) {
$cache_file = get_cache_file($quality, $zoom_crop);
if( file_exists( $cache_dir . '/' . $cache_file ) ) {
// check for updates
$gmdate_mod = gmdate('D, d M Y H:i:s', filemtime( $cache_dir . '/' . $cache_file ) ) . " GMT";
if ( isset( $_SERVER['HTTP_IF_MODIFIED_SINCE' ] ) ) {
$if_modified_since = preg_replace( "/;.*$/", "", $_SERVER[ "HTTP_IF_MODIFIED_SINCE" ] );
if ( $if_modified_since >= $gmdate_mod ) {
header( "HTTP/1.1 304 Not Modified" );
die();
}
}
$dateNow = date("d M Y H:i:s");
$thumbModified = strtotime($dateNow);
// send headers then display image
header( "Content-Type: " . mime_type( $cache_file ) );
header( "Last-Modified: " . gmdate('D, d M Y H:i:s', $thumbModified) . " GMT" );
header( "Content-Length: " . filesize( $cache_dir . '/' . $cache_file ) );
header( "Cache-Control: max-age=9999" );
header( "Expires: " . gmdate( "D, d M Y H:i:s", time() + 99999 ) . "GMT");
readfile( $cache_dir . '/' . $cache_file );
die();
}
}
function get_cache_file ($quality, $zoom_crop) {
$request_params = $_REQUEST;
$cachename = $_REQUEST['src'] . $_REQUEST['w'] . $_REQUEST['h'] . $zoom_crop . $quality;
$cache_file = md5( $cachename );
return $cache_file;
}
?>