Conditions | 24 |
Paths | 2019 |
Total Lines | 102 |
Code Lines | 63 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
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($settings->imgixSecurityToken)) { |
||
176 | $builder->setSignKey($settings->imgixSecurityToken); |
||
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 | } |
||
333 |