-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathImageCache.php
executable file
·441 lines (395 loc) · 14.8 KB
/
ImageCache.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
<?php
/**
* The primary class file for PHP Image Compressor & Caching
*
* This file is to be used in any PHP project that requires image compression
*
* @package PHP Image Compressor & Caching
* @author Erik Nielsen <[email protected] | @erikkylenielsen>
* @license http://freedomdefined.org/Licenses/CC-BY MIT
* @version GIT: 1.0.0
*
* http://dtbaker.net/web-development/how-to-cache-images-generated-by-php/
*/
namespace ImageCache;
ob_start();
class ImageCache {
const memory_value = 128;
/**
* Stores the image source given for reference
*/
public $image_src;
/**
* The version of the source image to which will be part of the cached filename (used to "force" browser to download a new version of the image).
* @var string
*/
public $image_src_version;
/**
* If the file is remote or not
*/
public $is_remote;
/**
* Allow the user to set the options for the setup
*/
public $options;
/**
* The location of the cached images directory
*/
public $cached_image_directory;
/**
* The version of the cached directory to which will be part of the cached filename (used to "force" browser to download a new version of the entire cache directory).
* @var string
*/
public $cached_directory_version;
/**
* The URL to access the cached images directory
*/
public $cached_image_url;
/**
* The name of the cached file
*/
public $cached_filename;
/**
* The compression's output quality
* @var array
*/
public $quality;
/**
* check link of cached file if is valid by curl
*/
public $check_link_cached;
/**
* Stores the server's version of the GD Library, if enabled
*/
private $gd_version;
/**
* The memory limit currently established on the server
*/
private $memory_limit;
/**
* The file mime type
*/
private $file_mime_type;
/**
* The extension of the file
*/
private $file_extension;
/**
* The original size of the file
*/
private $local_image_src;
/**
* The original size of the file
*/
private $src_filesize;
/**
* The extension of the file
*/
private $cached_filesize;
/**
* Constructor function
*
* @param array $options Options include the keys 'echo' (boolean) and 'cache_time' (integer). 'cache_time' is currently not employed, but in place for future reference.
* @return object Returns the class object for the user to reference it in the future.
*/
public function __construct($options = array()) {
if (!$this->can_run_image_cache())
$this->error('PHP Image Cache must be run on a server with a bundled GD version.');
$defaults = array(
'check_link_cached' => true, // Check link of cached file if is valid by curl
'echo' => false, // Determines whether the resulting source should be echoed or returned
'cache_time' => 0, // How long the image should be cached for. If the value is 0, then the cache never expires. Default is 0, never expires.m
"quality" => array(// Determines the quality of cache output
"jpeg" => 85,
"png" => 8
),
"cached_image_directory" => dirname(__FILE__) . "/php-image-cache",
"cached_image_url" => "",
"cached_directory_version" => ""
);
$this->options = (object) array_merge($defaults, $options);
$this->cached_image_directory = $this->options->cached_image_directory;
$this->cached_image_url = rtrim($this->options->cached_image_url, "/");
$this->quality = (object) array(
"jpeg" => $this->options->quality["jpeg"],
"png" => $this->options->quality["png"]
);
$this->cached_directory_version = $this->options->cached_directory_version;
$this->check_link_cached = $this->options->check_link_cached;
return $this;
}
/**
* Validates whether the user can use this class or not, based on the GD Version their server is carrying.
*
* @return bool
*/
public function can_run_image_cache() {
$gd_info = gd_info();
$this->gd_version = false;
if (preg_match('#bundled \((.+)\)$#i', $gd_info['GD Version'], $matches)) {
$this->gd_version = (float) $matches[1];
} else {
$this->gd_version = (float) substr($gd_info['GD Version'], 0, 3);
}
return (bool) $this->gd_version;
}
/**
* Caches the image. When using this method, the image will be compressed and then cached.
*
* @return string The source file to be referenced after compressing an image
*/
public function cache($image, $version = "") {
ob_start();
if ( ! is_writable($this->cached_image_directory))
$this->error( $this->cached_image_directory . ' must writable!');
if (!is_string($image))
$this->error('Image source given must be a string.');
$this->image_src = $image;
$this->image_src_version = $version;
$this->pre_set_class_vars();
// If the image hasn't been server up at this point, fetch, compress, cache, and return
if ($this->cached_file_exists()) {
$this->src_filesize = filesize($this->image_src);
$this->cached_filesize = filesize($this->cached_filename);
if ($this->src_filesize < $this->cached_filesize) {
ob_end_clean();
return $this->docroot_to_url($this->image_src);
}
ob_end_clean();
return $this->docroot_to_url();
}
if ($this->is_remote) {
$this->download_image();
}
if (!$this->fetch_image())
$this->error('Could not copy image resource.');
$this->src_filesize = filesize($this->image_src);
$this->cached_filesize = filesize($this->cached_filename);
if ($this->src_filesize < $this->cached_filesize) {
ob_end_clean();
return $this->docroot_to_url($this->image_src);
}
ob_end_clean();
return $this->docroot_to_url();
}
/**
* Downloads a remote file and stores it locally to be used for compression
*/
private function download_image() {
$image_resource = file_get_contents($this->image_src);
$basename = basename($this->image_src);
if (!stripos($basename, '.' . $this->file_extension)) {
$basename .= '.' . $this->file_extension;
}
$image_location = dirname($this->cached_image_directory) . '/' . $basename;
if (!file_exists($image_location)) {
if (!file_put_contents($image_location, $image_resource)) {
$this->error('Could not download the remote image');
}
}
$this->image_src = $image_location;
}
/**
* Creates the cached directory
*
* @return bool If the directory was successfully created or not
*/
private function make_cache_directory() {
if (is_dir($this->cached_image_directory)) {
return true;
}
try {
mkdir($this->cached_image_directory);
} catch (Exception $e) {
$this->error('There was an error creating the new directory:', $e);
return false;
}
return true;
}
/**
* Fetch the image as a resource and save it into the cache directory.
*
* @source http://stackoverflow.com/questions/9839150/image-compression-in-php
* @return If the image was successfully created or not
*/
private function fetch_image() {
$image_size = getimagesize($this->image_src);
$image_width = $image_size[0];
$image_height = $image_size[1];
$mime_array = explode('/', $this->file_mime_type);
$file_mime_as_ext = end($mime_array);
$image_dest_func = 'imagecreate';
if ($this->gd_version >= 2)
$image_dest_func = 'imagecreatetruecolor';
if (in_array($file_mime_as_ext, array('gif', 'jpeg', 'png'))) {
$image_src_func = 'imagecreatefrom' . $this->file_extension;
$image_create_func = 'image' . $this->file_extension;
} else {
$this->error('The image you supply must have a .gif, .jpg/.jpeg, or .png extension.');
return false;
}
$this->increase_memory_limit();
$image_src = @call_user_func($image_src_func, $this->image_src);
$image_dest = @call_user_func($image_dest_func, $image_width, $image_height);
if ($file_mime_as_ext === 'jpeg') {
$background = imagecolorallocate($image_dest, 255, 255, 255);
imagefill($image_dest, 0, 0, $background);
} elseif (in_array($file_mime_as_ext, array('gif', 'png'))) {
imagealphablending($image_src, false);
imagesavealpha($image_src, true);
imagealphablending($image_dest, false);
imagesavealpha($image_dest, true);
}
imagecopy($image_dest, $image_src, 0, 0, 0, 0, $image_width, $image_height);
switch ($file_mime_as_ext) {
case 'jpeg':
$created = imagejpeg($image_dest, $this->cached_filename, $this->quality->jpeg);
break;
case 'png':
$created = imagepng($image_dest, $this->cached_filename, $this->quality->png);
break;
case 'gif':
$created = imagegif($image_dest, $this->cached_filename);
break;
default:
return false;
break;
}
imagedestroy($image_src);
imagedestroy($image_dest);
$this->reset_memory_limit();
return $created;
}
/**
* Returns
*
* @param string $src The url to check validate
* @return string The URL of the image
*/
private function docroot_to_url($src = null) {
if (is_null($src)) {
$src = $this->cached_filename;
}
if (empty($this->cached_image_url)) {
$image_path = str_replace($_SERVER['DOCUMENT_ROOT'], '', $src);
$image_url = $_SERVER['REQUEST_SCHEME']."://" . $_SERVER['HTTP_HOST'].'/' . substr($image_path, 1);
} else {
$image_url = $this->cached_image_url . "/" . basename($src);
}
if ($this->link_is_broken($image_url)) {
$this->error('Final image URL is broken');
}
return $image_url;
}
/**
* Sets up all class variables in one central function.
*/
private function pre_set_class_vars() {
$this->set_file_mime_type();
$this->set_cached_filename();
$this->set_memory_limit();
$this->set_is_remote();
}
/**
* Utility function to determine of the link in question is broken or not.
*
* @param string $url The url to check validate
* @return bool Indicates whether or not the link is broken
*/
private function link_is_broken($url) {
if (!$this->check_link_cached || !function_exists('curl_init'))
return false;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code == 404) {
$broken = true;
} else {
$broken = false;
}
curl_close($ch);
return $broken;
}
/**
* Quick and dirty way to see if the file is remote or local. Deeper checking comes
* later if we don't find a compressed version of the file locally.
*
* @return bool Whether or not the image is remote
*/
private function is_image_local() {
if (file_exists($this->image_src))
return true;
$parsed_src = parse_url($this->image_src);
if ($_SERVER['HTTP_HOST'] === $parsed_src['host'])
return true;
return false;
}
/**
* Determines if the image is local or being referenced from a remote URL
*/
private function set_is_remote() {
$this->is_remote = !$this->is_image_local();
}
/**
* This function creates the filename of the new image using an MD5 hash of the original filename. This helps to create a unique
* filename for the newly compressed image so that this class can easily find and determine if the image has already been compressed and stored locally.
*
* Employing use of the "alt" and "title" tags on the image (which hopefully you're already doing) will negate any potentially negative impacts on SEO. See
* this article for more information: http://searchenginewatch.com/article/2120682/Image-Optimization-How-to-Rank-on-Image-Search
*/
private function set_cached_filename() {
$pathinfo = pathinfo($this->image_src);
$this->cached_filename = $this->cached_image_directory . '/' . md5($this->cached_directory_version . basename($this->image_src) . $this->image_src_version) . '.' . $this->file_extension;
}
/**
* Simply determines if a compressed of the image that's sent is already compressed or not.
*/
private function cached_file_exists() {
if ($this->is_remote) {
$this->download_image();
}
if (file_exists($this->cached_filename))
return true;
return false;
}
/**
* Stores the file's mime type and validates that the file being compressed is indeed an image.
*/
private function set_file_mime_type() {
$image_type = exif_imagetype($this->image_src);
if (!$image_type)
$this->error('The file you supplied isn\'t a valid image.');
$this->file_mime_type = image_type_to_mime_type($image_type);
$this->file_extension = image_type_to_extension($image_type, false);
}
/**
* Stores the original value of the server's memory limit
*/
private function set_memory_limit() {
$this->memory_limit = ini_get('memory_limit');
}
/**
* Temporarily increases the servers memory limit to 2480 MB to handle building larger images. Based on initial
* tests, this seems to be a large enough increase to handle rebuilding jpg images as large as 4300px wide with no pre-compression.
*/
private function increase_memory_limit() {
ini_set('memory_limit', '2480M');
}
/**
* Resets the servers memory limit to its original value
*/
private function reset_memory_limit() {
ini_set('memory_limit', $this->memory_limit);
}
/**
* Displays an error and kills the script
*
* @param String $status The message to be passed to the native `exit()` function
*/
private function error($status = null) {
if (is_null($status))
$status = 'Unknown Error:';
exit($status);
}
}