2016-09-07 4 views
0

外部リソースからの流体のイメージのサイズ変更が可能です。 SOAPからのデータを拡張しました。画像URLはhttp://www.example.com/url/of/image/imagename.jpgのようになります。外部リソースからのTypo3流体イメージ

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

は機能しません。

答えて

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は実稼働環境で使用しないでください。それは、そのようなビューヘルパーへの道がどのようになりうるかを示すだけです。コンパイルされたテンプレートはサポートされていません。また、ファイルがすでに存在するかどうかのチェックも必要ありません。あなたのホスティング環境にダウンロードが氾濫し、ファイルクォータが壊れる可能性があります。

+0

感謝です!正常に動作します! (/ tmp/MwtourdataOISGhs):そのようなファイルやディレクトリはありませんClasses/ViewHelpers/ExternalImageViewHelper.php line 62 私はunlink($ tempFileName)を削除すると、 ); - できます。あなたは一時ファイルのリンクを解除するのを助けることができますか? – matin

+0

'' '/ tmp /' ''ディレクトリはシステムによって代替されるべきです。 unlink( '' '' @unlink(...) '' ')の前にatを追加すると、エラーメッセージが表示されます。 別のディレクトリ(typo3tempなど)を使用して、一時ファイルを保存して、すべての作業が完了した後にそれらのリンクを解除することができます。 –

+0

ありがとう!もう1つの質問 - 全体のイメージタグの代わりにimg_resourceを得るにはどうすればいいですか?私は背景イメージ用のイメージが必要です... – matin

0

短い答えは:これは不可能です。 長い答えは:もちろん、イメージを最初に取得することは可能です。それを行うための様々な方法があります。

  • 実行時にrpflammあなたはSOAPコールを取得するとき、あなたのコントローラ/サービスでそれを行うなViewHelper
  • を使用することによって示唆されているように。 IMOこれが最良の方法です。その後、画像を持続し、あなたがフェッチイメージがそれほど大きくない場合は
  • ローカルパスを使用し、もちろんCSSを経由してサイズ変更もオプション
関連する問題