1

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.

moe2k
  • 13
  • 4
  • TBH I don't see how this could be achieved unless you also integrate a machine learning process to automatically detect the relevant portion of images. Otherwise you can only get crop variants anchored at (0,0) ... – Mathias Brodala Nov 27 '20 at 09:14
  • It doesn't have to be so sophisticated; when I don't select a specific crop area in the backend wizard, TYPO3 also simply places it in the center of the image and not at (0,0), so that would be totally fine. – moe2k Nov 27 '20 at 09:20

1 Answers1

0
// 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
Maik
  • 106
  • 4