Total Complexity | 40 |
Total Lines | 309 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ImgixImageTransform 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 ImgixImageTransform, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class ImgixImageTransform extends ImageTransform |
||
32 | { |
||
33 | // Constants |
||
34 | // ========================================================================= |
||
35 | |||
36 | const TRANSFORM_ATTRIBUTES_MAP = [ |
||
37 | 'width' => 'w', |
||
38 | 'height' => 'h', |
||
39 | 'quality' => 'q', |
||
40 | 'format' => 'fm', |
||
41 | ]; |
||
42 | |||
43 | const IMGIX_PURGE_ENDPOINT = 'https://api.imgix.com/v2/image/purger'; |
||
44 | |||
45 | // Static Methods |
||
46 | // ========================================================================= |
||
47 | |||
48 | /** |
||
49 | * @inheritdoc |
||
50 | */ |
||
51 | public static function displayName(): string |
||
54 | } |
||
55 | |||
56 | // Public Properties |
||
57 | // ========================================================================= |
||
58 | |||
59 | /** |
||
60 | * @var string |
||
61 | */ |
||
62 | public $domain; |
||
63 | |||
64 | /** |
||
65 | * @var string |
||
66 | */ |
||
67 | public $apiKey; |
||
68 | |||
69 | /** |
||
70 | * @var string |
||
71 | */ |
||
72 | public $securityToken; |
||
73 | |||
74 | // Public Methods |
||
75 | // ========================================================================= |
||
76 | |||
77 | /** |
||
78 | * @param Asset $asset |
||
79 | * @param AssetTransform|null $transform |
||
80 | * @param array $params |
||
81 | * |
||
82 | * @return string|null |
||
83 | * @throws \yii\base\Exception |
||
84 | * @throws \yii\base\InvalidConfigException |
||
85 | */ |
||
86 | public function getTransformUrl(Asset $asset, $transform, array $params = []) |
||
87 | { |
||
88 | $url = null; |
||
89 | $settings = ImageOptimize::$plugin->getSettings(); |
||
90 | |||
91 | $domain = $params['domain'] ?? 'demos.imgix.net'; |
||
92 | $builder = new UrlBuilder($domain); |
||
93 | if ($asset && $builder) { |
||
94 | $builder->setUseHttps(true); |
||
95 | if ($transform) { |
||
96 | // Map the transform properties |
||
97 | foreach (self::TRANSFORM_ATTRIBUTES_MAP as $key => $value) { |
||
98 | if (!empty($transform[$key])) { |
||
99 | $params[$value] = $transform[$key]; |
||
100 | } |
||
101 | } |
||
102 | // Remove any 'AUTO' settings |
||
103 | ArrayHelper::removeValue($params, 'AUTO'); |
||
104 | // Handle the Imgix auto setting for compression/format |
||
105 | $autoParams = []; |
||
106 | if (empty($params['q'])) { |
||
107 | $autoParams[] = 'compress'; |
||
108 | } |
||
109 | if (empty($params['fm'])) { |
||
110 | $autoParams[] = 'format'; |
||
111 | } |
||
112 | if (!empty($autoParams)) { |
||
113 | $params['auto'] = implode(',', $autoParams); |
||
114 | } |
||
115 | // Handle interlaced images |
||
116 | if (property_exists($transform, 'interlace')) { |
||
117 | if (($transform->interlace != 'none') |
||
118 | && (!empty($params['fm'])) |
||
119 | && ($params['fm'] == 'jpg') |
||
120 | ) { |
||
121 | $params['fm'] = 'pjpg'; |
||
122 | } |
||
123 | } |
||
124 | if ($settings->autoSharpenScaledImages) { |
||
125 | // See if the image has been scaled >= 50% |
||
126 | $widthScale = $asset->getWidth() / ($transform->width ?? $asset->getWidth()); |
||
127 | $heightScale = $asset->getHeight() / ($transform->height ?? $asset->getHeight()); |
||
128 | if (($widthScale >= 2.0) || ($heightScale >= 2.0)) { |
||
129 | $params['usm'] = 50.0; |
||
130 | } |
||
131 | } |
||
132 | // Handle the mode |
||
133 | switch ($transform->mode) { |
||
134 | case 'fit': |
||
135 | $params['fit'] = 'clamp'; |
||
136 | break; |
||
137 | |||
138 | case 'stretch': |
||
139 | $params['fit'] = 'scale'; |
||
140 | break; |
||
141 | |||
142 | default: |
||
143 | // Set a sane default |
||
144 | if (empty($transform->position)) { |
||
145 | $transform->position = 'center-center'; |
||
146 | } |
||
147 | // Fit mode |
||
148 | $params['fit'] = 'crop'; |
||
149 | $cropParams = []; |
||
150 | // Handle the focal point |
||
151 | $focalPoint = $asset->getFocalPoint(); |
||
152 | if (!empty($focalPoint)) { |
||
153 | $params['fp-x'] = $focalPoint['x']; |
||
154 | $params['fp-y'] = $focalPoint['y']; |
||
155 | $cropParams[] = 'focalpoint'; |
||
156 | } elseif (preg_match('/(top|center|bottom)-(left|center|right)/', $transform->position)) { |
||
157 | // Imgix defaults to 'center' if no param is present |
||
158 | $filteredCropParams = explode('-', $transform->position); |
||
159 | $filteredCropParams = array_diff($filteredCropParams, ['center']); |
||
160 | $cropParams[] = $filteredCropParams; |
||
161 | } |
||
162 | // Imgix |
||
163 | if (!empty($cropParams) && $transform->position !== 'center-center') { |
||
164 | $params['crop'] = implode(',', $cropParams); |
||
165 | } |
||
166 | break; |
||
167 | } |
||
168 | } else { |
||
169 | // No transform was passed in; so just auto all the things |
||
170 | $params['auto'] = 'format,compress'; |
||
171 | } |
||
172 | // Remove the api-key param |
||
173 | unset($params['api-key']); |
||
174 | // Apply the Security Token, if set |
||
175 | if (!empty($this->securityToken)) { |
||
176 | $builder->setSignKey($this->securityToken); |
||
177 | } |
||
178 | // Finally, create the Imgix URL for this transformed image |
||
179 | $assetUri = $this->getAssetUri($asset); |
||
180 | $url = $builder->createURL($assetUri, $params); |
||
181 | Craft::debug( |
||
182 | 'Imgix transform created for: '.$assetUri.' - Params: '.print_r($params, true).' - URL: '.$url, |
||
183 | __METHOD__ |
||
184 | ); |
||
185 | } |
||
186 | |||
187 | return $url; |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * @param string $url |
||
192 | * @param Asset $asset |
||
193 | * @param AssetTransform|null $transform |
||
194 | * @param array $params |
||
195 | * |
||
196 | * @return string |
||
197 | */ |
||
198 | public function getWebPUrl(string $url, Asset $asset, $transform, array $params = []): string |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * @param Asset $asset |
||
207 | * @param array $params |
||
208 | * |
||
209 | * @return null|string |
||
210 | * @throws \yii\base\InvalidConfigException |
||
211 | */ |
||
212 | public function getPurgeUrl(Asset $asset, array $params = []) |
||
213 | { |
||
214 | $url = null; |
||
215 | |||
216 | $domain = isset($params['domain']) |
||
217 | ? $params['domain'] |
||
218 | : 'demos.imgix.net'; |
||
219 | $builder = new UrlBuilder($domain); |
||
220 | if ($asset && $builder) { |
||
221 | $builder->setUseHttps(true); |
||
222 | // Create the Imgix URL for purging this image |
||
223 | $assetUri = $this->getAssetUri($asset); |
||
224 | $url = $builder->createURL($assetUri, $params); |
||
225 | // Strip the query string so we just pass in the raw URL |
||
226 | $url = UrlHelper::stripQueryString($url); |
||
227 | } |
||
228 | |||
229 | return $url; |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * @param string $url |
||
234 | * @param array $params |
||
235 | * |
||
236 | * @return bool |
||
237 | */ |
||
238 | public function purgeUrl(string $url, array $params = []): bool |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * @return array |
||
280 | */ |
||
281 | public function getTransformParams(): array |
||
282 | { |
||
283 | $params = [ |
||
284 | 'domain' => $this->domain, |
||
285 | 'api-key' => $this->apiKey, |
||
286 | ]; |
||
287 | |||
288 | return $params; |
||
289 | } |
||
290 | |||
291 | /** |
||
292 | * @param Asset $asset |
||
293 | * |
||
294 | * @return mixed |
||
295 | * @throws \yii\base\InvalidConfigException |
||
296 | */ |
||
297 | public function getAssetUri(Asset $asset) |
||
298 | { |
||
299 | $volume = $asset->getVolume(); |
||
300 | |||
301 | // If this is a local volume, it implies your are using a "Web Folder" |
||
302 | // source in Imgix. We can then also infer that: |
||
303 | // - This volume has URLs |
||
304 | // - The "Base URL" in Imgix is set to your domain root, per the ImageOptimize docs. |
||
305 | // |
||
306 | // Therefore, we need to parse the path from the full URL, so that it |
||
307 | // includes the path of the volume. |
||
308 | if ($volume instanceof \craft\volumes\Local) { |
||
309 | $assetUrl = AssetsHelper::generateUrl($volume, $asset); |
||
310 | $assetUri = parse_url($assetUrl, PHP_URL_PATH); |
||
311 | |||
312 | return $assetUri; |
||
313 | } |
||
314 | |||
315 | return parent::getAssetUri($asset); |
||
316 | } |
||
317 | |||
318 | /** |
||
319 | * @inheritdoc |
||
320 | */ |
||
321 | public function getSettingsHtml() |
||
325 | ]); |
||
326 | } |
||
327 | |||
328 | /** |
||
329 | * @inheritdoc |
||
330 | */ |
||
331 | public function rules() |
||
342 |