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
|
|
|
public function __construct(string $cachePath = 'cache/images', string $cacheRootPath = '', string $imagesRootPath = '') |
17
|
|
|
{ |
18
|
|
|
$this->cachePath = $cachePath; |
19
|
|
|
$this->cacheRootPath = rtrim($cacheRootPath, '/'); |
20
|
|
|
$this->imagesRootPath = rtrim($imagesRootPath, '/'); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function resize($image, $width = null, $height = null): Image |
24
|
|
|
{ |
25
|
|
|
return $this->manipulate($image, $width, $height, false); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function crop($image, $width = null, $height = null): Image |
29
|
|
|
{ |
30
|
|
|
return $this->manipulate($image, $width, $height, true); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
protected function manipulate($image, $width = null, $height = null, bool $cropImage = false): Image |
34
|
|
|
{ |
35
|
|
|
if (is_string($image)) { |
36
|
|
|
$image = new Image($image, $this->imagesRootPath); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
if ($this->isSmallerThanRequested($image, $width, $height)) { |
40
|
|
|
return $image; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$resizedWidth = $width ?? round($height * $image->getAspectRatio()); |
44
|
|
|
$resizedHeight = $height ?? round($width / $image->getAspectRatio()); |
45
|
|
|
|
46
|
|
|
if ($this->isAlreadyCached($image, $resizedWidth, $resizedHeight)) { |
47
|
|
|
return new Image($this->getCachedImagePathName($image, $resizedWidth, $resizedHeight), $this->cacheRootPath); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$layout = imagecreatetruecolor($resizedWidth, $resizedHeight); |
51
|
|
|
|
52
|
|
|
if ($this->isAlpha($image)) { |
53
|
|
|
imagealphablending($layout, false); |
54
|
|
|
imagesavealpha($layout, true); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if ($cropImage) { |
58
|
|
|
[$cutWidth, $cutHeight] = $this->getCutEdges($image, $resizedWidth, $resizedHeight); |
|
|
|
|
59
|
|
|
$cutX = ($image->getWidth() - $cutWidth) / 2; |
|
|
|
|
60
|
|
|
$cutY = ($image->getHeight() - $cutHeight) / 2; |
|
|
|
|
61
|
|
|
} else { |
62
|
|
|
$cutWidth = $image->getWidth(); |
63
|
|
|
$cutHeight = $image->getHeight(); |
64
|
|
|
$cutX = 0; |
65
|
|
|
$cutY = 0; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
imagecopyresampled($layout, $this->getImageResource($image), 0, 0, $cutX, $cutY, $resizedWidth, $resizedHeight, $cutWidth, $cutHeight); |
|
|
|
|
69
|
|
|
|
70
|
|
|
$this->saveImage($image, $layout, $resizedWidth, $resizedHeight); |
71
|
|
|
|
72
|
|
|
return new Image($this->getCachedImagePathName($image, $resizedWidth, $resizedHeight), $this->cacheRootPath); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
protected function isSmallerThanRequested(Image $image, $width, $height): bool |
76
|
|
|
{ |
77
|
|
|
return (! $width && ! $height) || $image->isSmallerThan($width, $height); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
protected function isAlreadyCached(Image $image, $width, $height): bool |
81
|
|
|
{ |
82
|
|
|
return file_exists($this->getCachedImageFullName($image, $width, $height)) |
83
|
|
|
&& $this->cachedImageIsTheSame($image, $width, $height); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
protected function cachedImageIsTheSame(Image $image, $width, $height): bool |
87
|
|
|
{ |
88
|
|
|
$cachedImageUpdatedAt = filemtime($this->getCachedImageFullName($image, $width, $height)); |
89
|
|
|
$imageUpdatedAt = filemtime($image->getOriginalFullPath()); |
90
|
|
|
|
91
|
|
|
return $cachedImageUpdatedAt >= $imageUpdatedAt; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
protected function getCachedImageFullName(Image $image, $width, $height): string |
95
|
|
|
{ |
96
|
|
|
if ($this->cacheRootPath === '') { |
97
|
|
|
return $this->getCachedImagePathName($image, $width, $height); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return "{$this->cacheRootPath}/{$this->getCachedImagePathName($image, $width, $height)}"; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
protected function getCachedImagePathName(Image $image, $width, $height): string |
104
|
|
|
{ |
105
|
|
|
return "{$this->cachePath}/{$this->getCachedImageName($image, $width, $height)}"; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
protected function getCachedImageName(Image $image, $width, $height): string |
109
|
|
|
{ |
110
|
|
|
return "{$this->getCacheImagePath($image->getPath(), $width, $height)}/{$image->getName()}"; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
protected function getCacheImagePath(string $path, $width, $height): string |
114
|
|
|
{ |
115
|
|
|
return ltrim("{$path}/{$width}x{$height}", '/'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
protected function isAlpha(Image $image): bool |
119
|
|
|
{ |
120
|
|
|
return in_array($image->getType(), ['gif', 'png']); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
protected function getImageResource(Image $image) |
124
|
|
|
{ |
125
|
|
|
if ($image->getType() === 'jpeg') { |
126
|
|
|
return imagecreatefromjpeg($image->getOriginalFullPath()); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
if ($image->getType() === 'png') { |
130
|
|
|
return imagecreatefrompng($image->getOriginalFullPath()); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
if ($image->getType() === 'gif') { |
134
|
|
|
return imagecreatefromgif($image->getOriginalFullPath()); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
throw new \Exception("Image type [{$image->getType()}] not supported."); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
protected function getCutEdges(Image $image, int $width, int $height): array |
141
|
|
|
{ |
142
|
|
|
$aspectRatio = $width / $height; |
143
|
|
|
|
144
|
|
|
$cutEdgeWidth = round($image->getHeight() * $aspectRatio); |
145
|
|
|
|
146
|
|
|
if ($cutEdgeWidth > $image->getWidth()) { |
147
|
|
|
$cutEdgeWidth = $image->getWidth(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$cutEdgeHeight = round($cutEdgeWidth / $aspectRatio); |
151
|
|
|
|
152
|
|
|
return [$cutEdgeWidth, $cutEdgeHeight]; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
protected function saveImage(Image $image, $layout, $width, $height): string |
156
|
|
|
{ |
157
|
|
|
$this->createCacheDirectoryIfNotExists($image, $width, $height); |
158
|
|
|
|
159
|
|
|
if ($image->getType() === 'jpeg') { |
160
|
|
|
return imagejpeg($layout, $this->getCachedImageFullName($image, $width, $height)); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
if ($image->getType() === 'png') { |
164
|
|
|
return imagepng($layout, $this->getCachedImageFullName($image, $width, $height)); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
if ($image->getType() === 'gif') { |
168
|
|
|
return imagegif($layout, $this->getCachedImageFullName($image, $width, $height)); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
throw new \Exception("Image type [{$image->getType()}] not supported."); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
protected function createCacheDirectoryIfNotExists(Image $image, $width, $height): void |
175
|
|
|
{ |
176
|
|
|
$cachePath = "{$this->cacheRootPath}/{$this->cachePath}/{$this->getCacheImagePath($image->getPath(), $width, $height)}"; |
177
|
|
|
|
178
|
|
|
if (substr($cachePath, 0, 1) !== '') { |
179
|
|
|
$cachePath = "/{$cachePath}"; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
if (! file_exists($cachePath)) { |
183
|
|
|
mkdir($cachePath, 0777, true); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
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,
$x
would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.