From b4afc6e05848e6476d5bebeb9c9d9e472f798b9a Mon Sep 17 00:00:00 2001 From: Stephen Holdaway Date: Thu, 18 Aug 2016 09:50:31 +1200 Subject: [PATCH] Work with higher level resize methods instead of getFormattedImage #17 raises an issue where a generate*Image method in a module has arguments that differ to the template-facing API, causing incorrect behaviour when used in a responsive image set. The arguments for the image resize methods exposed in templates ($Image.CroppedImage, $Image.PaddedImage, etc) are more appropriate for configuration via YAML as their parameters must be scalar to be written in a template. This change has the bonus of keeping the configuration API for this module consistent with use of image resize methods in templates, rather than the potentially different internal API of an image resize method. --- code/ResponsiveImageExtension.php | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/code/ResponsiveImageExtension.php b/code/ResponsiveImageExtension.php index 55a8d08..619c455 100644 --- a/code/ResponsiveImageExtension.php +++ b/code/ResponsiveImageExtension.php @@ -100,6 +100,11 @@ protected function createResponsiveSet($config, $defaultArgs, $set) $methodName = Config::inst()->get(__CLASS__, 'default_method'); } + if (!$this->owner->hasMethod($methodName)) { + throw new \RuntimeException(get_class($this->owner) . ' has no method ' . $methodName); + } + + // Create the resampled images for each query in the set $sizes = ArrayList::create(); foreach ($config['arguments'] as $query => $args) { if (is_numeric($query) || !$query) { @@ -110,26 +115,30 @@ protected function createResponsiveSet($config, $defaultArgs, $set) throw new Exception("Responsive set $set doesn't have any arguments provided for the query: $query"); } - array_unshift($args, $methodName); - $image = call_user_func_array(array($this->owner, 'getFormattedImage'), $args); $sizes->push(ArrayData::create(array( - 'Image' => $image, + 'Image' => $this->getResampledImage($methodName, $args), 'Query' => $query ))); } - // The first argument may be an image method such as 'CroppedImage' - if (!isset($defaultArgs[0]) || !$this->owner->hasMethod($defaultArgs[0])) { - array_unshift($defaultArgs, $methodName); - } - - $image = call_user_func_array(array($this->owner, 'getFormattedImage'), $defaultArgs); return $this->owner->customise(array( 'Sizes' => $sizes, - 'DefaultImage' => $image + 'DefaultImage' => $this->getResampledImage($methodName, $defaultArgs) ))->renderWith('ResponsiveImageSet'); } + /** + * Return a resampled image equivalent to $Image.MethodName(...$args) in a template + * + * @param string $methodName + * @param array $args + * @return Image + */ + protected function getResampledImage($methodName, $args) + { + return call_user_func(array($this->owner, '__call'), $methodName, $args); + } + /** * Due to {@link Object::allMethodNames()} requiring methods to be expressed * in all lowercase, getting the config for a given method requires a