在自定义ViewHelper中调整图像大小

Ala*_*lan 2 typo3 fluid

我有一个处理一些图像的ViewHelper.我有一个原始文件的图像路径.我需要调整此图像的大小.

我可以在TYPO3中使用PHP代码来执行此操作吗?

我试过这个:

$imgPath = 'img/path/from_database.jpg
$imgConf = array();
$imgConf['file'] = $imgPath;
$imgConf['altText'] = "Sample alt text.";
$image = $this->cObj->IMAGE($imgConf);
Run Code Online (Sandbox Code Playgroud)

但是我得到了这个例外: "Call to a member function IMAGE() on null"

我的ViewHelper继承自: \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper

Dim*_* L. 7

首先,您应该说明您使用的TYPO3版本以及您的扩展程序是否使用Extbase或旧的pibased代码.

我猜这是Extbase,因为你继承了命名空间的viewhelper.我的建议是看一下原始的f:imageviewhelper代码(TYPO3\CMS\Fluid\ViewHelpers\ImageViewHelper)并复制你需要的东西.

首先将其添加到viewhelper的顶部:

/**
 * @var \TYPO3\CMS\Extbase\Service\ImageService
 * @inject
 */
protected $imageService;
Run Code Online (Sandbox Code Playgroud)

然后是你方法中的代码

$image = $this->imageService->getImage($imgPath);

// you have to set these variables or remove if you don't need them
$processingInstructions = array(
    'width' => $width,
    'height' => $height,
    'minWidth' => $minWidth,
    'minHeight' => $minHeight,
    'maxWidth' => $maxWidth,
    'maxHeight' => $maxHeight,
    'crop' => $crop,
);
$processedImage = $this->imageService->applyProcessingInstructions($image, $processingInstructions);
$imageUri = $this->imageService->getImageUri($processedImage);
Run Code Online (Sandbox Code Playgroud)

$imageUri 现在保持调整大小的图像的路径.

PS:您复制的示例来自自7.x分支以来不再存在的旧的pibased系统.