Skip to content

Commit

Permalink
Remove MagicImageService
Browse files Browse the repository at this point in the history
  • Loading branch information
marekskopal committed Nov 11, 2024
1 parent 2f9dd38 commit 13e0068
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 205 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[*.php]
indent_size = 4
indent_style = space
46 changes: 13 additions & 33 deletions Classes/Controller/ImageLinkRenderingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@

namespace Netresearch\RteCKEditorImage\Controller;

use Netresearch\RteCKEditorImage\Service\MagicImageService;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Log\LogLevel as PsrLogLevel;
use TYPO3\CMS\Core\Log\Logger;
use TYPO3\CMS\Core\Log\LogManager;
use TYPO3\CMS\Core\Resource\Exception\FileDoesNotExistException;
use TYPO3\CMS\Core\Resource\File;
use TYPO3\CMS\Core\Resource\ProcessedFile;
use TYPO3\CMS\Core\Resource\ResourceFactory;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
Expand Down Expand Up @@ -54,7 +55,7 @@ class ImageLinkRenderingController
*/
public $extKey = 'rte_ckeditor_image';

protected ContentObjectRenderer $cObj;
protected ?ContentObjectRenderer $cObj = null;

public function setContentObjectRenderer(
ContentObjectRenderer $cObj,
Expand All @@ -70,7 +71,7 @@ public function setContentObjectRenderer(
*
* @return string HTML output
*/
public function renderImages(?string $content, array $conf = []): string
public function renderImages(?string $content, array $conf, ServerRequestInterface $request): string
{
// Get link inner HTML
$linkContent = $this->cObj !== null ? $this->cObj->getCurrentVal() : null;
Expand Down Expand Up @@ -109,8 +110,10 @@ public function renderImages(?string $content, array $conf = []): string
'height' => (int) ($imageAttributes['height'] ?? $systemImage->getProperty('height') ?? 0),
];

$processedFile = $this->getMagicImageService()
->createMagicImage($systemImage, $imageConfiguration);
$processedFile = $systemImage->process(
ProcessedFile::CONTEXT_IMAGECROPSCALEMASK,
$imageConfiguration,
);

$additionalAttributes = [
'src' => $processedFile->getPublicUrl(),
Expand All @@ -120,7 +123,7 @@ public function renderImages(?string $content, array $conf = []): string
'height' => $processedFile->getProperty('height') ?? $imageConfiguration['height'],
];

$lazyLoading = $this->getLazyLoadingConfiguration();
$lazyLoading = $this->getLazyLoadingConfiguration($request);

if ($lazyLoading !== null) {
$additionalAttributes['loading'] = $lazyLoading;
Expand Down Expand Up @@ -192,34 +195,11 @@ protected function getImageAttributes(string $passedImage): array
*
* @return null|string
*/
private function getLazyLoadingConfiguration(): ?string
{
return $GLOBALS['TSFE']->tmpl->setup['lib.']['contentElement.']['settings.']['media.']['lazyLoading'] ?? null;
}

/**
* Instantiates and prepares the Magic Image service.
*
* @return MagicImageService
*/
protected function getMagicImageService(): MagicImageService
private function getLazyLoadingConfiguration(ServerRequestInterface $request): ?string
{
static $magicImageService;

if ($magicImageService === null) {
$magicImageService = GeneralUtility::makeInstance(MagicImageService::class);

// Get RTE configuration

/** @var array<string, mixed[]> $pageTSConfig */
//$pageTSConfig = $GLOBALS['TSFE']->getPagesTSconfig();

//if (is_array($pageTSConfig['RTE.']['default.'])) {
// $magicImageService->setMagicImageMaximumDimensions($pageTSConfig['RTE.']['default.']);
//}
}
$setupArray = $request->getAttribute('frontend.typoscript')->getSetupArray();

return $magicImageService;
return $setupArray['lib.']['contentElement.']['settings.']['media.']['lazyLoading'] ?? null;
}

/**
Expand All @@ -234,7 +214,7 @@ protected function getLogger(): Logger
/**
* Returns attributes value or even empty string when override mode is enabled
*
* @param string $attributeName
* @param non-empty-string $attributeName
* @param array<string, string> $attributes
* @param File $image
*
Expand Down
49 changes: 12 additions & 37 deletions Classes/Controller/ImageRenderingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

namespace Netresearch\RteCKEditorImage\Controller;

use Netresearch\RteCKEditorImage\Service\MagicImageService;
use Netresearch\RteCKEditorImage\Utils\ProcessedFilesHandler;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Log\LogLevel as PsrLogLevel;
use TYPO3\CMS\Core\Log\Logger;
use TYPO3\CMS\Core\Log\LogManager;
Expand All @@ -22,9 +22,6 @@
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;

use function get_class;
use function is_array;

/**
* Controller to render the image tag in frontend.
*
Expand Down Expand Up @@ -55,7 +52,7 @@ class ImageRenderingController
*/
public $extKey = 'rte_ckeditor_image';

protected ContentObjectRenderer $cObj;
protected ?ContentObjectRenderer $cObj = null;

public function setContentObjectRenderer(
ContentObjectRenderer $cObj,
Expand All @@ -68,10 +65,10 @@ public function setContentObjectRenderer(
*
* @param null|string $content Content input (not used)
* @param mixed[] $conf TypoScript configuration
*
* @param ServerRequestInterface $request
* @return string HTML output
*/
public function renderImageAttributes(?string $content, array $conf = []): string
public function renderImageAttributes(?string $content, array $conf, ServerRequestInterface $request): string
{
$imageAttributes = $this->getImageAttributes();
$imageSource = $imageAttributes['src'] ?? '';
Expand Down Expand Up @@ -110,7 +107,7 @@ public function renderImageAttributes(?string $content, array $conf = []): strin
'height' => $processedFile->getProperty('height') ?? $imageConfiguration['height'],
];

$lazyLoading = $this->getLazyLoadingConfiguration();
$lazyLoading = $this->getLazyLoadingConfiguration($request);

if ($lazyLoading !== null) {
$additionalAttributes['loading'] = $lazyLoading;
Expand Down Expand Up @@ -169,7 +166,8 @@ public function renderImageAttributes(?string $content, array $conf = []): strin
|| isset($imageAttributes['data-htmlarea-clickenlarge']))
&& isset($systemImage)
) {
$config = $GLOBALS['TSFE']->tmpl->setup['lib.']['contentElement.']['settings.']['media.']['popup.'] ?? [];
$setupArray = $request->getAttribute('frontend.typoscript')->getSetupArray();
$config = $setupArray['lib.']['contentElement.']['settings.']['media.']['popup.'] ?? [];
$config['enable'] = 1;

$systemImage->updateProperties([
Expand All @@ -196,9 +194,11 @@ public function renderImageAttributes(?string $content, array $conf = []): strin
*
* @return null|string
*/
private function getLazyLoadingConfiguration(): ?string
private function getLazyLoadingConfiguration(ServerRequestInterface $request): ?string
{
return $GLOBALS['TSFE']->tmpl->setup['lib.']['contentElement.']['settings.']['media.']['lazyLoading'] ?? null;
$setupArray = $request->getAttribute('frontend.typoscript')->getSetupArray();

return $setupArray['lib.']['contentElement.']['settings.']['media.']['lazyLoading'] ?? null;
}

/**
Expand All @@ -211,31 +211,6 @@ protected function getImageAttributes(): array
return $this->cObj->parameters ?? [];
}

/**
* Instantiates and prepares the Magic Image service.
*
* @return MagicImageService
*/
protected function getMagicImageService(): MagicImageService
{
static $magicImageService;

if ($magicImageService === null) {
$magicImageService = GeneralUtility::makeInstance(MagicImageService::class);

// Get RTE configuration

/** @var array<string, mixed[]> $pageTSConfig */
//$pageTSConfig = $GLOBALS['TSFE']->getPagesTSconfig();

//if (is_array($pageTSConfig['RTE.']['default.'])) {
// $magicImageService->setMagicImageMaximumDimensions($pageTSConfig['RTE.']['default.']);
//}
}

return $magicImageService;
}

/**
* Tells whether the image URL is found to be "external".
*
Expand Down Expand Up @@ -268,7 +243,7 @@ protected function getLogger(): Logger
/**
* Returns attributes value or even empty string when override mode is enabled
*
* @param string $attributeName
* @param non-empty-string $attributeName
* @param array<string, string> $attributes
* @param File $image
*
Expand Down
46 changes: 11 additions & 35 deletions Classes/Controller/SelectImageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Netresearch\RteCKEditorImage\Controller;

use Exception;
use Netresearch\RteCKEditorImage\Service\MagicImageService;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Http\JsonResponse;
Expand All @@ -33,21 +32,11 @@
*/
class SelectImageController extends ElementBrowserController
{
/**
* @var bool
*/
protected bool $isInfoAction = false;

/**
* @var ResourceFactory
*/
private ResourceFactory $resourceFactory;

/**
* @var MagicImageService
*/
private MagicImageService $magicImageService;

/**
* Forward to infoAction if wanted
*
Expand All @@ -58,10 +47,9 @@ class SelectImageController extends ElementBrowserController
public function mainAction(ServerRequestInterface $request): ResponseInterface
{
$this->resourceFactory = GeneralUtility::makeInstance(ResourceFactory::class);
$this->magicImageService = GeneralUtility::makeInstance(MagicImageService::class);

$queryParams = $request->getQueryParams();
$isInfoAction = $queryParams['action'] ?? null === 'info';
$isInfoAction = ($queryParams['action'] ?? null) === 'info';

if (!$isInfoAction) {
$bparams = explode('|', (string) $queryParams['bparams']);
Expand Down Expand Up @@ -169,27 +157,15 @@ protected function getImage(int $id): File
*/
protected function processImage(File $file, array $params): ProcessedFile
{
// TODO: Get this from page ts config
$this->magicImageService->setMagicImageMaximumDimensions([
'buttons.' => [
'image.' => [
'options.' => [
'magic.' => [
'maxWidth' => 1920,
'maxHeight' => 9999,
]
]
]
]
]);

return $this->magicImageService
->createMagicImage(
$file,
[
'width' => (int) ($params['width'] ?? $file->getProperty('width')),
'height' => (int) ($params['height'] ?? $file->getProperty('height')),
]
);
$width = min(1920, (int) ($params['width'] ?? $file->getProperty('width')));
$height = min(9999, (int) ($params['height'] ?? $file->getProperty('height')));

return $file->process(
ProcessedFile::CONTEXT_IMAGECROPSCALEMASK,
[
'width' => $width,
'height' => $height,
],
);
}
}
17 changes: 9 additions & 8 deletions Classes/Database/RteImagesDbHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use TYPO3\CMS\Core\Resource\Exception\ResourceDoesNotExistException;
use TYPO3\CMS\Core\Resource\File;
use TYPO3\CMS\Core\Resource\FileInterface;
use TYPO3\CMS\Core\Resource\ProcessedFile;
use TYPO3\CMS\Core\Resource\ResourceFactory;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\ArrayUtility;
Expand Down Expand Up @@ -285,10 +286,6 @@ private function modifyRteField(string $value): string
}

$resourceFactory = GeneralUtility::makeInstance(ResourceFactory::class);
$magicImageService = GeneralUtility::makeInstance(MagicImageService::class);

$pageTsConfig = BackendUtility::getPagesTSconfig(0);
$magicImageService->setMagicImageMaximumDimensions($pageTsConfig['RTE.']['default.'] ?? []);

foreach ($imgSplit as $key => $v) {
// Odd numbers contains the <img> tags
Expand Down Expand Up @@ -361,8 +358,10 @@ private function modifyRteField(string $value): string
GeneralUtility::makeInstance(Context::class)
->setAspect('fileProcessing', new FileProcessingAspect(false));

$magicImage = $magicImageService
->createMagicImage($originalImageFile, $imageConfiguration);
$magicImage = $originalImageFile->process(
ProcessedFile::CONTEXT_IMAGECROPSCALEMASK,
$imageConfiguration,
);

$attribArray['width'] = $magicImage->getProperty('width');
$attribArray['height'] = $magicImage->getProperty('height');
Expand Down Expand Up @@ -413,8 +412,10 @@ private function modifyRteField(string $value): string
'height' => $attribArray['height']
];

$magicImage = $magicImageService
->createMagicImage($fileObject, $imageConfiguration);
$magicImage = $fileObject->process(
ProcessedFile::CONTEXT_IMAGECROPSCALEMASK,
$imageConfiguration,
);

$attribArray['width'] = $magicImage->getProperty('width');
$attribArray['height'] = $magicImage->getProperty('height');
Expand Down
Loading

0 comments on commit 13e0068

Please sign in to comment.