Created
December 24, 2021 12:10
-
-
Save derhansen/2ce92d64eccc802daa8212b5a0136b09 to your computer and use it in GitHub Desktop.
TYPO3 CMS CropVariantUtility to manually calculate the default crop variant string for a given image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace Derhansen\Typo3Dev\Utility; | |
use TYPO3\CMS\Core\Imaging\ImageManipulation\Area; | |
use TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection; | |
use TYPO3\CMS\Core\Imaging\ImageManipulation\InvalidConfigurationException; | |
use TYPO3\CMS\Core\Resource\File; | |
class CropVariantUtility | |
{ | |
/** | |
* Default element configuration | |
* | |
* @var array | |
*/ | |
protected static $defaultConfig = [ | |
'file_field' => 'uid_local', | |
'allowedExtensions' => null, // default: $GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'] | |
'cropVariants' => [ | |
'default' => [ | |
'title' => 'LLL:EXT:core/Resources/Private/Language/locallang_wizards.xlf:imwizard.crop_variant.default', | |
'allowedAspectRatios' => [ | |
'16:9' => [ | |
'title' => 'LLL:EXT:core/Resources/Private/Language/locallang_wizards.xlf:imwizard.ratio.16_9', | |
'value' => 16 / 9 | |
], | |
'3:2' => [ | |
'title' => 'LLL:EXT:core/Resources/Private/Language/locallang_wizards.xlf:imwizard.ratio.3_2', | |
'value' => 3 / 2 | |
], | |
'4:3' => [ | |
'title' => 'LLL:EXT:core/Resources/Private/Language/locallang_wizards.xlf:imwizard.ratio.4_3', | |
'value' => 4 / 3 | |
], | |
'1:1' => [ | |
'title' => 'LLL:EXT:core/Resources/Private/Language/locallang_wizards.xlf:imwizard.ratio.1_1', | |
'value' => 1.0 | |
], | |
'NaN' => [ | |
'title' => 'LLL:EXT:core/Resources/Private/Language/locallang_wizards.xlf:imwizard.ratio.free', | |
'value' => 0.0 | |
], | |
], | |
'selectedRatio' => 'NaN', | |
'cropArea' => [ | |
'x' => 0.0, | |
'y' => 0.0, | |
'width' => 1.0, | |
'height' => 1.0, | |
], | |
], | |
] | |
]; | |
/** | |
* Returns a crop variant string to be used in sys_file_relation field "crop" for the given file and table/fieldname | |
* | |
* @param File $file | |
* @param string $tableName | |
* @param string $fieldname | |
* @return string | |
* @throws InvalidConfigurationException | |
*/ | |
public static function getCropVariantString(File $file, string $tableName, string $fieldname): string | |
{ | |
$config = $GLOBALS['TCA'][$tableName]['columns'][$fieldname]['config']['overrideChildTca']['columns']['crop']['config']; | |
$cropVariants = self::populateConfiguration($config); | |
$cropVariantCollection = CropVariantCollection::create('', $cropVariants['cropVariants']); | |
if (!empty($file->getProperty('width'))) { | |
$cropVariantCollection = $cropVariantCollection->applyRatioRestrictionToSelectedCropArea($file); | |
} | |
return (string)$cropVariantCollection; | |
} | |
/** | |
* @param array $baseConfiguration | |
* @return array | |
* @throws InvalidConfigurationException | |
*/ | |
protected static function populateConfiguration(array $baseConfiguration): array | |
{ | |
$defaultConfig = self::$defaultConfig; | |
// If ratios are set do not add default options | |
if (isset($baseConfiguration['cropVariants'])) { | |
unset($defaultConfig['cropVariants']); | |
} | |
$config = array_replace_recursive($defaultConfig, $baseConfiguration); | |
if (!is_array($config['cropVariants'])) { | |
throw new InvalidConfigurationException('Crop variants configuration must be an array', 1485377267); | |
} | |
$cropVariants = []; | |
foreach ($config['cropVariants'] as $id => $cropVariant) { | |
// Filter allowed aspect ratios | |
$cropVariant['allowedAspectRatios'] = array_filter($cropVariant['allowedAspectRatios'] ?? [], static function ($aspectRatio) { | |
return !(bool)($aspectRatio['disabled'] ?? false); | |
}); | |
// Ignore disabled crop variants | |
if (!empty($cropVariant['disabled'])) { | |
continue; | |
} | |
if (empty($cropVariant['allowedAspectRatios'])) { | |
throw new InvalidConfigurationException('Crop variants configuration ' . $id . ' contains no allowed aspect ratios', 1620147893); | |
} | |
// Enforce a crop area (default is full image) | |
if (empty($cropVariant['cropArea'])) { | |
$cropVariant['cropArea'] = Area::createEmpty()->asArray(); | |
} | |
$cropVariants[$id] = $cropVariant; | |
} | |
$config['cropVariants'] = $cropVariants; | |
// By default we allow all image extensions that can be handled by the GFX functionality | |
if ($config['allowedExtensions'] === null) { | |
$config['allowedExtensions'] = $GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext']; | |
} | |
return $config; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment