来自外部资源的 Typo3 流体图像

Typo3 fluid image from external resource

是否可以从外部资源调整流体图像的大小。我有一个来自 SOAP 的数据扩展。所以图像 URL 看起来像 http://www.example.com/url/of/image/imagename.jpg.

<f:image src="{data.url.image}" with="300" />

不工作。

也许自己的 ViewHelper 可以获取外部图像并将其保存到临时文件夹中。之后就可以修改图片了。

像这样(未测试):

<?php
  namespace MyNamespaece\MyExt\ViewHelpers;

  use TYPO3\CMS\Core\Utility\GeneralUtility;
  use TYPO3\CMS\Fluid\ViewHelpers\ImageViewHelper;
  use TYPO3\CMS\Core\Resource\FileInterface;
  use TYPO3\CMS\Extbase\Domain\Model\AbstractFileFolder;

  class ExternalImageViewHelper extends ImageViewHelper
  {

  const UPLOAD_DIRECTORY = 'externalImages';
  const TEMP_PREFIX = 'MyExt';

  /**
   * ResourceFactory
   *
   * @var \TYPO3\CMS\Core\Resource\ResourceFactory
   * @inject
   */
  protected $resourceFactory = null;

  /**
   * Resizes a given image (if required) and renders the respective img tag
   *
   * @see https://docs.typo3.org/typo3cms/TyposcriptReference/ContentObjects/Image/
   *
   * @param string                           $src                a path to a file, a combined FAL identifier or an uid (integer). If $treatIdAsReference is set, the integer is considered the uid of the sys_file_reference record. If you already got a FAL object, consider using the $image parameter instead
   * @param string                           $width              width of the image. This can be a numeric value representing the fixed width of the image in pixels. But you can also perform simple calculations by adding "m" or "c" to the value. See imgResource.width for possible options.
   * @param string                           $height             height of the image. This can be a numeric value representing the fixed height of the image in pixels. But you can also perform simple calculations by adding "m" or "c" to the value. See imgResource.width for possible options.
   * @param integer                          $minWidth           minimum width of the image
   * @param integer                          $minHeight          minimum height of the image
   * @param integer                          $maxWidth           maximum width of the image
   * @param integer                          $maxHeight          maximum height of the image
   * @param boolean                          $treatIdAsReference given src argument is a sys_file_reference record
   * @param FileInterface|AbstractFileFolder $image              a FAL object
   *
   * @return string
   * @throws \Exception
   * @throws \TYPO3\CMS\Core\Resource\Exception\InsufficientFolderAccessPermissionsException
   * @throws \TYPO3\CMS\Core\Resource\Exception\InsufficientFolderWritePermissionsException
   * @throws \TYPO3\CMS\Fluid\Core\ViewHelper\Exception
   */
  public function render($src = null, $width = null, $height = null, $minWidth = null, $minHeight = null, $maxWidth = null, $maxHeight = null, $treatIdAsReference = false, $image = null)
  {
    if (filter_var($src, FILTER_VALIDATE_URL)) {
      $storage = $this->resourceFactory->getDefaultStorage();
      if (!$storage->hasFolder(self::UPLOAD_DIRECTORY)) {
        $storage->createFolder(self::UPLOAD_DIRECTORY);
      }

      $externalFile = GeneralUtility::getUrl($src);
      if ($externalFile) {
        $tempFileName = tempnam(sys_get_temp_dir(), self::TEMP_PREFIX);
        $handle       = fopen($tempFileName, "w");
        fwrite($handle, $externalFile);
        fclose($handle);

        $uploadFolder = $storage->getFolder(self::UPLOAD_DIRECTORY);
        $file         = $uploadFolder->addFile($tempFileName, basename(basename($src)), 'changeName');
        $src          = $file->getPublicUrl();
        unlink($tempFileName);
      } else {
        throw new \Exception(sprintf('External URL % cannot accessed.', $src), 1473233519);
      }
    }

    return parent::render($src, $width, $height, $minWidth, $minHeight, $maxWidth, $maxHeight, $treatIdAsReference, $image);
  }
}

请注意:此 ViewHelper 不检查图像是否已获取!所以应该集成检查。否则这个 viewhelper 在每次页面刷新时获取图像!

如评论中所述,我想澄清的是,不应在任何生产环境中使用此 ViewHelper。它应该只演示如何使用这种 viewhelper。不支持已编译的模板。也不需要检查文件是否已经存在。您的托管环境可能会被下载淹没,并可能打破您的文件配额!

简短的回答是:这是不可能的。 长答案是:当然,如果你先获取图像是可能的。有多种方法可以做到:

  • 在运行时按照 rpflamm 的建议使用 ViewHelper
  • 在获取 SOAP 调用时在 controller/service 中执行此操作。海事组织这将是最好的方法。然后保留图像并使用本地路径
  • 如果你抓取的图片不是那么大,当然也可以通过CSS调整大小