diff --git a/administrator/language/en-GB/plg_media-action_resize.ini b/administrator/language/en-GB/plg_media-action_resize.ini index 67a926e1b8e76..caad5ba975402 100644 --- a/administrator/language/en-GB/plg_media-action_resize.ini +++ b/administrator/language/en-GB/plg_media-action_resize.ini @@ -8,6 +8,9 @@ PLG_MEDIA-ACTION_RESIZE_AUTOMATIC_DESC="Settings for automatic processing when i PLG_MEDIA-ACTION_RESIZE_AUTOMATIC_LABEL="Automatic Processing" ; The following string is deprecated and will be removed with 8.0 PLG_MEDIA-ACTION_RESIZE_BATCH_DESC="Settings for server side actions when images are created." +PLG_MEDIA-ACTION_RESIZE_BATCH_FOLDERS_DESC="Maximum dimensions for images uploaded to a folder and its subfolders. The most specific folder takes precedence over the default values above. Leave the width and height empty to disable resizing for a folder." +PLG_MEDIA-ACTION_RESIZE_BATCH_FOLDERS_LABEL="Folder Specific Sizes" +PLG_MEDIA-ACTION_RESIZE_BATCH_FOLDER_LABEL="Folder" ; The following string is deprecated and will be removed with 8.0 PLG_MEDIA-ACTION_RESIZE_BATCH_LABEL="Batch Settings" PLG_MEDIA-ACTION_RESIZE_BATCH_MAX_HEIGHT_LABEL="Image Max Height" diff --git a/plugins/media-action/resize/resize.xml b/plugins/media-action/resize/resize.xml index bad0de64f3ab8..000633c6df2af 100644 --- a/plugins/media-action/resize/resize.xml +++ b/plugins/media-action/resize/resize.xml @@ -41,6 +41,39 @@ addonAfter="px" filter="integer" /> + +
+ + + + + diff --git a/plugins/media-action/resize/src/Extension/Resize.php b/plugins/media-action/resize/src/Extension/Resize.php index de5e763e6ebc0..453108340ff80 100644 --- a/plugins/media-action/resize/src/Extension/Resize.php +++ b/plugins/media-action/resize/src/Extension/Resize.php @@ -58,7 +58,9 @@ public function onContentBeforeSave(BeforeSaveEvent $event): void return; } - if (!$this->params->get('batch_width') && !$this->params->get('batch_height')) { + [$maxWidth, $maxHeight] = $this->getMaxSize((string) $item->adapter, (string) $item->path); + + if (!$maxWidth && !$maxHeight) { return; } @@ -72,8 +74,6 @@ public function onContentBeforeSave(BeforeSaveEvent $event): void $imgObject = new Image(imagecreatefromstring($item->data)); - $maxWidth = (int) $this->params->get('batch_width', 0); - $maxHeight = (int) $this->params->get('batch_height', 0); if ( !(($maxWidth && $imgObject->getWidth() > $maxWidth) || ($maxHeight && $imgObject->getHeight() > $maxHeight)) @@ -109,4 +109,48 @@ public function onContentBeforeSave(BeforeSaveEvent $event): void $imgObject->toFile(null, $type); $item->data = ob_get_clean(); } + + /** + * Returns the maximum width and height for an upload into the given folder. + * Uses default resizing if folder not matches and sizes given + * + * @param string $adapter The media adapter name, e.g. "local-images" + * @param string $path The folder path inside the adapter + * + * @return int[] The maximum width and height, 0 meaning no resizing + * + * @since __DEPLOY_VERSION__ + */ + private function getMaxSize(string $adapter, string $path): array + { + $uploadPath = trim($path, '/'); + $matchedRule = null; + $matchedPath = null; + + foreach ((array) $this->params->get('batch_folders', []) as $folderRule) { + $folderRule = (array) $folderRule; + $folder = explode(':', (string) ($folderRule['folder'] ?? ''), 2); + + if (\count($folder) !== 2 || $folder[0] !== $adapter) { + continue; + } + + $folderPath = trim($folder[1], '/'); + + if ($folderPath !== '' && $uploadPath !== $folderPath && !str_starts_with($uploadPath, $folderPath . '/')) { + continue; + } + + if ($matchedPath === null || \strlen($folderPath) > \strlen($matchedPath)) { + $matchedRule = $folderRule; + $matchedPath = $folderPath; + } + } + + if ($matchedRule !== null) { + return [(int) ($matchedRule['width'] ?? 0), (int) ($matchedRule['height'] ?? 0)]; + } + + return [(int) $this->params->get('batch_width', 0), (int) $this->params->get('batch_height', 0)]; + } }