From 6bb8e644eb7e9256bf36b49dcf4add0c9b55912c Mon Sep 17 00:00:00 2001 From: Cyrill Gsell Date: Mon, 25 Mar 2024 12:04:50 +0100 Subject: [PATCH 1/5] [TASK] Task-35198-18: Adding focuspoint to imgproxy url generation --- Classes/Aspects/ThumbnailAspect.php | 9 +++++++++ Classes/ImgproxyUrl.php | 5 +++++ 2 files changed, 14 insertions(+) diff --git a/Classes/Aspects/ThumbnailAspect.php b/Classes/Aspects/ThumbnailAspect.php index f6caf4a..ee97a80 100644 --- a/Classes/Aspects/ThumbnailAspect.php +++ b/Classes/Aspects/ThumbnailAspect.php @@ -9,6 +9,7 @@ use Neos\Media\Domain\Model\Image; use Neos\Media\Domain\Model\ImageVariant; use Neos\Media\Domain\Model\ThumbnailConfiguration; +use Neos\Utility\ObjectAccess; use Networkteam\ImageProxy\Eel\SourceUriHelper; use Networkteam\ImageProxy\ImgproxyBuilder; use Networkteam\ImageProxy\Model\Dimensions; @@ -109,6 +110,14 @@ public function generateImgproxyUri(JoinPointInterface $joinPoint): ?array $expectedSize = ImgproxyBuilder::expectedSize($actualDimension, $targetDimension, $resizingType, $enlarge); + $focusPointX = ObjectAccess::getProperty($configuration, 'focusPointX', true); + $focusPointY = ObjectAccess::getProperty($configuration, 'focusPointY', true); + if($focusPointX && $focusPointY){ + $focusPointX = ($focusPointX + 1) / 2; + $focusPointY = ($focusPointY + 1) / 2; + $url->focusPoint($focusPointX, $focusPointY); + } + return [ 'width' => $expectedSize->getWidth(), 'height' => $expectedSize->getHeight(), diff --git a/Classes/ImgproxyUrl.php b/Classes/ImgproxyUrl.php index 9401be2..c200f0c 100644 --- a/Classes/ImgproxyUrl.php +++ b/Classes/ImgproxyUrl.php @@ -76,4 +76,9 @@ public function cacheBuster(string $cacheBuster) { $this->processingOptions[] = 'cb:' . $cacheBuster; } + + public function focusPoint(float $focusPointX, float $focusPointY) + { + $this->processingOptions[] = 'gravity:fp:' . $focusPointX . ':' . $focusPointY; + } } From 3f6758ddce07d338892830418d0c13f79182f8ab Mon Sep 17 00:00:00 2001 From: Cyrill Gsell Date: Mon, 25 Mar 2024 12:21:47 +0100 Subject: [PATCH 2/5] [TASK] Task-35198-18: Check if focuspoint is set with is_float --- Classes/Aspects/ThumbnailAspect.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Classes/Aspects/ThumbnailAspect.php b/Classes/Aspects/ThumbnailAspect.php index ee97a80..8681848 100644 --- a/Classes/Aspects/ThumbnailAspect.php +++ b/Classes/Aspects/ThumbnailAspect.php @@ -112,7 +112,7 @@ public function generateImgproxyUri(JoinPointInterface $joinPoint): ?array $focusPointX = ObjectAccess::getProperty($configuration, 'focusPointX', true); $focusPointY = ObjectAccess::getProperty($configuration, 'focusPointY', true); - if($focusPointX && $focusPointY){ + if(is_float($focusPointX) && is_float($focusPointX)){ $focusPointX = ($focusPointX + 1) / 2; $focusPointY = ($focusPointY + 1) / 2; $url->focusPoint($focusPointX, $focusPointY); From 8e39f1970c14b8617bc8ad6b90d926a4505392de Mon Sep 17 00:00:00 2001 From: Cyrill Gsell Date: Thu, 28 Mar 2024 09:42:49 +0100 Subject: [PATCH 3/5] [FEATURE] Adding a hook to allow imgproxy url modification and adding additional processing options --- Classes/Aspects/ThumbnailAspect.php | 21 ++++++++++++------- Classes/ImgproxyUrl.php | 4 ++-- .../Model/ImgproxyUrlModifierInterface.php | 11 ++++++++++ Configuration/Settings.yaml | 4 ++++ 4 files changed, 31 insertions(+), 9 deletions(-) create mode 100644 Classes/Model/ImgproxyUrlModifierInterface.php diff --git a/Classes/Aspects/ThumbnailAspect.php b/Classes/Aspects/ThumbnailAspect.php index 8681848..85f0e29 100644 --- a/Classes/Aspects/ThumbnailAspect.php +++ b/Classes/Aspects/ThumbnailAspect.php @@ -9,10 +9,10 @@ use Neos\Media\Domain\Model\Image; use Neos\Media\Domain\Model\ImageVariant; use Neos\Media\Domain\Model\ThumbnailConfiguration; -use Neos\Utility\ObjectAccess; use Networkteam\ImageProxy\Eel\SourceUriHelper; use Networkteam\ImageProxy\ImgproxyBuilder; use Networkteam\ImageProxy\Model\Dimensions; +use Networkteam\ImageProxy\Model\ImgproxyUrlModifierInterface; /** * @Flow\Aspect @@ -110,12 +110,19 @@ public function generateImgproxyUri(JoinPointInterface $joinPoint): ?array $expectedSize = ImgproxyBuilder::expectedSize($actualDimension, $targetDimension, $resizingType, $enlarge); - $focusPointX = ObjectAccess::getProperty($configuration, 'focusPointX', true); - $focusPointY = ObjectAccess::getProperty($configuration, 'focusPointY', true); - if(is_float($focusPointX) && is_float($focusPointX)){ - $focusPointX = ($focusPointX + 1) / 2; - $focusPointY = ($focusPointY + 1) / 2; - $url->focusPoint($focusPointX, $focusPointY); + foreach ($this->settings['imgproxyUrlModifiers'] as $modifier){ + if(!str_contains($modifier, '->')){ + continue; + } + + [$class, $method] = explode('->', $modifier); + if(class_exists($class) && method_exists($class, $method)){ + $modifier = new $class(); + if(!$modifier instanceof ImgproxyUrlModifierInterface){ + throw new \Exception("The class $class must implement the ImgproxyUrlModifierInterface interface."); + } + $modifier->modify($url, $configuration); + } } return [ diff --git a/Classes/ImgproxyUrl.php b/Classes/ImgproxyUrl.php index c200f0c..a647010 100644 --- a/Classes/ImgproxyUrl.php +++ b/Classes/ImgproxyUrl.php @@ -77,8 +77,8 @@ public function cacheBuster(string $cacheBuster) $this->processingOptions[] = 'cb:' . $cacheBuster; } - public function focusPoint(float $focusPointX, float $focusPointY) + public function addProcessingOption($key, $value) { - $this->processingOptions[] = 'gravity:fp:' . $focusPointX . ':' . $focusPointY; + $this->processingOptions[] = $key . ':' . $value; } } diff --git a/Classes/Model/ImgproxyUrlModifierInterface.php b/Classes/Model/ImgproxyUrlModifierInterface.php new file mode 100644 index 0000000..eb0f344 --- /dev/null +++ b/Classes/Model/ImgproxyUrlModifierInterface.php @@ -0,0 +1,11 @@ + Date: Thu, 28 Mar 2024 10:56:29 +0100 Subject: [PATCH 4/5] [TASK] Removes ImgproxyUrlModifierInterface to remove package dependency of modifier class --- Classes/Aspects/ThumbnailAspect.php | 17 +++++------------ Classes/Model/ImgproxyUrlModifierInterface.php | 11 ----------- Configuration/Settings.yaml | 1 - 3 files changed, 5 insertions(+), 24 deletions(-) delete mode 100644 Classes/Model/ImgproxyUrlModifierInterface.php diff --git a/Classes/Aspects/ThumbnailAspect.php b/Classes/Aspects/ThumbnailAspect.php index 85f0e29..b39aba5 100644 --- a/Classes/Aspects/ThumbnailAspect.php +++ b/Classes/Aspects/ThumbnailAspect.php @@ -12,7 +12,6 @@ use Networkteam\ImageProxy\Eel\SourceUriHelper; use Networkteam\ImageProxy\ImgproxyBuilder; use Networkteam\ImageProxy\Model\Dimensions; -use Networkteam\ImageProxy\Model\ImgproxyUrlModifierInterface; /** * @Flow\Aspect @@ -110,18 +109,12 @@ public function generateImgproxyUri(JoinPointInterface $joinPoint): ?array $expectedSize = ImgproxyBuilder::expectedSize($actualDimension, $targetDimension, $resizingType, $enlarge); - foreach ($this->settings['imgproxyUrlModifiers'] as $modifier){ - if(!str_contains($modifier, '->')){ - continue; - } - - [$class, $method] = explode('->', $modifier); - if(class_exists($class) && method_exists($class, $method)){ - $modifier = new $class(); - if(!$modifier instanceof ImgproxyUrlModifierInterface){ - throw new \Exception("The class $class must implement the ImgproxyUrlModifierInterface interface."); + foreach ($this->settings['imgproxyUrlModifiers'] as $modifierClassName){ + if(class_exists($modifierClassName)){ + $modifier = new $modifierClassName(); + if(is_callable($modifier)){ + $modifier($url, $configuration); } - $modifier->modify($url, $configuration); } } diff --git a/Classes/Model/ImgproxyUrlModifierInterface.php b/Classes/Model/ImgproxyUrlModifierInterface.php deleted file mode 100644 index eb0f344..0000000 --- a/Classes/Model/ImgproxyUrlModifierInterface.php +++ /dev/null @@ -1,11 +0,0 @@ - Date: Thu, 28 Mar 2024 11:04:08 +0100 Subject: [PATCH 5/5] [TASK] Adding instructions to the readme on how to add a url modifier --- README.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/README.md b/README.md index b4b7171..43f8231 100644 --- a/README.md +++ b/README.md @@ -55,3 +55,25 @@ docker run -p 8080:8080 -it \ * Neos will **not generate any thumbnail**, they are generated ad-hoc from imgproxy as soon as a client requests it. Note: make sure to add a caching proxy / CDN on top of imgproxy, it has no built-in caching! + +## How to modify the imgproxy url +If you need to modify the imgproxy url for your custom needs you can use the following hook: +1. Create a modifier class in this format: +``` +namespace Foo\Bar; + +class ModifierClass +{ + public function __invoke(ImgproxyUrl $url, ThumbnailConfiguration $configuration): void + { + // modify the ImgproxyUrl object + } +} +``` +2. Register your modifier in the settings: +``` +Networkteam: + ImageProxy: + imgproxyUrlModifiers: + - Foo\Bar\ModifierClass +``` \ No newline at end of file