使用来自 `sys_file_reference` 的裁剪值创建裁剪图像
Create cropped image with crop values from `sys_file_reference`
我从 table sys_file_reference
.
中获得了以下裁剪值
{"default":{"cropArea":{"height":0.7157894736842105,"width":1,"x":0,"y":0},"selectedRatio":"1:1","focusArea":null}}
如何使用这些值来创建裁剪后的图像(如果您想知道,我正在调度程序任务中使用它)?我想 GraphicalFunctions->imageMagickConvert()
做了我正在寻找的东西,但我似乎无法弄清楚哪个参数去哪里。
最好使用"ImageService"。
有关如何使用它的示例可以在 Image Viewhelpers 中看到,例如uri.image ViewHelper: \TYPO3\CMS\Fluid\ViewHelpers\Uri\ImageViewHelper
一个简化的例子看起来像这样:
/** @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
是您的原始 json 编码裁剪配置,$fileOrFileReference 是引用您要应用裁剪的文件的文件或 FileReference(核心,而非 extbase)对象。
注意:如果您有来自 sys_file_reference table 的裁剪字符串,直接使用 ResourceFactory 获取 FileReference 对象可能会更好:
$resourceFactory = $this->objectManager->get(\TYPO3\CMS\Core\Resource\ResourceFactory::class);
$fileReference = $resourceFactory->getFileReferenceObject($uid);
然后再进行上述步骤。
编辑:
如果您不在 objectManager 可用的上下文中(例如在 extbase 控制器中),您可以使用 GeneralUtility:
获取 ObjectManager
$objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
我从 table sys_file_reference
.
{"default":{"cropArea":{"height":0.7157894736842105,"width":1,"x":0,"y":0},"selectedRatio":"1:1","focusArea":null}}
如何使用这些值来创建裁剪后的图像(如果您想知道,我正在调度程序任务中使用它)?我想 GraphicalFunctions->imageMagickConvert()
做了我正在寻找的东西,但我似乎无法弄清楚哪个参数去哪里。
最好使用"ImageService"。
有关如何使用它的示例可以在 Image Viewhelpers 中看到,例如uri.image ViewHelper: \TYPO3\CMS\Fluid\ViewHelpers\Uri\ImageViewHelper
一个简化的例子看起来像这样:
/** @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
是您的原始 json 编码裁剪配置,$fileOrFileReference 是引用您要应用裁剪的文件的文件或 FileReference(核心,而非 extbase)对象。
注意:如果您有来自 sys_file_reference table 的裁剪字符串,直接使用 ResourceFactory 获取 FileReference 对象可能会更好:
$resourceFactory = $this->objectManager->get(\TYPO3\CMS\Core\Resource\ResourceFactory::class);
$fileReference = $resourceFactory->getFileReferenceObject($uid);
然后再进行上述步骤。
编辑: 如果您不在 objectManager 可用的上下文中(例如在 extbase 控制器中),您可以使用 GeneralUtility:
获取 ObjectManager$objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);