diff --git a/Classes/Aspects/ThumbnailAspect.php b/Classes/Aspects/ThumbnailAspect.php index f6caf4a..83a2f9e 100644 --- a/Classes/Aspects/ThumbnailAspect.php +++ b/Classes/Aspects/ThumbnailAspect.php @@ -5,12 +5,15 @@ use Neos\Flow\Annotations as Flow; use Neos\Flow\Aop\JoinPointInterface; use Neos\Flow\Mvc\ActionRequest; +use Neos\Flow\ObjectManagement\ObjectManager; use Neos\Media\Domain\Model\Asset; 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\ImgproxyBuilderInterface; use Networkteam\ImageProxy\Model\Dimensions; /** @@ -36,6 +39,12 @@ class ThumbnailAspect */ protected $sourceUriHelper; + /** + * @Flow\Inject + * @var ObjectManager + */ + protected $objectManager; + /** * @Flow\Around("method(Neos\Media\Domain\Service\AssetService->getThumbnailUriAndSizeForAsset())") */ @@ -56,8 +65,10 @@ public function generateImgproxyUri(JoinPointInterface $joinPoint): ?array /** @var ThumbnailConfiguration $configuration */ $configuration = $joinPoint->getMethodArgument('configuration'); - $builder = new ImgproxyBuilder( + $builder = $this->objectManager->get( + ImgproxyBuilderInterface::class, $this->settings['imgproxyUrl'], + $configuration, $this->settings['key'], $this->settings['salt'] ); @@ -112,7 +123,7 @@ public function generateImgproxyUri(JoinPointInterface $joinPoint): ?array return [ 'width' => $expectedSize->getWidth(), 'height' => $expectedSize->getHeight(), - 'src' => $url->build() + 'src' => $url->build(), ]; } diff --git a/Classes/ImgproxyBuilder.php b/Classes/ImgproxyBuilder.php index bb2c4d8..4200f1f 100644 --- a/Classes/ImgproxyBuilder.php +++ b/Classes/ImgproxyBuilder.php @@ -2,9 +2,10 @@ namespace Networkteam\ImageProxy; +use Neos\Media\Domain\Model\ThumbnailConfiguration; use Networkteam\ImageProxy\Model\Dimensions; -class ImgproxyBuilder +class ImgproxyBuilder implements ImgproxyBuilderInterface { /** @@ -20,23 +21,30 @@ class ImgproxyBuilder */ public const RESIZE_TYPE_FORCE = 'force'; - private string $imgproxyUrl; - private ?string $key = null; - private ?string $salt = null; + protected string $imgproxyUrl; + protected ThumbnailConfiguration $thumbnailConfiguration; + protected ?string $key = null; + protected ?string $salt = null; /** * @param string $imgproxyUrl The URL where imgproxy is publicly available + * @param ThumbnailConfiguration $thumbnailConfiguration * @param string|null $key * @param string|null $salt */ - public function __construct(string $imgproxyUrl, string $key = null, string $salt = null) - { + public function __construct( + string $imgproxyUrl, + ThumbnailConfiguration $thumbnailConfiguration, + string $key = null, + string $salt = null + ) { if ((string)$key !== '' && (string)$salt !== '') { $this->key = pack("H*", $key); $this->salt = pack("H*", $salt); } $this->imgproxyUrl = $imgproxyUrl; + $this->thumbnailConfiguration = $thumbnailConfiguration; } /** @@ -98,20 +106,18 @@ public function buildUrl(string $sourceUrl): ImgproxyUrl } /** - * @param string $sourceUrl - * @param string[] $processingOptions - * @param string|null $extension + * @param ImgproxyUrl $imgproxyUrl * @return string * @internal * Generate an imgproxy URL with processing options and signature (if key and salt are set). * */ - public function generateUrl(string $sourceUrl, array $processingOptions, ?string $extension): string + public function generateUrl(ImgproxyUrl $imgproxyUrl): string { - $encodedSourceUrl = self::base64UrlEncode($sourceUrl); - $pathAfterSignature = '/' . join('/', $processingOptions) . '/' . $encodedSourceUrl; - if ($extension !== null) { - $pathAfterSignature .= '.' . $extension; + $encodedSourceUrl = self::base64UrlEncode($imgproxyUrl->getUrl()); + $pathAfterSignature = '/' . implode('/', $imgproxyUrl->getProcessingOptions()) . '/' . $encodedSourceUrl; + if ($imgproxyUrl->getExtension() !== null) { + $pathAfterSignature .= '.' . $imgproxyUrl->getExtension(); } if ($this->key !== null) { @@ -124,7 +130,7 @@ public function generateUrl(string $sourceUrl, array $processingOptions, ?string } } - private static function base64UrlEncode(string $data): string + protected static function base64UrlEncode(string $data): string { return rtrim(strtr(base64_encode($data), '+/', '-_'), '='); } diff --git a/Classes/ImgproxyBuilderInterface.php b/Classes/ImgproxyBuilderInterface.php new file mode 100644 index 0000000..9f41651 --- /dev/null +++ b/Classes/ImgproxyBuilderInterface.php @@ -0,0 +1,19 @@ +builder->generateUrl($this->url, $this->processingOptions, $this->extension); + return $this->builder->generateUrl($this); } public function quality(int $quality) @@ -76,4 +76,29 @@ public function cacheBuster(string $cacheBuster) { $this->processingOptions[] = 'cb:' . $cacheBuster; } + + public function focusPoint(float $focusPointX, float $focusPointY) + { + $this->processingOptions[] = 'gravity:fp:' . $focusPointX . ':' . $focusPointY; + } + + public function getUrl(): string + { + return $this->url; + } + + public function getExtension(): ?string + { + return $this->extension; + } + + public function getProcessingOptions(): array + { + return $this->processingOptions; + } + + public function addProcessingOption(string $option) + { + $this->processingOptions[] = $option; + } }