|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GNAHotelSolutions\ImageCacher; |
|
4
|
|
|
|
|
5
|
|
|
class Cacher |
|
6
|
|
|
{ |
|
7
|
|
|
/** @var string */ |
|
8
|
|
|
protected $cachePath; |
|
9
|
|
|
|
|
10
|
|
|
/** @var string */ |
|
11
|
|
|
protected $cacheRootPath; |
|
12
|
|
|
|
|
13
|
|
|
/** @var string */ |
|
14
|
|
|
protected $imagesRootPath; |
|
15
|
|
|
|
|
16
|
|
|
/** @var string */ |
|
17
|
|
|
protected $requiredFormat; |
|
18
|
|
|
|
|
19
|
|
|
public function __construct(string $cachePath = 'cache/images', string $cacheRootPath = '', string $imagesRootPath = '') |
|
20
|
|
|
{ |
|
21
|
|
|
$this->cachePath = $cachePath; |
|
22
|
|
|
$this->cacheRootPath = rtrim($cacheRootPath, '/'); |
|
23
|
|
|
$this->imagesRootPath = rtrim($imagesRootPath, '/'); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function resize($image, $width = null, $height = null, $format = null): Image |
|
27
|
|
|
{ |
|
28
|
|
|
$this->requiredFormat = $format; |
|
29
|
|
|
|
|
30
|
|
|
return $this->manipulate($image, $width, $height, false); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function crop($image, $width = null, $height = null, $format = null): Image |
|
34
|
|
|
{ |
|
35
|
|
|
$this->requiredFormat = $format; |
|
36
|
|
|
|
|
37
|
|
|
return $this->manipulate($image, $width, $height, true); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
protected function manipulate($image, $width = null, $height = null, bool $cropImage = false): Image |
|
41
|
|
|
{ |
|
42
|
|
|
if (is_string($image)) { |
|
43
|
|
|
$image = new Image($image, $this->imagesRootPath); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
if ($this->isSmallerThanRequested($image, $width, $height)) { |
|
47
|
|
|
return $image; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$resizedWidth = $width ?? round($height * $image->getAspectRatio()); |
|
51
|
|
|
$resizedHeight = $height ?? round($width / $image->getAspectRatio()); |
|
52
|
|
|
|
|
53
|
|
|
if ($this->isAlreadyCached($image, $resizedWidth, $resizedHeight)) { |
|
54
|
|
|
return new Image($this->getCachedImagePathName($image, $resizedWidth, $resizedHeight), $this->cacheRootPath); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$layout = imagecreatetruecolor($resizedWidth, $resizedHeight); |
|
58
|
|
|
|
|
59
|
|
|
if ($this->isAlpha($image)) { |
|
60
|
|
|
imagealphablending($layout, false); |
|
61
|
|
|
imagesavealpha($layout, true); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
if ($cropImage) { |
|
65
|
|
|
[$cutWidth, $cutHeight] = $this->getCutEdges($image, $resizedWidth, $resizedHeight); |
|
|
|
|
|
|
66
|
|
|
$cutX = ($image->getWidth() - $cutWidth) / 2; |
|
|
|
|
|
|
67
|
|
|
$cutY = ($image->getHeight() - $cutHeight) / 2; |
|
|
|
|
|
|
68
|
|
|
} else { |
|
69
|
|
|
$cutWidth = $image->getWidth(); |
|
70
|
|
|
$cutHeight = $image->getHeight(); |
|
71
|
|
|
$cutX = 0; |
|
72
|
|
|
$cutY = 0; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
imagecopyresampled($layout, $this->getImageResource($image), 0, 0, $cutX, $cutY, $resizedWidth, $resizedHeight, $cutWidth, $cutHeight); |
|
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
$this->saveImage($image, $layout, $resizedWidth, $resizedHeight); |
|
78
|
|
|
|
|
79
|
|
|
return new Image($this->getCachedImagePathName($image, $resizedWidth, $resizedHeight), $this->cacheRootPath); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
protected function isSmallerThanRequested(Image $image, $width, $height): bool |
|
83
|
|
|
{ |
|
84
|
|
|
return (! $width && ! $height) || $image->isSmallerThan($width, $height); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
protected function isAlreadyCached(Image $image, $width, $height): bool |
|
88
|
|
|
{ |
|
89
|
|
|
return file_exists($this->getCachedImageFullName($image, $width, $height)) |
|
90
|
|
|
&& $this->cachedImageIsTheSame($image, $width, $height); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
protected function cachedImageIsTheSame(Image $image, $width, $height): bool |
|
94
|
|
|
{ |
|
95
|
|
|
$cachedImageUpdatedAt = filemtime($this->getCachedImageFullName($image, $width, $height)); |
|
96
|
|
|
$imageUpdatedAt = filemtime($image->getOriginalFullPath()); |
|
97
|
|
|
|
|
98
|
|
|
return $cachedImageUpdatedAt >= $imageUpdatedAt; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
protected function getCachedImageFullName(Image $image, $width, $height): string |
|
102
|
|
|
{ |
|
103
|
|
|
if ($this->cacheRootPath === '') { |
|
104
|
|
|
return $this->getCachedImagePathName($image, $width, $height); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return "{$this->cacheRootPath}/{$this->getCachedImagePathName($image, $width, $height)}"; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
protected function getCachedImagePathName(Image $image, $width, $height): string |
|
111
|
|
|
{ |
|
112
|
|
|
return "{$this->cachePath}/{$this->getCachedImageName($image, $width, $height)}"; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
protected function getCachedImageName(Image $image, $width, $height): string |
|
116
|
|
|
{ |
|
117
|
|
|
$imageName = explode('.', $image->getName())[0]; |
|
118
|
|
|
$imageName = $this->requiredFormat === null ? $image->getName() : "{$imageName}.{$this->requiredFormat}"; |
|
119
|
|
|
|
|
120
|
|
|
return "{$this->getCacheImagePath($image->getPath(), $width, $height)}/{$imageName}"; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
protected function getCacheImagePath(string $path, $width, $height): string |
|
124
|
|
|
{ |
|
125
|
|
|
return ltrim("{$path}/{$width}x{$height}", '/'); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
protected function isAlpha(Image $image): bool |
|
129
|
|
|
{ |
|
130
|
|
|
return in_array($image->getType(), ['gif', 'png']); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
protected function getImageResource(Image $image) |
|
134
|
|
|
{ |
|
135
|
|
|
if ($image->getType() === 'jpeg') { |
|
136
|
|
|
return imagecreatefromjpeg($image->getOriginalFullPath()); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
if ($image->getType() === 'png') { |
|
140
|
|
|
return imagecreatefrompng($image->getOriginalFullPath()); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
if ($image->getType() === 'gif') { |
|
144
|
|
|
return imagecreatefromgif($image->getOriginalFullPath()); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
if ($image->getType() === 'webp') { |
|
148
|
|
|
return imagecreatefromwebp($image->getOriginalFullPath()); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
throw new \Exception("Image type [{$image->getType()}] not supported."); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
protected function getCutEdges(Image $image, int $width, int $height): array |
|
155
|
|
|
{ |
|
156
|
|
|
$aspectRatio = $width / $height; |
|
157
|
|
|
|
|
158
|
|
|
$cutEdgeWidth = round($image->getHeight() * $aspectRatio); |
|
159
|
|
|
|
|
160
|
|
|
if ($cutEdgeWidth > $image->getWidth()) { |
|
161
|
|
|
$cutEdgeWidth = $image->getWidth(); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
$cutEdgeHeight = round($cutEdgeWidth / $aspectRatio); |
|
165
|
|
|
|
|
166
|
|
|
return [$cutEdgeWidth, $cutEdgeHeight]; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
protected function saveImage(Image $image, $layout, $width, $height): string |
|
170
|
|
|
{ |
|
171
|
|
|
$this->createCacheDirectoryIfNotExists($image, $width, $height); |
|
172
|
|
|
|
|
173
|
|
|
if($this->requiredFormat !== null) { |
|
174
|
|
|
$image->setType($this->requiredFormat); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
if ($image->getType() === 'jpeg') { |
|
178
|
|
|
return imagejpeg($layout, $this->getCachedImageFullName($image, $width, $height)); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
if ($image->getType() === 'png') { |
|
182
|
|
|
return imagepng($layout, $this->getCachedImageFullName($image, $width, $height)); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
if ($image->getType() === 'gif') { |
|
186
|
|
|
return imagegif($layout, $this->getCachedImageFullName($image, $width, $height)); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
if ($image->getType() === 'webp') { |
|
190
|
|
|
return imagewebp($layout, $this->getCachedImageFullName($image, $width, $height)); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
throw new \Exception("Image type [{$image->getType()}] not supported."); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
protected function createCacheDirectoryIfNotExists(Image $image, $width, $height): void |
|
197
|
|
|
{ |
|
198
|
|
|
$cachePath = "{$this->cacheRootPath}/{$this->cachePath}/{$this->getCacheImagePath($image->getPath(), $width, $height)}"; |
|
199
|
|
|
|
|
200
|
|
|
if (! in_array(substr($cachePath, 0, 1), ['', '/'])) { |
|
201
|
|
|
$cachePath = "/{$cachePath}"; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
if (! file_exists($cachePath)) { |
|
205
|
|
|
mkdir($cachePath, 0777, true); |
|
206
|
|
|
} |
|
207
|
|
|
} |
|
208
|
|
|
} |
|
209
|
|
|
|
This error can happen if you refactor code and forget to move the variable initialization.
Let’s take a look at a simple example:
The above code is perfectly fine. Now imagine that we re-order the statements:
In that case,
$xwould be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.