1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GNAHotelSolutions\ImageCacher; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
|
7
|
|
|
class Cacher |
8
|
|
|
{ |
9
|
|
|
/** @var string */ |
10
|
|
|
protected $cachePath; |
11
|
|
|
|
12
|
|
|
/** @var string */ |
13
|
|
|
protected $cacheRootPath; |
14
|
|
|
|
15
|
|
|
/** @var string */ |
16
|
|
|
protected $imagesRootPath; |
17
|
|
|
|
18
|
|
|
/** @var string */ |
19
|
|
|
protected $outputFormat = null; |
20
|
|
|
|
21
|
|
|
const SUPPORTED_OUTPUT_FORMATS = [Format::WEBP]; |
22
|
|
|
|
23
|
|
|
public function __construct( |
24
|
|
|
string $cachePath = 'cache/images', |
25
|
|
|
string $cacheRootPath = '', |
26
|
|
|
string $imagesRootPath = '', |
27
|
|
|
?string $outputFormat = null |
28
|
|
|
) { |
29
|
|
|
$this->cachePath = $cachePath; |
30
|
|
|
$this->cacheRootPath = rtrim($cacheRootPath, '/'); |
31
|
|
|
$this->imagesRootPath = rtrim($imagesRootPath, '/'); |
32
|
|
|
$this->outputFormat = $outputFormat; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function setOutputFormat(string $format): self |
36
|
|
|
{ |
37
|
|
|
if (! in_array($format, self::SUPPORTED_OUTPUT_FORMATS)) { |
38
|
|
|
throw new Exception("Cannot transform files into `{$format}` because is not a supported format."); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$this->outputFormat = $format; |
42
|
|
|
|
43
|
|
|
return $this; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function resize($image, $width = null, $height = null): Image |
47
|
|
|
{ |
48
|
|
|
return $this->manipulate($image, $width, $height, false); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function crop($image, $width = null, $height = null): Image |
52
|
|
|
{ |
53
|
|
|
return $this->manipulate($image, $width, $height, true); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
protected function manipulate($image, $width = null, $height = null, bool $cropImage = false): Image |
57
|
|
|
{ |
58
|
|
|
if (is_string($image)) { |
59
|
|
|
$image = new Image($image, $this->imagesRootPath); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if ($this->isSmallerThanRequested($image, $width, $height)) { |
63
|
|
|
return $image; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$resizedWidth = $width ?? round($height * $image->getAspectRatio()); |
67
|
|
|
$resizedHeight = $height ?? round($width / $image->getAspectRatio()); |
68
|
|
|
|
69
|
|
|
if ($this->outputFormat !== null && $this->outputFormat !== $image->getOutputFormat()) { |
70
|
|
|
$image->setOutputFormat($this->outputFormat); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if ($this->isAlreadyCached($image, $resizedWidth, $resizedHeight)) { |
74
|
|
|
return new Image($this->getCachedImagePathName($image, $resizedWidth, $resizedHeight), $this->cacheRootPath); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$layout = imagecreatetruecolor($resizedWidth, $resizedHeight); |
78
|
|
|
|
79
|
|
|
if ($this->isAlpha($image)) { |
80
|
|
|
imagealphablending($layout, false); |
81
|
|
|
imagesavealpha($layout, true); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if ($cropImage) { |
85
|
|
|
[$cutWidth, $cutHeight] = $this->getCutEdges($image, $resizedWidth, $resizedHeight); |
|
|
|
|
86
|
|
|
$cutX = ($image->getWidth() - $cutWidth) / 2; |
|
|
|
|
87
|
|
|
$cutY = ($image->getHeight() - $cutHeight) / 2; |
|
|
|
|
88
|
|
|
} else { |
89
|
|
|
$cutWidth = $image->getWidth(); |
90
|
|
|
$cutHeight = $image->getHeight(); |
91
|
|
|
$cutX = 0; |
92
|
|
|
$cutY = 0; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
imagecopyresampled($layout, $this->getImageResource($image), 0, 0, $cutX, $cutY, $resizedWidth, $resizedHeight, $cutWidth, $cutHeight); |
|
|
|
|
96
|
|
|
|
97
|
|
|
$this->saveImage($image, $layout, $resizedWidth, $resizedHeight); |
98
|
|
|
|
99
|
|
|
return new Image($this->getCachedImagePathName($image, $resizedWidth, $resizedHeight), $this->cacheRootPath); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
protected function isSmallerThanRequested(Image $image, $width, $height): bool |
103
|
|
|
{ |
104
|
|
|
return (! $width && ! $height) || $image->isSmallerThan($width, $height); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
protected function isAlreadyCached(Image $image, $width, $height): bool |
108
|
|
|
{ |
109
|
|
|
return file_exists($this->getCachedImageFullName($image, $width, $height)) |
110
|
|
|
&& $this->cachedImageIsTheSame($image, $width, $height); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
protected function cachedImageIsTheSame(Image $image, $width, $height): bool |
114
|
|
|
{ |
115
|
|
|
$cachedImageUpdatedAt = filemtime($this->getCachedImageFullName($image, $width, $height)); |
116
|
|
|
$imageUpdatedAt = filemtime($image->getOriginalFullPath()); |
117
|
|
|
|
118
|
|
|
return $cachedImageUpdatedAt >= $imageUpdatedAt; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
protected function getCachedImageFullName(Image $image, $width, $height): string |
122
|
|
|
{ |
123
|
|
|
if ($this->cacheRootPath === '') { |
124
|
|
|
return $this->getCachedImagePathName($image, $width, $height); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return "{$this->cacheRootPath}/{$this->getCachedImagePathName($image, $width, $height)}"; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
protected function getCachedImagePathName(Image $image, $width, $height): string |
131
|
|
|
{ |
132
|
|
|
return "{$this->cachePath}/{$this->getCachedImageName($image, $width, $height)}"; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
protected function getCachedImageName(Image $image, $width, $height): string |
136
|
|
|
{ |
137
|
|
|
return "{$this->getCacheImagePath($image->getPath(), $width, $height)}/{$image->getName()}"; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
protected function getCacheImagePath(string $path, $width, $height): string |
141
|
|
|
{ |
142
|
|
|
return ltrim("{$path}/{$width}x{$height}", '/'); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
protected function isAlpha(Image $image): bool |
146
|
|
|
{ |
147
|
|
|
return in_array($image->getType(), [Format::GIF, Format::PNG]); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
protected function getImageResource(Image $image) |
151
|
|
|
{ |
152
|
|
|
return Manipulator::create($image->getOutputFormat(), $image->getOriginalFullPath()); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
protected function getCutEdges(Image $image, int $width, int $height): array |
156
|
|
|
{ |
157
|
|
|
$aspectRatio = $width / $height; |
158
|
|
|
|
159
|
|
|
$cutEdgeWidth = round($image->getHeight() * $aspectRatio); |
160
|
|
|
|
161
|
|
|
if ($cutEdgeWidth > $image->getWidth()) { |
162
|
|
|
$cutEdgeWidth = $image->getWidth(); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
$cutEdgeHeight = round($cutEdgeWidth / $aspectRatio); |
166
|
|
|
|
167
|
|
|
return [$cutEdgeWidth, $cutEdgeHeight]; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
protected function saveImage(Image $image, $layout, $width, $height): string |
171
|
|
|
{ |
172
|
|
|
$this->createCacheDirectoryIfNotExists($image, $width, $height); |
173
|
|
|
|
174
|
|
|
return Manipulator::save($image->getOutputFormat(), $layout, $this->getCachedImageFullName($image, $width, $height)); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
protected function createCacheDirectoryIfNotExists(Image $image, $width, $height): void |
178
|
|
|
{ |
179
|
|
|
$cachePath = "{$this->cacheRootPath}/{$this->cachePath}/{$this->getCacheImagePath($image->getPath(), $width, $height)}"; |
180
|
|
|
|
181
|
|
|
if (! in_array(substr($cachePath, 0, 1), ['', '/'])) { |
182
|
|
|
$cachePath = "/{$cachePath}"; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
if (! file_exists($cachePath)) { |
186
|
|
|
mkdir($cachePath, 0777, true); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
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.