在自定义 ViewHelper 中调整图像大小
Resize image in custom ViewHelper
我有一个处理一些图像的 ViewHelper。我有一个原始文件的图像路径。我需要调整这张图片的大小。
我可以在 TYPO3 中使用 PHP 代码来执行此操作吗?
我试过这个:
$imgPath = 'img/path/from_database.jpg
$imgConf = array();
$imgConf['file'] = $imgPath;
$imgConf['altText'] = "Sample alt text.";
$image = $this->cObj->IMAGE($imgConf);
但我遇到了这个异常:"Call to a member function IMAGE() on null"
我的 ViewHelper 继承自:\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper
首先你应该说明你使用的是什么 TYPO3 版本,以及你的扩展是使用 Extbase 还是旧的基于 pi 的代码。
我猜它是 Extbase,因为您是从命名空间的 viewhelper 继承的。我的建议是查看原始 f:image
viewhelper 代码 (TYPO3\CMS\Fluid\ViewHelpers\ImageViewHelper
) 并复制您需要的内容。
首先将此添加到您的 viewhelper 的顶部:
/**
* @var \TYPO3\CMS\Extbase\Service\ImageService
* @inject
*/
protected $imageService;
然后是你方法中的代码
$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);
$imageUri
现在保存调整大小后的图像的路径。
PS:您复制的示例来自旧的基于 pi 的系统,该系统自 7.x 分支后不再存在。
我有一个处理一些图像的 ViewHelper。我有一个原始文件的图像路径。我需要调整这张图片的大小。
我可以在 TYPO3 中使用 PHP 代码来执行此操作吗?
我试过这个:
$imgPath = 'img/path/from_database.jpg
$imgConf = array();
$imgConf['file'] = $imgPath;
$imgConf['altText'] = "Sample alt text.";
$image = $this->cObj->IMAGE($imgConf);
但我遇到了这个异常:"Call to a member function IMAGE() on null"
我的 ViewHelper 继承自:\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper
首先你应该说明你使用的是什么 TYPO3 版本,以及你的扩展是使用 Extbase 还是旧的基于 pi 的代码。
我猜它是 Extbase,因为您是从命名空间的 viewhelper 继承的。我的建议是查看原始 f:image
viewhelper 代码 (TYPO3\CMS\Fluid\ViewHelpers\ImageViewHelper
) 并复制您需要的内容。
首先将此添加到您的 viewhelper 的顶部:
/**
* @var \TYPO3\CMS\Extbase\Service\ImageService
* @inject
*/
protected $imageService;
然后是你方法中的代码
$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);
$imageUri
现在保存调整大小后的图像的路径。
PS:您复制的示例来自旧的基于 pi 的系统,该系统自 7.x 分支后不再存在。