-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageCache.php
154 lines (132 loc) · 4.28 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
<?php
namespace drishu\yii2imagecache;
use yii\base\Component;
use yii\helpers\BaseFileHelper;
/**
* @author Szilagyi Andras <[email protected]>
*/
class ImageCache extends Component
{
/**
* The folder the images are coming from
* @TODO: support external images
* @var type
*/
public $sourcePath;
/**
* Path to cache folder
* @var type
*/
public $cachePath;
/**
* Tries to create a cached resized image.
* @TODO: try imagemagick and fallback to GD if not found
* @param type $path
* @param type $preset
* @param type $expires
* @return string
*/
public function get($path, $preset, $expires = 31536000)
{
if (strpos($path, 'http') !== false)
{
$sourceImagePath = $path;
}
else
{
$sourceImagePath = $this->sourcePath.$path;
}
$fileExtension = pathinfo($sourceImagePath, PATHINFO_EXTENSION);
$fileName = pathinfo($sourceImagePath, PATHINFO_FILENAME);
list($width, $height) = getimagesize($sourceImagePath);
$pathToSave = $this->cachePath.'/imagecache/'.$preset.'/'.$fileName.'.'.$fileExtension;
BaseFileHelper::createDirectory(dirname($this->sourcePath.$pathToSave), 0777, true);
// expired ?
if (is_file($this->sourcePath.$pathToSave) && $lastModified = filemtime($this->sourcePath.$pathToSave))
{
$expiresIn = $lastModified + $expires;
if (time() > $expiresIn)
{
unlink($this->sourcePath.$pathToSave);
}
}
// not expired but cached
if (is_file($this->sourcePath.$pathToSave))
{
return $pathToSave;
}
$preset = strtolower($preset);
$parts = explode('x', $preset);
$newWidth = intval($parts['0']);
$newHeight = isset($parts['1']) ? intval($parts['1']) : 0;
if (strpos($path, 'http') !== false)
{
// read the external image
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $path);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$content = curl_exec($ch);
curl_close($ch);
if (empty($content))
{
return $path;
}
$image = imagecreatefromstring($content);
}
else
{
switch($fileExtension)
{
case 'jpeg':
case 'jpg':
$image = imagecreatefromjpeg($sourceImagePath);
break;
case 'gif':
$image = imagecreatefromgif($sourceImagePath);
break;
case 'png':
$image = imagecreatefrompng($sourceImagePath);
break;
}
}
if (!empty($newWidth) && !empty($newHeight))
{
}
elseif (!empty($newWidth))
{
$x = ($newWidth * 100) / $width;
$newHeight = ceil(($height * $x) / 100);
}
elseif (!empty($newHeight))
{
$x = ($newHeight * 100) / $height;
$newWidth = ceil(($width * $x) / 100);
}
$tmp = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($tmp,$image,0,0,0,0,$newWidth,$newHeight,$width,$height);
switch($fileExtension)
{
case 'jpeg':
case 'jpg':
imagejpeg($tmp, $this->sourcePath.$pathToSave, 100);
break;
case 'gif':
imagegif($tmp, $this->sourcePath.$pathToSave, 100);
break;
case 'png':
imagealphablending($tmp, false);
imagesavealpha($tmp, true);
imagepng($tmp, $this->sourcePath.$pathToSave, 9);
break;
}
return $pathToSave;
}
/**
* Remove all cached images
*/
public function clearAll()
{
@BaseFileHelper::removeDirectory($this->sourcePath.$this->cachePath.'/imagecache/');
}
}