Skip to content

Instantly share code, notes, and snippets.

@wazum
Created March 4, 2020 15:03
Show Gist options
  • Save wazum/1201a47b0301cafa662ddfcdaf1b27a6 to your computer and use it in GitHub Desktop.
Save wazum/1201a47b0301cafa662ddfcdaf1b27a6 to your computer and use it in GitHub Desktop.
Create an image with a specific cropping variant in a TYPO3 user function or similar
<?php
$contentObjectRenderer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::class);
$filesContent = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Frontend\ContentObject\FilesContentObject::class,
$contentObjectRenderer);
$filesConfiguration = [
'references' => '50', // this is a UID from sys_file_reference
'renderObj' => 'IMAGE',
'renderObj.' => [
'file' => '50', // this is the same UID as above
'file.' => [
'treatIdAsReference' => true,
'width' => '265c',
'height' => '143c',
'crop.' => [
'data' => 'file:current:crop'
],
'cropVariant' => 'xs' // the name of the crop variant
]
]
];
$imageTag = $filesContent->render($filesConfiguration);
echo $imageTag;
@cepheiVV
Copy link

cepheiVV commented Feb 11, 2024

Works fine up to TYPO3 v11.
Breaks with v12. The problem is that I don't have a valid content object in my request, since I'm running the process as a user func.
If this is in extbase context where you initialize the process through a plugin, it probably also works fine in v12.

In my case, this solution works in v12 without initializing a frontend request:

$contentObjectRenderer = GeneralUtility::makeInstance(ContentObjectRenderer::class);
$imgResourceConf = [
    'file' => $publicUrl,   // file path or a file UID
    'file.' => [
        'treatIdAsReference' => true,
        'width' => $width,
        'height' => $height,
    ]
];
$url = $contentObjectRenderer->cObjGetSingle('IMG_RESOURCE', $imgResourceConf);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment