image.php
1.75 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
<?php
N2Loader::import('libraries.cache.cache');
class N2CacheImage extends N2Cache {
protected $_storageEngine = 'filesystem';
protected function getScope() {
return 'image';
}
public function makeCache($fileExtension, $callable, $parameters = array(), $hash = false) {
if (!$hash) {
$hash = $this->generateHash($fileExtension, $callable, $parameters);
}
$keepFileName = pathinfo($parameters[1], PATHINFO_FILENAME);
$fileName = $hash . (!empty($keepFileName) ? '/' . $keepFileName : '') . '.' . $fileExtension;
if (!$this->exists($fileName)) {
$this->set($fileName, call_user_func_array($callable, $parameters));
}
return $this->getPath($fileName);
}
private function generateHash($fileExtension, $callable, $parameters) {
return md5(json_encode(array(
$fileExtension,
$callable,
$parameters
)));
}
}
class N2StoreImage extends N2Cache {
protected $_storageEngine = 'filesystem';
protected function getScope() {
return 'image';
}
public function makeCache($fileName, $content) {
if (!$this->isImage($fileName)) {
return false;
}
if (!$this->exists($fileName)) {
$this->set($fileName, $content);
}
return $this->getPath($fileName);
}
private function isImage($fileName) {
$supported_image = array(
'gif',
'jpg',
'jpeg',
'png',
'mp4',
'mp3'
);
$ext = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
if (in_array($ext, $supported_image)) {
return true;
}
return false;
}
}