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