From 985e159c7c7f7eae0c19371dc07866fe199bdd04 Mon Sep 17 00:00:00 2001 From: Thorben Nissen Date: Thu, 5 Sep 2024 10:51:10 +0200 Subject: [PATCH 1/3] [BUGFIX] Read max width and height from page TSConfig * use correct route URL, that includes the current records PID * apply the maximum width and height from page TSConfig, when processing the image * return the correct width and height properties to apply in the image dialog --- Classes/Controller/SelectImageController.php | 34 ++++++++++++++----- .../RteConfigurationListener.php | 21 ------------ Configuration/Services.yaml | 8 +---- .../Public/JavaScript/Plugins/typo3image.js | 4 +-- 4 files changed, 28 insertions(+), 39 deletions(-) delete mode 100644 Classes/EventListener/RteConfigurationListener.php diff --git a/Classes/Controller/SelectImageController.php b/Classes/Controller/SelectImageController.php index 96786b20..2160f295 100644 --- a/Classes/Controller/SelectImageController.php +++ b/Classes/Controller/SelectImageController.php @@ -14,6 +14,7 @@ use Exception; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; +use TYPO3\CMS\Backend\Utility\BackendUtility; use TYPO3\CMS\Core\Configuration\Richtext; use TYPO3\CMS\Core\Http\JsonResponse; use TYPO3\CMS\Core\Resource\File; @@ -98,14 +99,15 @@ public function infoAction(ServerRequestInterface $request): ResponseInterface } $file = $this->getImage((int) $id); - $processedFile = $this->processImage($file, $params); + $maxDimensions = $this->getMaxDimensions($params); + $processedFile = $this->processImage($file, $params, $maxDimensions); return new JsonResponse([ 'uid' => $file->getUid(), 'alt' => $file->getProperty('alternative') ?? '', 'title' => $file->getProperty('title') ?? '', - 'width' => $file->getProperty('width'), - 'height' => $file->getProperty('height'), + 'width' => min($file->getProperty('width'), $maxDimensions['width']), + 'height' => min($file->getProperty('height'), $maxDimensions['height']), 'url' => $file->getPublicUrl(), 'processed' => [ 'width' => $processedFile->getProperty('width'), @@ -163,21 +165,21 @@ protected function getImage(int $id): File /** * Get the processed image. * - * @param File $file The original image file - * @param string[] $params The parameters used to process the image + * @param File $file The original image file + * @param string[] $params The parameters used to process the image + * @param array $maxDimensions The maximum width and height * * @return ProcessedFile */ - protected function processImage(File $file, array $params): ProcessedFile + protected function processImage(File $file, array $params, array $maxDimensions): ProcessedFile { - // TODO: Get this from page ts config $this->magicImageService->setMagicImageMaximumDimensions([ 'buttons.' => [ 'image.' => [ 'options.' => [ 'magic.' => [ - 'maxWidth' => 1920, - 'maxHeight' => 9999, + 'maxWidth' => $maxDimensions['width'], + 'maxHeight' => $maxDimensions['height'], ] ] ] @@ -193,4 +195,18 @@ protected function processImage(File $file, array $params): ProcessedFile ] ); } + + protected function getMaxDimensions(array $params): array + { + $tsConfig = BackendUtility::getPagesTSconfig($params['pid'] ?? 0); + $richtextConfigurationName = $params['richtextConfigurationName'] ?? 'default'; + if (empty($richtextConfigurationName)) { + $richtextConfigurationName = 'default'; + } + $rteConfig = $tsConfig['RTE.'][$richtextConfigurationName . '.']; + $maxHeight = $rteConfig['buttons.']['image.']['options.']['magic.']['maxHeight'] ?? 9999; + $maxWidth = $rteConfig['buttons.']['image.']['options.']['magic.']['maxWidth'] ?? 1920; + + return ['width' => $maxWidth, 'height' => $maxHeight]; + } } diff --git a/Classes/EventListener/RteConfigurationListener.php b/Classes/EventListener/RteConfigurationListener.php deleted file mode 100644 index 50a5e53a..00000000 --- a/Classes/EventListener/RteConfigurationListener.php +++ /dev/null @@ -1,21 +0,0 @@ -getConfiguration(); - $configuration['style']['typo3image'] = [ - 'routeUrl' => (string)$uriBuilder->buildUriFromRoute('rteckeditorimage_wizard_select_image'), - ]; - $event->setConfiguration($configuration); - } -} diff --git a/Configuration/Services.yaml b/Configuration/Services.yaml index 230199bb..4d9b7751 100644 --- a/Configuration/Services.yaml +++ b/Configuration/Services.yaml @@ -16,11 +16,5 @@ services: Netresearch\RteCKEditorImage\Database\RteImagesDbHook: public: true - Netresearch\RteCKEditorImage\EventListener\RteConfigurationListener: - tags: - - name: event.listener - identifier: 'rte_configuration_listener' - event: TYPO3\CMS\RteCKEditor\Form\Element\Event\AfterPrepareConfigurationForEditorEvent - Netresearch\RteCKEditorImage\Controller\SelectImageController: - tags: ['backend.controller'] \ No newline at end of file + tags: ['backend.controller'] diff --git a/Resources/Public/JavaScript/Plugins/typo3image.js b/Resources/Public/JavaScript/Plugins/typo3image.js index 018d899d..25a6a58e 100644 --- a/Resources/Public/JavaScript/Plugins/typo3image.js +++ b/Resources/Public/JavaScript/Plugins/typo3image.js @@ -334,7 +334,7 @@ function askImageAttributes(editor, img, attributes, table) { * @return {$.Deferred} */ function getImageInfo(editor, table, uid, params) { - let url = editor.config.get('style').typo3image.routeUrl + '&action=info&fileId=' + encodeURIComponent(uid) + '&table=' + encodeURIComponent(table) + '&contentsLanguage=en&editorId=123'; + let url = editor.config.get('typo3image').routeUrl + '&action=info&fileId=' + encodeURIComponent(uid) + '&table=' + encodeURIComponent(table) + '&contentsLanguage=en&editorId=123'; if (typeof params.width !== 'undefined' && params.width.length) { url += '&P[width]=' + params.width; @@ -357,7 +357,7 @@ function selectImage(editor) { ]; // TODO: Use ajaxUrl - const contentUrl = editor.config.get('style').typo3image.routeUrl + '&contentsLanguage=en&editorId=123&bparams=' + bparams.join('|'); + const contentUrl = editor.config.get('typo3image').routeUrl + '&contentsLanguage=en&editorId=123&bparams=' + bparams.join('|'); const modal = Modal.advanced({ type: Modal.types.iframe, From 94ea7ed57d8f1bb2bc1e3f543245960b65b1cb10 Mon Sep 17 00:00:00 2001 From: Thorben Nissen Date: Thu, 5 Sep 2024 11:00:09 +0200 Subject: [PATCH 2/3] [BUGFIX] Use more specific type annotations --- Classes/Controller/SelectImageController.php | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/Classes/Controller/SelectImageController.php b/Classes/Controller/SelectImageController.php index 2160f295..a958ce6b 100644 --- a/Classes/Controller/SelectImageController.php +++ b/Classes/Controller/SelectImageController.php @@ -165,9 +165,9 @@ protected function getImage(int $id): File /** * Get the processed image. * - * @param File $file The original image file - * @param string[] $params The parameters used to process the image - * @param array $maxDimensions The maximum width and height + * @param File $file The original image file + * @param string[] $params The parameters used to process the image + * @param array $maxDimensions The maximum width and height * * @return ProcessedFile */ @@ -196,11 +196,16 @@ protected function processImage(File $file, array $params, array $maxDimensions) ); } + /** + * @param string[] $params + * + * @return int[]|array + */ protected function getMaxDimensions(array $params): array { $tsConfig = BackendUtility::getPagesTSconfig($params['pid'] ?? 0); $richtextConfigurationName = $params['richtextConfigurationName'] ?? 'default'; - if (empty($richtextConfigurationName)) { + if ($richtextConfigurationName === '') { $richtextConfigurationName = 'default'; } $rteConfig = $tsConfig['RTE.'][$richtextConfigurationName . '.']; From cd536930b1fbb4e48d3375f4b2b3b1f6093f28c3 Mon Sep 17 00:00:00 2001 From: Thorben Nissen Date: Thu, 5 Sep 2024 11:01:48 +0200 Subject: [PATCH 3/3] [BUGFIX] Make sure given parameter is of type int --- Classes/Controller/SelectImageController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Classes/Controller/SelectImageController.php b/Classes/Controller/SelectImageController.php index a958ce6b..a8584d3b 100644 --- a/Classes/Controller/SelectImageController.php +++ b/Classes/Controller/SelectImageController.php @@ -203,7 +203,7 @@ protected function processImage(File $file, array $params, array $maxDimensions) */ protected function getMaxDimensions(array $params): array { - $tsConfig = BackendUtility::getPagesTSconfig($params['pid'] ?? 0); + $tsConfig = BackendUtility::getPagesTSconfig((int) ($params['pid'] ?? 0)); $richtextConfigurationName = $params['richtextConfigurationName'] ?? 'default'; if ($richtextConfigurationName === '') { $richtextConfigurationName = 'default';