, famelo * @package Vhs * @subpackage ViewHelpers\Media */ class SourceViewHelper extends AbstractTagBasedViewHelper { const SCOPE = 'FluidTYPO3\Vhs\ViewHelpers\Media\PictureViewHelper'; const SCOPE_VARIABLE_SRC = 'src'; const SCOPE_VARIABLE_DEFAULT_SOURCE = 'default-source'; /** * name of the tag to be created by this view helper * * @var string * @api */ protected $tagName = 'source'; /** * @var ContentObjectRenderer */ protected $contentObject; /** * @var ConfigurationManagerInterface */ protected $configurationManager; /** * @param ConfigurationManagerInterface $configurationManager * @return void */ public function injectConfigurationManager(ConfigurationManagerInterface $configurationManager) { $this->configurationManager = $configurationManager; $this->contentObject = $this->configurationManager->getContentObject(); } /** * Initialize arguments. * * @return void * @api */ public function initializeArguments() { parent::initializeArguments(); $this->registerArgument('media', 'string', 'Media query for which breakpoint this sources applies', FALSE, NULL); $this->registerArgument('width', 'string', '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.', FALSE); $this->registerArgument('height', 'string', '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.', FALSE); $this->registerArgument('maxW', 'integer', 'Maximum Width of the image. (no upscaling)', FALSE); $this->registerArgument('maxH', 'integer', 'Maximum Height of the image. (no upscaling)', FALSE); $this->registerArgument('minW', 'integer', 'Minimum Width of the image.', FALSE); $this->registerArgument('minH', 'integer', 'Minimum Height of the image.', FALSE); $this->registerArgument('format', 'string', 'Format of the processed file - also determines the target file format. If blank, TYPO3/IM/GM default is taken into account.', FALSE, NULL); $this->registerArgument('quality', 'integer', 'Quality of the processed image. If blank/not present falls back to the default quality defined in install tool.', FALSE, NULL); } /** * Render method * * @return string */ public function render() { $src = $this->viewHelperVariableContainer->get(self::SCOPE, self::SCOPE_VARIABLE_SRC); if ('BE' === TYPO3_MODE) { $tsfeBackup = FrontendSimulationUtility::simulateFrontendEnvironment(); } $setup = array( 'width' => $this->arguments['width'], 'height' => $this->arguments['height'], 'minW' => $this->arguments['minW'], 'minH' => $this->arguments['minH'], 'maxW' => $this->arguments['maxW'], 'maxH' => $this->arguments['maxH'] ); $quality = $this->arguments['quality']; $format = $this->arguments['format']; if (FALSE === empty($format)) { $setup['ext'] = $format; } if (0 < intval($quality)) { $quality = MathUtility::forceIntegerInRange($quality, 10, 100, 75); $setup['params'] .= ' -quality ' . $quality; } if ('BE' === TYPO3_MODE && '../' === substr($src, 0, 3)) { $src = substr($src, 3); } $result = $this->contentObject->getImgResource($src, $setup); if ('BE' === TYPO3_MODE) { FrontendSimulationUtility::resetFrontendEnvironment($tsfeBackup); } $src = $this->preprocessSourceUri(rawurldecode($result[3])); if (NULL === $this->arguments['media']) { $this->viewHelperVariableContainer->addOrUpdate(self::SCOPE, self::SCOPE_VARIABLE_DEFAULT_SOURCE, $src); } else { $this->tag->addAttribute('media', $this->arguments['media']); } $this->tag->addAttribute('srcset', $src); return $this->tag->render(); } /** * Turns a relative source URI into an absolute URL * if required * * @param string $src * @return string */ public function preprocessSourceUri($src) { if (FALSE === empty($GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_vhs.']['settings.']['prependPath'])) { $src = $GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_vhs.']['settings.']['prependPath'] . $src; } elseif ('BE' === TYPO3_MODE || FALSE === (boolean) $this->arguments['relative']) { $src = GeneralUtility::getIndpEnv('TYPO3_SITE_URL') . ltrim($src, '/'); } return $src; } }