Skip to content

Instantly share code, notes, and snippets.

@fchaussin
Created March 30, 2021 14:23
Show Gist options
  • Save fchaussin/30bc8c53cb19a1c2ca0480f369318c05 to your computer and use it in GitHub Desktop.
Save fchaussin/30bc8c53cb19a1c2ca0480f369318c05 to your computer and use it in GitHub Desktop.
TYPO3 ImageManipulation TCA Helper
<?php
namespace Vendor\MyExt\UserFunc;
/**
* Class TcaImageManipulation
* Image manipulation helper for TCA config
* @package Smile\MqbBase\UserFunc
*/
class TcaImageManipulation
{
private const ARRAY_ASPECTRATIOS = [
'Nan' => [
'title' => 'LLL:EXT:core/Resources/Private/Language/locallang_wizards.xlf:imwizard.ratio.free',
'value' => 0.0,
],
'vertical' => [
'title' => 'Vertical',
'value' => 640 / 889,
],
'horizontal' => [
'title' => 'Horizontal',
'value' => 450 / 385,
],
'square' => [
'title' => 'Square',
'value' => 1.0,
],
'wide' => [
'title' => 'Panoramic',
'value' => 1440 / 615,
],
'carrousel' => [
'title' => 'Carrousel',
'value' => 930 / 520,
],
];
private const ARRAY_CROPAREA = [
'x' => 0.0,
'y' => 0.0,
'width' => 1.0,
'height' => 1.0,
];
private const ARRAY_COVERAREAS = [
'x' => 0.0,
'y' => 0.0,
'width' => 1.0,
'height' => 1.0,
];
/**
* The return must be array_merge_recursive with the conf to extend
* @param string $returnKey
* @param string[] $aspectRatios
* @return array
*/
public static function getConfig($returnKey = 'config', $aspectRatios = null): array
{
if (is_null($aspectRatios) || empty($aspectRatios)) {
$aspectRatios = array_keys(self::ARRAY_ASPECTRATIOS);
}
if (is_string($aspectRatios)) {
$aspectRatios = [$aspectRatios];
}
// build exhaustive config
$output = self::getBaseConfig();
$output['config']['overrideChildTca']['columns'] = self::getCropConfig();
$output['config']['overrideChildTca']['columns']['crop']['config']['cropVariants']['default']['allowedAspectRatios'] = self::getAllowedAspectRatios($aspectRatios);
$output['config']['overrideChildTca']['columns']['crop']['config']['cropVariants']['default']['cropArea'] = self::ARRAY_CROPAREA;
// $output['config']['overrideChildTca']['columns']['crop']['config']['cropVariants']['mqb']['coverAreas'] = self::ARRAY_COVERAREAS;
switch($returnKey){
case 'crop':
return $output['config']['overrideChildTca']['columns']['crop'];
break;
case 'overrideChildTca':
return $output['config']['overrideChildTca'];
break;
case 'config':
default:
return $output['config'];
}
}
/**
* @return array
*/
private static function getBaseConfig(): array
{
return [
'config' => [
'overrideChildTca' => [
'columns' => [],
'types' => [
\TYPO3\CMS\Core\Resource\File::FILETYPE_IMAGE => [
'showitem' => '
--palette--;LLL:EXT:lang/Resources/Private/Language/locallang_tca.xlf:sys_file_reference.imageoverlayPalette;imageoverlayPalette,
--palette--;;filePalette',
],
],
],
],
];
}
/**
* @return array
*/
private static function getCropConfig(): array
{
return [
'crop' => [
'label' => 'LLL:EXT:core/Resources/Private/Language/locallang_tca.xlf:sys_file_reference.crop',
'config' => [
'type' => 'imageManipulation',
'cropVariants' => [
'default' => [
'title' => 'LLL:EXT:core/Resources/Private/Language/locallang_wizards.xlf:imwizard.crop_variant.default',
'allowedAspectRatios' => [],
'selectedRatio' => 'NaN',
'cropArea' => [],
'coverAreas' => [],
],
],
],
],
];
}
/**
* @param string[] $aspectRatios
* @return array
*/
private static function getAllowedAspectRatios($aspectRatios): array
{
$output = [];
foreach ($aspectRatios as $aspectRatio) {
$output[$aspectRatio] = self::ARRAY_ASPECTRATIOS[$aspectRatio];
}
return $output;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment