Skip to content

Commit

Permalink
Merge pull request #83 from sitegeist/bugfix/fixAsyncImages
Browse files Browse the repository at this point in the history
BUGFIX: Fix generation of async image uris
  • Loading branch information
mficzel authored Dec 5, 2024
2 parents e6aeca5 + 6fd5157 commit 855ee9e
Showing 1 changed file with 41 additions and 6 deletions.
47 changes: 41 additions & 6 deletions Classes/Domain/AssetImageSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@
namespace Sitegeist\Kaleidoscope\Domain;

use Neos\Flow\Annotations as Flow;
use Neos\Flow\Http\ServerRequestAttributes;
use Neos\Flow\Mvc\ActionRequest;
use Neos\Flow\Mvc\ActionRequestFactory;
use Neos\Flow\Mvc\Routing\Dto\RouteParameters;
use Neos\Flow\Mvc\Routing\UriBuilder;
use Neos\Media\Domain\Model\AssetInterface;
use Neos\Media\Domain\Model\AssetVariantInterface;
use Neos\Media\Domain\Model\ImageInterface;
use Neos\Media\Domain\Model\ThumbnailConfiguration;
use Neos\Media\Domain\Model\VariantSupportInterface;
use Neos\Media\Domain\Service\AssetService;
use Neos\Media\Domain\Service\ThumbnailService;
use Psr\Http\Message\ServerRequestFactoryInterface;
use Psr\Http\Message\UriFactoryInterface;

class AssetImageSource extends AbstractScalableImageSource
{
Expand All @@ -30,6 +36,24 @@ class AssetImageSource extends AbstractScalableImageSource
*/
protected $assetService;

/**
* @var UriFactoryInterface
* @Flow\Inject
*/
protected $uriFactory;

/**
* @var ServerRequestFactoryInterface
* @Flow\Inject
*/
protected $serverRequestFactory;

/**
* @var ActionRequestFactory
* @Flow\Inject
*/
protected $actionRequestFactory;

/**
* @var ImageInterface
*/
Expand Down Expand Up @@ -131,7 +155,6 @@ public function src(): string
$width = $this->getCurrentWidth();
$height = $this->getCurrentHeight();

$async = $this->request ? $this->async : false;
$allowCropping = true;
$allowUpScaling = $this->supportsUpscaling();
$thumbnailConfiguration = new ThumbnailConfiguration(
Expand All @@ -141,15 +164,27 @@ public function src(): string
$height,
$allowCropping,
$allowUpScaling,
$async,
$this->async,
$this->targetQuality,
$this->targetFormat
);

if ($this->request instanceof ActionRequest) {
$request = $this->request;
} else {
$uri = $this->uriFactory->createUri('http://localhost');
$httpRequest = $this->serverRequestFactory->createServerRequest('GET', $uri)
->withAttribute(
ServerRequestAttributes::ROUTING_PARAMETERS,
RouteParameters::createEmpty()->withParameter('requestUriHost', $uri->getHost())
);
$request = $this->actionRequestFactory->createActionRequest($httpRequest);
}

$thumbnailData = $this->assetService->getThumbnailUriAndSizeForAsset(
$this->asset,
$thumbnailConfiguration,
$this->request
$request
);

$this->srcCache = ($thumbnailData === null) ? '' : $thumbnailData['src'];
Expand All @@ -171,7 +206,6 @@ public function dataSrc(): string
$width = $this->getCurrentWidth();
$height = $this->getCurrentHeight();

$async = false;
$allowCropping = true;
$allowUpScaling = $this->supportsUpscaling();
$thumbnailConfiguration = new ThumbnailConfiguration(
Expand All @@ -181,7 +215,7 @@ public function dataSrc(): string
$height,
$allowCropping,
$allowUpScaling,
$async,
false,
$this->targetQuality,
$this->targetFormat
);
Expand All @@ -192,8 +226,9 @@ public function dataSrc(): string
if ($stream = $thumbnailImage->getResource()->getStream()) {
if (is_resource($stream)) {
if ($content = stream_get_contents($stream)) {
$mediaType = $thumbnailImage->getResource()->getMediaType();
if (is_string($content)) {
return 'data:image/png;base64,' . base64_encode($content);
return 'data:' . $mediaType . ';base64,' . base64_encode($content);
}
}
}
Expand Down

0 comments on commit 855ee9e

Please sign in to comment.