Conditions | 23 |
Paths | 2310 |
Total Lines | 99 |
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 |
||
57 | public static function getTransformUrl(Asset $asset, $transform, array $params = []) |
||
58 | { |
||
59 | $url = null; |
||
60 | $settings = ImageOptimize::$plugin->getSettings(); |
||
61 | |||
62 | $domain = isset($params['domain']) |
||
63 | ? $params['domain'] |
||
64 | : 'demos.imgix.net'; |
||
65 | $builder = new UrlBuilder($domain); |
||
66 | if ($asset && $builder) { |
||
67 | $builder->setUseHttps(true); |
||
68 | if ($transform) { |
||
69 | // Map the transform properties |
||
70 | foreach (self::TRANSFORM_ATTRIBUTES_MAP as $key => $value) { |
||
71 | if (!empty($transform[$key])) { |
||
72 | $params[$value] = $transform[$key]; |
||
73 | } |
||
74 | } |
||
75 | // Remove any 'AUTO' settings |
||
76 | ArrayHelper::removeValue($params, 'AUTO'); |
||
77 | // Handle the Imgix auto setting for compression/format |
||
78 | $autoParams = []; |
||
79 | if (empty($params['q'])) { |
||
80 | $autoParams[] = 'compress'; |
||
81 | } |
||
82 | if (empty($params['fm'])) { |
||
83 | $autoParams[] = 'format'; |
||
84 | } |
||
85 | if (!empty($autoParams)) { |
||
86 | $params['auto'] = implode(',', $autoParams); |
||
87 | } |
||
88 | // Handle interlaced images |
||
89 | if (property_exists($transform, 'interlace')) { |
||
90 | if (($transform->interlace != 'none') |
||
91 | && (!empty($params['fm'])) |
||
92 | && ($params['fm'] == 'jpg') |
||
93 | ) { |
||
94 | $params['fm'] = 'pjpg'; |
||
95 | } |
||
96 | } |
||
97 | if ($settings->autoSharpenScaledImages) { |
||
98 | // See if the image has been scaled >= 50% |
||
99 | $widthScale = $asset->getWidth() / ($transform->width ?? $asset->getWidth()); |
||
100 | $heightScale = $asset->getHeight() / ($transform->height ?? $asset->getHeight()); |
||
101 | if (($widthScale >= 2.0) || ($heightScale >= 2.0)) { |
||
102 | $params['usm'] = 50.0; |
||
103 | } |
||
104 | } |
||
105 | // Handle the mode |
||
106 | switch ($transform->mode) { |
||
107 | case 'fit': |
||
108 | $params['fit'] = 'clamp'; |
||
109 | break; |
||
110 | |||
111 | case 'stretch': |
||
112 | $params['fit'] = 'scale'; |
||
113 | break; |
||
114 | |||
115 | default: |
||
116 | // Fit mode |
||
117 | $params['fit'] = 'crop'; |
||
118 | $cropParams = []; |
||
119 | // Handle the focal point |
||
120 | $focalPoint = $asset->getFocalPoint(); |
||
121 | if (!empty($focalPoint)) { |
||
122 | $params['fp-x'] = $focalPoint['x']; |
||
123 | $params['fp-y'] = $focalPoint['y']; |
||
124 | $cropParams[] = 'focalpoint'; |
||
125 | } elseif (preg_match('/(top|center|bottom)-(left|center|right)/', $transform->position)) { |
||
126 | // Imgix defaults to 'center' if no param is present |
||
127 | $filteredCropParams = explode('-', $transform->position); |
||
128 | $filteredCropParams = array_diff($filteredCropParams, ['center']); |
||
129 | $cropParams[] = $filteredCropParams; |
||
130 | } |
||
131 | if (!empty($cropParams)) { |
||
132 | $params['crop'] = implode(',', $cropParams); |
||
133 | } |
||
134 | break; |
||
135 | } |
||
136 | } else { |
||
137 | // No transform was passed in; so just auto all the things |
||
138 | $params['auto'] = 'format,compress'; |
||
139 | } |
||
140 | // Remove the api-key param |
||
141 | unset($params['api-key']); |
||
142 | // Apply the Security Token, if set |
||
143 | if (!empty($settings->imgixSecurityToken)) { |
||
144 | $builder->setSignKey($settings->imgixSecurityToken); |
||
145 | } |
||
146 | // Finally, create the Imgix URL for this transformed image |
||
147 | $assetUri = self::getAssetUri($asset); |
||
148 | $url = $builder->createURL($assetUri, $params); |
||
149 | Craft::debug( |
||
150 | 'Imgix transform created for: '.$assetUri.' - Params: '.print_r($params, true).' - URL: '.$url, |
||
151 | __METHOD__ |
||
152 | ); |
||
153 | } |
||
154 | |||
155 | return $url; |
||
156 | } |
||
275 |