From 84462c48f8e226ab752ef73f44b3475e551fe512 Mon Sep 17 00:00:00 2001 From: Mark Croxton Date: Fri, 5 Apr 2024 17:58:08 +0100 Subject: [PATCH] Add a provider for Craft native image transforms, that can be used as a fallback or for local dev. --- src/providers/CraftProvider.php | 127 ++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 src/providers/CraftProvider.php diff --git a/src/providers/CraftProvider.php b/src/providers/CraftProvider.php new file mode 100644 index 0000000..be49a1a --- /dev/null +++ b/src/providers/CraftProvider.php @@ -0,0 +1,127 @@ +craftTransformUrl($asset, $params); + } + + /** + * Generate an image transform URL using Craft's transform service + * + * @access protected + * @param Asset $asset The asset + * @param array $params An array of parameters + * @return string|null + * @throws InvalidConfigException + */ + protected function craftTransformUrl(Asset $asset, array $params): ?string + { + // Only allow a limited set of params that we can map to Craft native params + $allowedParams = array_flip(array('w', 'h', 'q', 'fm', 'fit', 'crop', 'fp-x', 'fp-y', 'fill-color')); + $params = array_intersect_key($params, $allowedParams); + + // Map core params to native params + $mappedParams = []; + foreach ($params as $key => $value) { + switch ($key) { + + case 'w' : + $mappedParams['width'] = $value; + break; + + case 'h' : + $mappedParams['height'] = $value; + break; + + case 'q' : + $mappedParams['quality'] = $value; + break; + + case 'fm' : + if (in_array($value, ['auto', 'jpg', 'gif', 'png', 'webp', 'avif'])) { + $mappedParams['format'] = $value; + } + break; + + case 'fill-color' : + $mappedParams['fill'] = $value; + break; + + case 'fit' : + if ($value === 'fill' || $value === 'clip' || $value === 'min' || $value === 'max' || $value === 'fillmax') { + $mappedParams['mode'] = 'fit'; + } + elseif ($value === 'crop' || $value === 'facearea') { + $mappedParams['mode'] = 'crop'; + } + elseif ($value === 'scale') { + $mappedParams['mode'] = 'stretch'; + } + break; + + case 'crop' : + if ($value === 'top') { + $mappedParams['crop'] = 'Top-Center'; + } elseif ($value === 'top,left') { + $mappedParams['crop'] = 'Top-Left'; + } elseif ($value === 'top,right') { + $mappedParams['crop'] = 'Top-Right'; + } elseif ($value === 'bottom') { + $mappedParams['crop'] = 'Bottom-Center'; + } elseif ($value === 'bottom,left') { + $mappedParams['crop'] = 'Bottom-Left'; + } elseif ($value === 'bottom,right') { + $mappedParams['crop'] = 'Bottom-Right'; + } elseif ($value === 'left') { + $mappedParams['crop'] = 'Center-Left'; + } elseif ($value === 'right') { + $mappedParams['crop'] = 'Center-Right'; + } else { + $mappedParams['crop'] = 'Center-Center'; + } + break; + + default : + $mappedParams[$key] = $value; + break; + } + } + + // letterbox fill color + if (isset($mappedParams['fill'])) { + $mappedParams['mode'] = 'letterbox'; + } + + // generate a native transform url + $transform = new ImageTransform($mappedParams); + return $asset->getUrl($transform,true); + + } +} \ No newline at end of file