/**
  * @test
  * @dataProvider prefixIsCorrectlyAppliedToGetImageUriDataProvider
  */
 public function prefixIsCorrectlyAppliedToGetImageUri($imageUri, $expected)
 {
     $this->environmentService->expects($this->any())->method('isEnvironmentInFrontendMode')->willReturn(TRUE);
     $GLOBALS['TSFE'] = new \stdClass();
     $GLOBALS['TSFE']->absRefPrefix = '/prefix/';
     $file = $this->getMock('TYPO3\\CMS\\Core\\Resource\\File', array(), array(), '', FALSE);
     $file->expects($this->once())->method('getPublicUrl')->willReturn($imageUri);
     $this->assertSame($expected, $this->subject->getImageUri($file));
 }
 /**
  * Resizes the image (if required) and returns its path. If the image was not resized, the path will be equal to $src
  *
  * @see http://typo3.org/documentation/document-library/references/doc_core_tsref/4.2.0/view/1/5/#id4164427
  * @param string $src
  * @param FileInterface|AbstractFileFolder $image
  * @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
  * @throws \TYPO3\CMS\Fluid\Core\ViewHelper\Exception
  * @return string path to the image
  */
 public function render($src = NULL, $image = NULL, $width = NULL, $height = NULL, $minWidth = NULL, $minHeight = NULL, $maxWidth = NULL, $maxHeight = NULL, $treatIdAsReference = FALSE)
 {
     if (is_null($src) && is_null($image) || !is_null($src) && !is_null($image)) {
         throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception('You must either specify a string src or a File object.', 1382284105);
     }
     $image = $this->imageService->getImage($src, $image, $treatIdAsReference);
     $processingInstructions = array('width' => $width, 'height' => $height, 'minWidth' => $minWidth, 'minHeight' => $minHeight, 'maxWidth' => $maxWidth, 'maxHeight' => $maxHeight);
     $processedImage = $this->imageService->applyProcessingInstructions($image, $processingInstructions);
     return $this->imageService->getImageUri($processedImage);
 }
Example #3
0
 /**
  * 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 (int). 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 int $minWidth minimum width of the image
  * @param int $minHeight minimum height of the image
  * @param int $maxWidth maximum width of the image
  * @param int $maxHeight maximum height of the image
  * @param bool $treatIdAsReference given src argument is a sys_file_reference record
  * @param FileInterface|AbstractFileFolder $image a FAL object
  * @param string|bool $crop overrule cropping of image (setting to FALSE disables the cropping set in FileReference)
  * @param bool $absolute Force absolute URL
  *
  * @throws \TYPO3\CMS\Fluid\Core\ViewHelper\Exception
  * @return string Rendered tag
  */
 public function render($src = null, $width = null, $height = null, $minWidth = null, $minHeight = null, $maxWidth = null, $maxHeight = null, $treatIdAsReference = false, $image = null, $crop = null, $absolute = false)
 {
     if (is_null($src) && is_null($image) || !is_null($src) && !is_null($image)) {
         throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception('You must either specify a string src or a File object.', 1382284106);
     }
     $image = $this->imageService->getImage($src, $image, $treatIdAsReference);
     if ($crop === null) {
         $crop = $image instanceof FileReference ? $image->getProperty('crop') : null;
     }
     $processingInstructions = array('width' => $width, 'height' => $height, 'minWidth' => $minWidth, 'minHeight' => $minHeight, 'maxWidth' => $maxWidth, 'maxHeight' => $maxHeight, 'crop' => $crop);
     $processedImage = $this->imageService->applyProcessingInstructions($image, $processingInstructions);
     $imageUri = $this->imageService->getImageUri($processedImage, $absolute);
     $this->tag->addAttribute('src', $imageUri);
     $this->tag->addAttribute('width', $processedImage->getProperty('width'));
     $this->tag->addAttribute('height', $processedImage->getProperty('height'));
     $alt = $image->getProperty('alternative');
     $title = $image->getProperty('title');
     // The alt-attribute is mandatory to have valid html-code, therefore add it even if it is empty
     if (empty($this->arguments['alt'])) {
         $this->tag->addAttribute('alt', $alt);
     }
     if (empty($this->arguments['title']) && $title) {
         $this->tag->addAttribute('title', $title);
     }
     return $this->tag->render();
 }
Example #4
0
 /**
  * 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
  *
  * @throws \TYPO3\CMS\Fluid\Core\ViewHelper\Exception
  * @return string Rendered tag
  */
 public function render($src = NULL, $width = NULL, $height = NULL, $minWidth = NULL, $minHeight = NULL, $maxWidth = NULL, $maxHeight = NULL, $treatIdAsReference = FALSE, $image = NULL)
 {
     if (is_null($src) && is_null($image) || !is_null($src) && !is_null($image)) {
         throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception('You must either specify a string src or a File object.', 1382284106);
     }
     try {
         $image = $this->imageService->getImage($src, $image, $treatIdAsReference);
         $processingInstructions = array('width' => $width, 'height' => $height, 'minWidth' => $minWidth, 'minHeight' => $minHeight, 'maxWidth' => $maxWidth, 'maxHeight' => $maxHeight);
         $processedImage = $this->imageService->applyProcessingInstructions($image, $processingInstructions);
         $imageUri = $this->imageService->getImageUri($processedImage);
         $this->tag->addAttribute('src', $imageUri);
         $this->tag->addAttribute('width', $processedImage->getProperty('width'));
         $this->tag->addAttribute('height', $processedImage->getProperty('height'));
         $alt = $image->getProperty('alternative');
         $title = $image->getProperty('title');
         // The alt-attribute is mandatory to have valid html-code, therefore add it even if it is empty
         if (empty($this->arguments['alt'])) {
             $this->tag->addAttribute('alt', $alt);
         }
         if (empty($this->arguments['title']) && $title) {
             $this->tag->addAttribute('title', $title);
         }
     } catch (ResourceDoesNotExistException $e) {
         // thrown if file does not exist
     } catch (\UnexpectedValueException $e) {
         // thrown if a file has been replaced with a folder
     } catch (\RuntimeException $e) {
         // RuntimeException thrown if a file is outside of a storage
     } catch (\InvalidArgumentException $e) {
         // thrown if file storage does not exist
     }
     return $this->tag->render();
 }
 /**
  * @test
  * @dataProvider prefixIsCorrectlyAppliedToGetImageUriWithAbsolutePathDataProvider
  */
 public function prefixIsCorrectlyAppliedToGetImageUriWithForcedAbsoluteUrl($imageUri, $expected)
 {
     $this->environmentService->expects($this->any())->method('isEnvironmentInFrontendMode')->willReturn(true);
     $GLOBALS['TSFE'] = new \stdClass();
     $GLOBALS['TSFE']->absRefPrefix = '/prefix/';
     $file = $this->getMock(File::class, array(), array(), '', false);
     $file->expects($this->once())->method('getPublicUrl')->willReturn($imageUri);
     $this->assertSame($expected, $this->subject->getImageUri($file, true));
 }
Example #6
0
 /**
  * Resizes the image (if required) and returns its path. If the image was not resized, the path will be equal to $src
  *
  * @see https://docs.typo3.org/typo3cms/TyposcriptReference/ContentObjects/ImgResource/
  * @param string $src
  * @param FileInterface|AbstractFileFolder $image
  * @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
  * @throws \TYPO3\CMS\Fluid\Core\ViewHelper\Exception
  * @return string path to the image
  */
 public function render($src = NULL, $image = NULL, $width = NULL, $height = NULL, $minWidth = NULL, $minHeight = NULL, $maxWidth = NULL, $maxHeight = NULL, $treatIdAsReference = FALSE)
 {
     if (is_null($src) && is_null($image) || !is_null($src) && !is_null($image)) {
         throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception('You must either specify a string src or a File object.', 1382284105);
     }
     try {
         $image = $this->imageService->getImage($src, $image, $treatIdAsReference);
         $processingInstructions = array('width' => $width, 'height' => $height, 'minWidth' => $minWidth, 'minHeight' => $minHeight, 'maxWidth' => $maxWidth, 'maxHeight' => $maxHeight);
         $processedImage = $this->imageService->applyProcessingInstructions($image, $processingInstructions);
         return $this->imageService->getImageUri($processedImage);
     } catch (ResourceDoesNotExistException $e) {
         // thrown if file does not exist
     } catch (\UnexpectedValueException $e) {
         // thrown if a file has been replaced with a folder
     } catch (\RuntimeException $e) {
         // RuntimeException thrown if a file is outside of a storage
     } catch (\InvalidArgumentException $e) {
         // thrown if file storage does not exist
     }
     return '';
 }
 /**
  * Resizes a given image (if required) and renders the respective img tag
  *
  * @see http://typo3.org/documentation/document-library/references/doc_core_tsref/4.2.0/view/1/5/#id4164427
  * @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
  * @param boolean $onlyReturnUri
  * @param boolean $returnBackgroundStyles
  *
  * @throws \TYPO3\CMS\Fluid\Core\ViewHelper\Exception
  * @return string Rendered tag
  */
 public function render($src = NULL, $width = NULL, $height = NULL, $minWidth = NULL, $minHeight = NULL, $maxWidth = NULL, $maxHeight = NULL, $treatIdAsReference = FALSE, $image = NULL, $onlyReturnUri = NULL, $returnBackgroundStyles = NULL)
 {
     //print_r($this);//		\TYPO3\CMS\Core\Utility\DebugUtility::debug( $this );
     /*		
     		$objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('\TYPO3\CMS\Extbase\Object\ObjectManager');
     		$configurationManager = $objectManager->get('\TYPO3\CMS\Extbase\Configuration\ConfigurationManager');
     		$data = $configurationManager->getContentObject()->data;
     */
     $contextIsBackend = TYPO3_MODE == 'BE';
     $fileReferenceRepository = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('\\TYPO3\\CMS\\Core\\Resource\\FileRepository');
     if (is_null($src) && is_null($image) || !is_null($src) && !is_null($image)) {
         throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception('You must either specify a string src or a File object.', 1382284106);
     }
     $fileRef = false;
     $additionalAttributes = $this->arguments['additionalAttributes'];
     $image = $this->imageService->getImage($src, $image, $treatIdAsReference);
     $cropParams = false;
     if (!$treatIdAsReference && !$contextIsBackend && $this->templateVariableContainer && $this->templateVariableContainer->exists('contentObject')) {
         // DCE-Variante
         if ($data = $this->templateVariableContainer->get('contentObject')) {
             $fileRef = $this->getSysFileReference(array('uid_local' => $image->getUid(), 'uid_foreign' => $data['uid']));
             if ($fileRef) {
                 $treatIdAsReference = true;
             }
         }
     }
     if ($treatIdAsReference && !$contextIsBackend) {
         if (!$fileRef) {
             $fileRef = $fileReferenceRepository->findFileReferenceByUid($src);
         }
         $fileAttr = $image->getProperties();
         $cropExtensionLoaded = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('t3pimper');
         $imgvariants = json_decode($fileRef->getProperty('imgvariants'), true);
         if ($cropExtensionLoaded && $imgvariants) {
             $cropVariants = $imgvariants['crop'];
             //				\TYPO3\CMS\Core\Utility\DebugUtility::debug( $cropVariants );
             $cprop = false;
             if (($maxWidth || $maxHeight) && $cropVariants['default']) {
                 $bestprop = 'default';
                 $factor = 0;
                 $p = $cropVariants[$bestprop];
                 $tw = ($p['x2'] - $p['x1']) * $fileAttr['width'];
                 $th = ($p['y2'] - $p['y1']) * $fileAttr['height'];
                 $srcProps = $tw / $th;
                 if ($maxWidth && $maxHeight) {
                     $f = 1 / $tw * $maxWidth;
                     if ($th * $f > $maxHeight) {
                         $f = 1 / $th * $maxHeight;
                     }
                     $maxWidth = $tw * $f;
                     $maxHeight = $th * $f;
                 }
                 if (!$maxHeight) {
                     $maxHeight = $maxWidth * $srcProps;
                 }
                 if (!$maxWidth) {
                     $maxWidth = $th * $srcProps * (1 / $th * $maxHeight);
                 }
                 $cprop = 1 / $srcProps;
                 //					echo "<pre>srcProps: {$srcProps} / cprop: {$cprop} / maxWidth: {$maxWidth} / maxHeight: {$maxHeight} f:".(1/$fileAttr['height']*$maxHeight)."</pre>";
             } else {
                 if ($width && $height) {
                     $cprop = intval($height) / intval($width);
                     // Bild-Einstellung finden, die am Besten zu den Proportionen passt
                     $closest = false;
                     $bestcrop = false;
                     foreach ($cropVariants as $k => $v) {
                         $vprop = ($v['y2'] - $v['y1']) * $fileAttr['height'] / (($v['x2'] - $v['x1']) * $fileAttr['width']);
                         //						echo "<pre>{$k} [{$width}x{$height}] = vprop: {$vprop} / cprop: {$cprop} / == ".abs($vprop-$cprop)."</pre>";
                         if ($closest === false || abs($vprop - $cprop) < $closest) {
                             $closest = abs($vprop - $cprop);
                             $bestprop = $k;
                         }
                     }
                     // Faktor für Lineare Interpolation zwischen Endformat und bestmöglichem Ausschnitt
                     $factor = max(min($closest, 1), 0);
                 }
             }
             if ($cprop !== false) {
                 //					echo "<pre>best: {$bestprop} - faktor: {$factor}</pre>";
                 // Ziel-Größe des Ausschnitts errechnen
                 $p = $cropVariants[$bestprop];
                 $tw = ($p['x2'] - $p['x1']) * $fileAttr['width'];
                 $th = ($p['y2'] - $p['y1']) * $fileAttr['height'];
                 $cw = $tw + ($fileAttr['width'] - $tw) * $factor;
                 $ch = $cw * $cprop;
                 $tw_x1 = $fileAttr['width'] * $p['x1'];
                 $tw_x2 = $fileAttr['width'] * $p['x2'];
                 $tw_y1 = $fileAttr['height'] * $p['y1'];
                 $tw_y2 = $fileAttr['height'] * $p['y2'];
                 $x1 = $tw_x1 + ($tw_x2 - $tw_x1) / 2 - $cw / 2;
                 $y1 = $tw_y1 + ($tw_y2 - $tw_y1) / 2 - $ch / 2;
                 $m = abs($ch - ($tw_y2 - $tw_y1));
                 //					echo "<pre>{$cw} x {$ch}<br /> x1:{$x1} y1:{$y1}<br /> {$m} </pre>";
                 if ($x1 + $cw > $fileAttr['width']) {
                     $x1 = $fileAttr['width'] - $cw;
                 }
                 if ($x1 < 0) {
                     $x1 = 0;
                 }
                 if ($y1 + $ch > $fileAttr['height']) {
                     $y1 = $fileAttr['height'] - $ch;
                 }
                 if ($y1 < 0) {
                     $y1 = 0;
                 }
                 $cropParams = array('x1' => $x1, 'y1' => $y1, 'x2' => $x1 + $cw, 'y2' => $y1 + $ch);
             }
         }
     }
     $processingInstructions = array('width' => $width, 'height' => $height, 'minWidth' => $minWidth, 'minHeight' => $minHeight, 'maxWidth' => $maxWidth, 'maxHeight' => $maxHeight);
     if ($cropParams) {
         //echo "<pre>".print_r($cropParams,true)."</pre>";
         $cropProcessingInstructions = $this->initCrop($cropParams, $image->getProperties(), $processingInstructions);
         $processedImage = $this->imageService->applyProcessingInstructions($image, $cropProcessingInstructions);
         $imageUri = $this->imageService->getImageUri($processedImage);
     } else {
         $processedImage = $this->imageService->applyProcessingInstructions($image, $processingInstructions);
         $imageUri = $this->imageService->getImageUri($processedImage);
         // \TYPO3\CMS\Core\Utility\DebugUtility::debug( $processedImage );
     }
     if ($onlyReturnUri && !$returnBackgroundStyles) {
         return $imageUri;
     }
     $this->tag->addAttribute('src', $imageUri);
     $this->tag->addAttribute('width', $processedImage->getProperty('width'));
     $this->tag->addAttribute('height', $processedImage->getProperty('height'));
     //echo "<img src=\"$imageUri\" />";
     $alt = $image->getProperty('alternative');
     $title = $image->getProperty('title');
     // The alt-attribute is mandatory to have valid html-code, therefore add it even if it is empty
     if (empty($this->arguments['alt'])) {
         $this->tag->addAttribute('alt', $alt);
     }
     if (empty($this->arguments['title']) && $title) {
         $this->tag->addAttribute('title', $title);
     }
     $this->cObj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('\\TYPO3\\CMS\\Frontend\\ContentObject\\ContentObjectRenderer');
     $configuration = $GLOBALS['TSFE']->tmpl->setup['tt_content.']["image."]['20.']['1.'];
     $layoutKey = $configuration['layoutKey'];
     unset($configuration['file.']);
     // Render srcset-Attribute
     $attr = $this->cObj->getImageSourceCollection($layoutKey, $configuration, $imageUri);
     //echo $imageUri;
     $this->tag->addAttribute($layoutKey, $attr);
     // Rendering mit Focus-Point
     if (TYPO3_MODE != 'BE') {
         if ($additionalAttributes['useFocusPoint'] || $returnBackgroundStyles) {
             $focalpoint = $imgvariants['focalpoint']['default'];
             if (!$focalpoint) {
                 $focalpoint = array('x1' => 0.5, 'y1' => 0.5);
             }
             $this->tag->removeAttribute('useFocusPoint');
             $imgWidth = $this->tag->getAttribute('width');
             $imgHeight = $this->tag->getAttribute('height');
             $class = $this->tag->getAttribute('class');
             $id = $this->tag->getAttribute('id');
             $GLOBALS['TSFE']->getPageRenderer()->addCssFile('typo3conf/ext/t3pimper/Resources/Public/Frontend/Css/focuspoint.css');
             $GLOBALS['TSFE']->getPageRenderer()->addJsLibrary('focuspoint', 'typo3conf/ext/t3pimper/Resources/Public/Frontend/Js/jquery.focuspoint.min.js');
             $this->tag->removeAttribute('id');
             $this->tag->removeAttribute('class');
             $this->tag->removeAttribute('width');
             $this->tag->removeAttribute('height');
             $imgTag = $this->tag->render();
             if ($returnBackgroundStyles) {
                 $focalpointX = 100 * $focalpoint['x1'];
                 $focalpointY = 100 * $focalpoint['y1'];
                 return "background-image:url({$imageUri});background-position:{$focalpointX}% {$focalpointY}%;background-size:cover;";
             } else {
                 $focalpointX = max(-1, min(1, $focalpoint['x1']) * 2 - 1);
                 $focalpointY = max(-1, min(1, $focalpoint['y1']) * 2 - 1);
                 return "<div  style=\"width:100%;height:{$imgHeight}px\" class=\"focuspoint\" data-focus-x=\"{$focalpointX}\" data-focus-y=\"{$focalpointY}\" data-image-w=\"{$imgWidth}\" data-image-h=\"{$imgHeight}\">{$imgTag}</div>";
             }
         }
     }
     return $this->tag->render();
 }