Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX] Read max width and height from page TSConfig #299

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
39 changes: 30 additions & 9 deletions Classes/Controller/SelectImageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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'),
Expand Down Expand Up @@ -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<string, int> $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'],
]
]
]
Expand All @@ -193,4 +195,23 @@ protected function processImage(File $file, array $params): ProcessedFile
]
);
}

/**
* @param string[] $params
*
* @return int[]|array<string, int>
*/
protected function getMaxDimensions(array $params): array
{
$tsConfig = BackendUtility::getPagesTSconfig((int) ($params['pid'] ?? 0));
$richtextConfigurationName = $params['richtextConfigurationName'] ?? 'default';
if ($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];
}
}
21 changes: 0 additions & 21 deletions Classes/EventListener/RteConfigurationListener.php

This file was deleted.

8 changes: 1 addition & 7 deletions Configuration/Services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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']
tags: ['backend.controller']
4 changes: 2 additions & 2 deletions Resources/Public/JavaScript/Plugins/typo3image.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down