Generate TYPO3 cropVariants for images from CLI

240 views Asked by At

Is there a way to trigger the generation or update of cropVariants for existing image relations?

My use case: I want to make sure that images always obey a certain aspect ratio. Thus, I have defined a cropVariant called "myCropVariant" for images, for example in pages and some custom tables. This basically works fine, I see a new wizard in the "create image relation" dialog where I can select the cropArea, and even if I don't click on that wizard, the cropVariant image along with the cropVariant database record is created as soon as I save the parent record, using the center of the image as the crop area. Frontend output also works fine using

<f:image cropVariant="myCropVariant"/>

The problem is: I imported a lot of content from another website into the database (using a custom migration CLI command). That also includes images; I've created the corresponding sys_file records and references. The images are rendered fine in frontend, but the cropVariant is not being used. That's because it doesn't exist in database because nobody has clicked through the imported content in backend. Now creating them through the migration script from outside TYPO3 would in theory be possible, but would be pretty nasty (because one needs to know the image dimensions and so on), so I look for a best practice for this through TYPO3 by CLI or something similar.

1

There are 1 answers

0
Maik On
// get crop variant conf directly from TCA
$cropVariants = $GLOBALS['TCA']['tx_myext_domain_model_example']['columns']['attachments']['config']['overrideChildTca']['types'][2]['columnsOverrides']['crop']['config']['cropVariants'];

// extend for default cropArea configuration
foreach ($cropVariants as &$variant) {
    $variant['cropArea'] = ['x' => 0.0, 'y' => 0.0, 'width' => 1.0, 'height' => 1.0];
}

// init variants
$cropVariantCollection = CropVariantCollection::create('', $cropVariants);

// apply configuration to TYPO3\CMS\Core\Resource\File
$processedCollection = $cropVariantCollection->applyRatioRestrictionToSelectedCropArea($file);

// get json string from variants ({"desktop":{"cropArea":{"x":0,"y":0.125,"width":1,"height":0.75},"selectedRatio":"16:9","focusArea":null}})
$cropValue = (string)$processedCollection;

// save e.g. via dataHandler to sys_file_reference