Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions Classes/Aspects/ThumbnailAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -36,6 +39,12 @@ class ThumbnailAspect
*/
protected $sourceUriHelper;

/**
* @Flow\Inject
* @var ObjectManager
*/
protected $objectManager;

/**
* @Flow\Around("method(Neos\Media\Domain\Service\AssetService->getThumbnailUriAndSizeForAsset())")
*/
Expand All @@ -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']
);
Expand Down Expand Up @@ -112,7 +123,7 @@ public function generateImgproxyUri(JoinPointInterface $joinPoint): ?array
return [
'width' => $expectedSize->getWidth(),
'height' => $expectedSize->getHeight(),
'src' => $url->build()
'src' => $url->build(),
];
}

Expand Down
36 changes: 21 additions & 15 deletions Classes/ImgproxyBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

namespace Networkteam\ImageProxy;

use Neos\Media\Domain\Model\ThumbnailConfiguration;
use Networkteam\ImageProxy\Model\Dimensions;

class ImgproxyBuilder
class ImgproxyBuilder implements ImgproxyBuilderInterface
{

/**
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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) {
Expand All @@ -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), '+/', '-_'), '=');
}
Expand Down
19 changes: 19 additions & 0 deletions Classes/ImgproxyBuilderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Networkteam\ImageProxy;

use Networkteam\ImageProxy\Model\Dimensions;

interface ImgproxyBuilderInterface
{
public static function expectedSize(
Dimensions $actualDimension,
Dimensions $targetDimension,
string $resizingType,
bool $enlarge
): Dimensions;

public function buildUrl(string $sourceUrl): ImgproxyUrl;

public function generateUrl(ImgproxyUrl $imgproxyUrl): string;
}
27 changes: 26 additions & 1 deletion Classes/ImgproxyUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function fileName(string $path): self

public function build(): string
{
return $this->builder->generateUrl($this->url, $this->processingOptions, $this->extension);
return $this->builder->generateUrl($this);
}

public function quality(int $quality)
Expand All @@ -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;
}
}