Skip to content

Commit

Permalink
Update Image Extension
Browse files Browse the repository at this point in the history
  • Loading branch information
mcrasser committed Feb 16, 2024
1 parent 3223d3b commit 646354a
Showing 1 changed file with 42 additions and 19 deletions.
61 changes: 42 additions & 19 deletions Classes/Twig/Extension/ImageExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@

use DMK\T3twig\Twig\EnvironmentTwig;
use Twig\TwigFunction;
use TYPO3\CMS\Core\Resource\File;
use TYPO3\CMS\Core\Resource\ProcessedFile;
use TYPO3\CMS\Extbase\Utility\DebuggerUtility;
use TYPO3Fluid\Fluid\Core\ViewHelper\TagBuilder;

/**
* Class ImageExtension.
Expand Down Expand Up @@ -74,34 +78,53 @@ public function getFunctions()
* @param mixed $image
* @param array $arguments
*
* @return array
* @return string
*/
public function renderImage(
EnvironmentTwig $env,
$image,
array $arguments = []
) {
// backward compatibility, create the ts_config key
if (!isset($arguments['ts_config'])) {
$arguments['ts_config'] = $arguments;
}
try {
// get Resource Object (non ExtBase version), taken from Fluid\MediaViewHelper
if (is_object($image) && is_callable([$image, 'getOriginalResource'])) {
// We have a domain model, so we need to fetch the FAL resource object from there
$image = $image->getOriginalResource();
} else {
$image = $env->getContentObject()->getImgResource(
$image,
$arguments
)['originalFile'];
}

// get Resource Object (non ExtBase version), taken from Fluid\MediaViewHelper
if (is_object($image) && is_callable([$image, 'getOriginalResource'])) {
// We have a domain model, so we need to fetch the FAL resource object from there
$image = $image->getOriginalResource();
$processingInstructions = [
'width' => $arguments['width'] ?? '',
'height' => $arguments['height'] ?? '',
'minWidth' => $arguments['minWidth'] ?? ($arguments['minW'] ?? ''),
'minHeight' => $arguments['minHeight'] ?? ($arguments['minH'] ?? ''),
'maxWidth' => $arguments['maxWidth'] ?? ($arguments['maxW'] ?? ''),
'maxHeight' => $arguments['maxHeight'] ?? ($arguments['maxH'] ?? ''),
];
/** @var File $image */
$processedImg = $image->process(ProcessedFile::CONTEXT_IMAGECROPSCALEMASK, $processingInstructions);
$tag = new TagBuilder('img');
$tag->addAttribute('src', $processedImg->getPublicUrl());
$tag->addAttribute('width', $processedImg->getProperty('width'));
$tag->addAttribute('height', $processedImg->getProperty('height'));

$tag->addAttribute('alt',
$arguments['alt'] ?? ($arguments['altText'] ?? ($image->hasProperty('alternative') ? $image->getProperty('alternative') : ''))
);

$title = $arguments['title'] ?? ($arguments['titleText'] ?? ($image->hasProperty('title') ? $image->getProperty('title') : false));
if ($title) {
$tag->addAttribute('title', $title);
}
} catch (\Exception $exception) {
throw $exception;
}

return $this->performCommand(
function (\Sys25\RnBase\Domain\Model\DataModel $arguments) use ($env, $image) {
return $env->getContentObject()->cImage(
$image,
$arguments->getTsConfig()
);
},
$env,
$arguments
);
return $tag->render();
}

/**
Expand Down

0 comments on commit 646354a

Please sign in to comment.