Total Complexity | 45 |
Total Lines | 278 |
Duplicated Lines | 0 % |
Changes | 7 | ||
Bugs | 3 | Features | 0 |
Complex classes like Image often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Image, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class Image |
||
29 | { |
||
30 | /** |
||
31 | * Create new manager instance with available driver. |
||
32 | */ |
||
33 | private static function manager(): ImageManager |
||
34 | { |
||
35 | $driver = null; |
||
36 | |||
37 | // ImageMagick is available? (for a future quality option) |
||
38 | if (\extension_loaded('imagick') && class_exists('Imagick')) { |
||
39 | $driver = ImagickDriver::class; |
||
40 | } |
||
41 | // Use GD, because it's the faster driver |
||
42 | if (\extension_loaded('gd') && \function_exists('gd_info')) { |
||
43 | $driver = GdDriver::class; |
||
44 | } |
||
45 | |||
46 | if ($driver) { |
||
47 | return ImageManager::withDriver( |
||
48 | $driver, |
||
49 | [ |
||
50 | 'autoOrientation' => true, |
||
51 | 'decodeAnimation' => true, |
||
52 | 'blendingColor' => 'ffffff', |
||
53 | 'strip' => true, // remove metadata |
||
54 | ] |
||
55 | ); |
||
56 | } |
||
57 | |||
58 | throw new RuntimeException('PHP GD (or Imagick) extension is required.'); |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * Scales down an image Asset to the given width, keeping the aspect ratio. |
||
63 | * |
||
64 | * @throws RuntimeException |
||
65 | */ |
||
66 | public static function resize(Asset $asset, int $width, int $quality): string |
||
77 | } |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * Crops an image Asset to the given width and height, keeping the aspect ratio. |
||
82 | * |
||
83 | * @throws RuntimeException |
||
84 | */ |
||
85 | public static function cover(Asset $asset, int $width, int $height, string $position, int $quality): string |
||
100 | } |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * Makes an image Asset maskable, meaning it can be used as a web app icon. |
||
105 | * |
||
106 | * @throws RuntimeException |
||
107 | */ |
||
108 | public static function maskable(Asset $asset, int $quality): string |
||
109 | { |
||
110 | try { |
||
111 | // creates image object from source |
||
112 | $source = self::manager()->read($asset['content']); |
||
113 | // creates a new image with the dominant color and adds a 15% margin |
||
114 | $image = self::manager()->create( |
||
115 | width: (int) round($asset['width'] * (1 + 15 / 100), 0), |
||
116 | height: (int) round($asset['height'] * (1 + 15 / 100), 0) |
||
117 | )->fill(self::getDominantColor($asset)); |
||
118 | // inserts the original image in the center |
||
119 | $image->place($source, position: 'center'); |
||
120 | // scales down the new image to the original image size |
||
121 | $image->scaleDown(width: $asset['width']); |
||
122 | // return image data |
||
123 | return (string) $image->encodeByMediaType($asset['subtype'], /** @scrutinizer ignore-type */ progressive: true, /** @scrutinizer ignore-type */ interlaced: false, quality: $quality); |
||
124 | } catch (\Exception $e) { |
||
125 | throw new RuntimeException(\sprintf('Can\'t make Asset "%s" maskable: %s', $asset['path'], $e->getMessage())); |
||
126 | } |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * Converts an image Asset to the target format. |
||
131 | * |
||
132 | * @throws RuntimeException |
||
133 | */ |
||
134 | public static function convert(Asset $asset, string $format, int $quality): string |
||
135 | { |
||
136 | try { |
||
137 | $image = self::manager()->read($asset['content']); |
||
138 | |||
139 | if (!\function_exists("image$format")) { |
||
140 | throw new RuntimeException(\sprintf('Function "image%s" is not available.', $format)); |
||
141 | } |
||
142 | |||
143 | return (string) $image->encodeByExtension($format, /** @scrutinizer ignore-type */ progressive: true, /** @scrutinizer ignore-type */ interlaced: false, quality: $quality); |
||
144 | } catch (\Exception $e) { |
||
145 | throw new RuntimeException(\sprintf('Not able to convert "%s" to %s: %s', $asset['path'], $format, $e->getMessage())); |
||
146 | } |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * Returns the Data URL (encoded in Base64). |
||
151 | * |
||
152 | * @throws RuntimeException |
||
153 | */ |
||
154 | public static function getDataUrl(Asset $asset, int $quality): string |
||
155 | { |
||
156 | try { |
||
157 | $image = self::manager()->read($asset['content']); |
||
158 | |||
159 | return (string) $image->encode(new AutoEncoder(quality: $quality))->toDataUri(); |
||
160 | } catch (\Exception $e) { |
||
161 | throw new RuntimeException(\sprintf('Can\'t get Data URL of "%s": %s', $asset['path'], $e->getMessage())); |
||
162 | } |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * Returns the dominant RGB color of an image asset. |
||
167 | * |
||
168 | * @throws RuntimeException |
||
169 | */ |
||
170 | public static function getDominantColor(Asset $asset): string |
||
171 | { |
||
172 | try { |
||
173 | $image = self::manager()->read(self::resize($asset, 100, 50)); |
||
174 | |||
175 | return $image->reduceColors(1)->pickColor(0, 0)->toString(); |
||
176 | } catch (\Exception $e) { |
||
177 | throw new RuntimeException(\sprintf('Can\'t get dominant color of "%s": %s', $asset['path'], $e->getMessage())); |
||
178 | } |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * Returns a Low Quality Image Placeholder (LQIP) as data URL. |
||
183 | * |
||
184 | * @throws RuntimeException |
||
185 | */ |
||
186 | public static function getLqip(Asset $asset): string |
||
187 | { |
||
188 | try { |
||
189 | $image = self::manager()->read(self::resize($asset, 100, 50)); |
||
190 | |||
191 | return (string) $image->blur(50)->encode()->toDataUri(); |
||
192 | } catch (\Exception $e) { |
||
193 | throw new RuntimeException(\sprintf('can\'t create LQIP of "%s": %s', $asset['path'], $e->getMessage())); |
||
194 | } |
||
195 | } |
||
196 | |||
197 | /** |
||
198 | * Build the `srcset` attribute for responsive images. |
||
199 | * e.g.: `srcset="/img-480.jpg 480w, /img-800.jpg 800w"`. |
||
200 | * |
||
201 | * @throws RuntimeException |
||
202 | */ |
||
203 | public static function buildSrcset(Asset $asset, array $widths): string |
||
204 | { |
||
205 | if (!self::isImage($asset)) { |
||
206 | throw new RuntimeException(\sprintf('can\'t build "srcset" of "%s": it\'s not an image file.', $asset['path'])); |
||
207 | } |
||
208 | |||
209 | $srcset = ''; |
||
210 | $widthMax = 0; |
||
211 | sort($widths, SORT_NUMERIC); |
||
212 | $widths = array_reverse($widths); |
||
213 | foreach ($widths as $width) { |
||
214 | if ($asset['width'] < $width) { |
||
215 | continue; |
||
216 | } |
||
217 | $img = $asset->resize($width); |
||
218 | $srcset .= \sprintf('%s %sw, ', (string) $img, $width); |
||
219 | $widthMax = $width; |
||
220 | } |
||
221 | // adds source image |
||
222 | if (!empty($srcset) && ($asset['width'] < max($widths) && $asset['width'] != $widthMax)) { |
||
223 | $srcset .= \sprintf('%s %sw', (string) $asset, $asset['width']); |
||
224 | } |
||
225 | |||
226 | return rtrim($srcset, ', '); |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * Returns the value of the "sizes" attribute corresponding to the configured class. |
||
231 | */ |
||
232 | public static function getSizes(string $class, array $sizes = []): string |
||
233 | { |
||
234 | $result = ''; |
||
235 | $classArray = explode(' ', $class); |
||
236 | foreach ($classArray as $class) { |
||
237 | if (\array_key_exists($class, $sizes)) { |
||
238 | $result = $sizes[$class] . ', '; |
||
239 | } |
||
240 | } |
||
241 | if (!empty($result)) { |
||
242 | return trim($result, ', '); |
||
243 | } |
||
244 | |||
245 | return $sizes['default'] ?? '100vw'; |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * Checks if an asset is an animated GIF. |
||
250 | */ |
||
251 | public static function isAnimatedGif(Asset $asset): bool |
||
260 | } |
||
261 | |||
262 | /** |
||
263 | * Returns true if asset is a SVG. |
||
264 | */ |
||
265 | public static function isSVG(Asset $asset): bool |
||
266 | { |
||
267 | return \in_array($asset['subtype'], ['image/svg', 'image/svg+xml']) || $asset['ext'] == 'svg'; |
||
268 | } |
||
269 | |||
270 | /** |
||
271 | * Returns true if asset is an ICO. |
||
272 | */ |
||
273 | public static function isIco(Asset $asset): bool |
||
274 | { |
||
275 | return \in_array($asset['subtype'], ['image/x-icon', 'image/vnd.microsoft.icon']) || $asset['ext'] == 'ico'; |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * Asset is a valid image? |
||
280 | */ |
||
281 | public static function isImage(Asset $asset): bool |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * Returns SVG attributes. |
||
292 | * |
||
293 | * @return \SimpleXMLElement|false |
||
294 | */ |
||
295 | public static function getSvgAttributes(Asset $asset) |
||
306 | } |
||
307 | } |
||
308 |