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

时间:2016-09-07 06:33:25

标签: image resources typo3 external fluid

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

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

无效。

2 个答案:

答案 0 :(得分:1)

也许自己的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不应该在任何生产环境中使用。它应该只展示如何通过这种观察者的方式。不支持编译的模板。也不需要检查文件是否已经存在。您的托管环境可能充斥着下载,可能会破坏您的文件配额!

答案 1 :(得分:0)

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

  • 在运行时使用ViewHelper建议的rpflamm
  • 在您获取SOAP电话时,在您的控制器/服务中执行此操作。 IMO这将是最好的方式。然后坚持图像并使用本地路径
  • 如果您获取的图像不是那么大,当然通过CSS调整大小也是一个选项