Reputation: 106
I've got the following crop value from the table sys_file_reference
.
{"default":{"cropArea":{"height":0.7157894736842105,"width":1,"x":0,"y":0},"selectedRatio":"1:1","focusArea":null}}
How can I use these values to create a cropped image (I'm using this in a scheduler task, if you are wondering)? I guess GraphicalFunctions->imageMagickConvert()
does what I'm looking for, but I cannot seem to figure out which parameter goes where.
Upvotes: 0
Views: 1208
Reputation: 789
It's best to use the "ImageService".
An example on how it's used can be seen in the Image Viewhelpers e.g. the uri.image ViewHelper: \TYPO3\CMS\Fluid\ViewHelpers\Uri\ImageViewHelper
A reduced example would look something like this:
/** @var \TYPO3\CMS\Core\Resource\FileReference $fileOrFileReference */
$fileOrFileReference = /* needs to be a File or FileReference */;
$cropString = '{...}';
// could also be from FileReference directly
//$cropString = $fileOrFileReference->getProperty('crop');
$cropVariantCollection = CropVariantCollection::create($cropString);
$cropArea = $cropVariantCollection->getCropArea('default'); // cropVariant
$processingInstructions = [
'crop' => $cropArea->isEmpty() ? null : $cropArea->makeAbsoluteBasedOnFile($fileOrFileReference)
];
$imageService = $this->objectManager->get(\TYPO3\CMS\Extbase\Service\ImageService::class);
$processedFile = $imageService->applyProcessingInstructions(
$fileOrFileReference,
$processingInstructions
);
$cropString
is your raw json encoding crop configuration and $fileOrFileReference is a File or FileReference (core, not extbase) object referencing the file you want to apply the cropping on.
Note: if you have a crop string from a sys_file_reference table, it might be even better to just fetch the FileReference object directly using the ResourceFactory:
$resourceFactory = $this->objectManager->get(\TYPO3\CMS\Core\Resource\ResourceFactory::class);
$fileReference = $resourceFactory->getFileReferenceObject($uid);
And then go through the steps above.
Edit: If you're not in a context where the objectManager is available as shown above (e.g. in an extbase controller), you can get the ObjectManager using GeneralUtility:
$objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
Upvotes: 3