Is there a way to get the actual image size (width/height) of a cropped image ("Image manipulation" > [Open Editor]) in a fluid template?
All I can come up with is {image.properties}
and width
and height
. But these are the dimensions of the original resource, not the cropped one. And there is of course an attribute crop
which holds all the crop info:
{"default":{"cropArea":{"height":0.6431784107946027,"width":0.608,"x":0.037,"y":0.15592203898050974},"selectedRatio":"NaN","focusArea":null}}
But do I really have to find a way to parse this in fluid (if that's even possible)? Isn't there an official way to calculate the actual dimension of a generated image?
My use case is an image gallery component for which I need to explicitly state the image dimensions.
I have to answer my own question 7 years later :-) sorry for that.
Here is a viewhelper (typo3 v12) which basicly does what I wanted to achieve:
<?php
namespace Vendor\Extensionname\ViewHelpers;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Utility\DebuggerUtility;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
use TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperInterface;
/**
* ViewHelper to calculate croped image dimensions
*
* inline example: {yourvendorshortcut:CroppedImageHeight(image: image, width:'1000', variantname: 'desktop')}
*/
class CroppedImageHeightViewHelper extends AbstractViewHelper implements ViewHelperInterface
{
use CompileWithRenderStatic;
public function initializeArguments()
{
$this->registerArgument('image', 'mixed', 'Image', true);
$this->registerArgument('width', 'int', 'Image width in fluid template', true);
$this->registerArgument('variantname', 'string', 'Variant name', true);
}
/**
* @param array $arguments
* @param \Closure $renderChildrenClosure
* @param RenderingContextInterface $renderingContext
*/
public static function renderStatic(
array $arguments,
\Closure $renderChildrenClosure,
RenderingContextInterface $renderingContext
) {
$width = (int) $arguments['width'];
if ($width === 0) {return '';}
$image = $arguments['image'];
$imageProperties = $image->getProperties();
$variantname = $arguments['variantname'];
$cropinfo = json_decode($imageProperties['crop'], true);
if (isset($cropinfo[$variantname])) {
$originalCropedWidth = $imageProperties['width'] * $cropinfo[$variantname]['cropArea']['width'];
$originalCropedHeight = $imageProperties['height'] * $cropinfo[$variantname]['cropArea']['height'];
$aspectRatio = $originalCropedHeight / $originalCropedWidth;
$height = floor($width * $aspectRatio);
return $height;
}
return '';
}
}