"Fossies" - the Fresh Open Source Software Archive

Member "neos-development-collection-7.0.1/Neos.Media.Browser/Classes/ViewHelpers/ThumbnailViewHelper.php" (23 Feb 2021, 2836 Bytes) of package /linux/www/neos-development-collection-7.0.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 <?php
    2 namespace Neos\Media\Browser\ViewHelpers;
    3 
    4 /*
    5  * This file is part of the Neos.Media.Browser package.
    6  *
    7  * (c) Contributors of the Neos Project - www.neos.io
    8  *
    9  * This package is Open Source Software. For the full copyright and license
   10  * information, please view the LICENSE file which was distributed with this
   11  * source code.
   12  */
   13 
   14 use Neos\Media\Domain\Model\AssetSource\AssetProxy\AssetProxyInterface;
   15 use Neos\FluidAdaptor\Core\ViewHelper\AbstractTagBasedViewHelper;
   16 
   17 /**
   18  * Renders an <img> HTML tag from a given Asset Proxy instance
   19  *
   20  * This view helper is INTERNAL for now, and only used in the Media Browser.
   21  * The functionality of this view helper might become part of the Neos.Media
   22  * thumbnail view helper if there is a demand for it.
   23  *
   24  * = Examples =
   25  *
   26  * <code title="Rendering an asset proxy thumbnail">
   27  * <mediaBrowser:thumbnail assetProxy="{assetProxyObject}" alt="a sample asset" />
   28  * </code>
   29  * <output>
   30  * <img src="https://my-asset-management.com/thumbnails/espresso.jpg" width="120" height="180" alt="a sample asset without scaling" />
   31  * </output>
   32  */
   33 class ThumbnailViewHelper extends AbstractTagBasedViewHelper
   34 {
   35     /**
   36      * name of the tag to be created by this view helper
   37      *
   38      * @var string
   39      */
   40     protected $tagName = 'img';
   41 
   42     /**
   43      * @return void
   44      * @throws \Neos\FluidAdaptor\Core\ViewHelper\Exception
   45      */
   46     public function initializeArguments()
   47     {
   48         parent::initializeArguments();
   49         $this->registerUniversalTagAttributes();
   50         $this->registerTagAttribute('alt', 'string', 'Specifies an alternate text for an asset', true);
   51 
   52         $this->registerArgument('assetProxy', AssetProxyInterface::class, 'The asset to be rendered as a thumbnail', true);
   53         $this->registerArgument('width', 'integer', 'Desired width of the thumbnail');
   54         $this->registerArgument('height', 'integer', 'Desired height of the thumbnail');
   55     }
   56 
   57     /**
   58      * Renders an HTML img tag with a thumbnail or preview image, created from a given asset proxy.
   59      *
   60      * @return string an <img...> html tag
   61      */
   62     public function render(): string
   63     {
   64         /** @var AssetProxyInterface $assetProxy */
   65         $assetProxy = $this->arguments['assetProxy'];
   66         $width= $this->arguments['width'];
   67         $height= $this->arguments['height'];
   68 
   69         if ($width === null || $height === null) {
   70             $width = 250;
   71             $height = 250;
   72         }
   73 
   74         if ($width <= 250 && $height <= 250) {
   75             $thumbnailUri = $assetProxy->getThumbnailUri();
   76         } else {
   77             $thumbnailUri = $assetProxy->getPreviewUri();
   78         }
   79 
   80         $this->tag->addAttributes([
   81             'width' => $width,
   82             'height' => $height,
   83             'src' => $thumbnailUri
   84         ]);
   85 
   86         return $this->tag->render();
   87     }
   88 }