【问题标题】:Resize image in custom ViewHelper在自定义 ViewHelper 中调整图像大小
【发布时间】:2016-10-14 08:35:54
【问题描述】:

我有一个处理一些图像的 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

【问题讨论】:

  • 我假设这是表pages 的字段media,对吗?您可能会考虑,使用可用的 <f:image> view-helper... 视图组件是从 Extbase 上下文调用还是由某些 TypoScript FLUIDTEMPLATE 调用?
  • @OliverHader 抱歉,代码是从其他网站粘贴的,我现在对其进行了编辑以更接近地匹配我的代码,但这不会影响此问题。这个 ViewHelper 在 Fluid 模板中被调用,放置在同一个扩展(作为 ViewHelper 的扩展)中,所以我假设它是 Extbase 上下文。我想使用 但我不知道如何在 PHP 代码中使用它?

标签: typo3 fluid


【解决方案1】:

首先你应该说明你使用的是什么 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:您复制的示例来自自 7.x 分支以来不再存在的旧 pibased 系统。

【讨论】:

  • 谢谢,效果很好。唯一的问题是依赖注入在我的情况下不起作用 - 我清除了缓存(后端的缓存文件夹和缓存图标)并用 LF 替换了 CR LF - 没有帮助。因此,我必须以这种方式获取这个对象\TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance("TYPO3\\CMS\\Extbase\\Service\\ImageService")。
  • 谢谢,我必须使用 TYPO3 8.7 将 3 arguments 提供给 getImage 函数 $image = $this->imageService->getImage($filename, null, false);。我也遇到了艾伦的注射问题。我的解决方案是在安装工具中Dump Autoload Information。
  • 当你创建一个新类时,你总是需要更新自动加载文件。您可以将其与“转储自动加载信息”一起使用,也可以仅删除 typo3temp/autoload 中的文件
猜你喜欢
  • 2017-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多