Skip to content

Commit

Permalink
[BUGFIX] Allow external images for FluidEmail image ViewHelpers
Browse files Browse the repository at this point in the history
To overcome external images via FluidEmail and apply the similar
behaviour as in LoginController, the <f:image> and <f:uri.image>
ViewHelpers do not process external images but just return them,
allowing logins again with custom login images and "sendNotificationEmail"
settings turned on.

Resolves: #90656
Releases: master
Change-Id: I8ae70da66ee506c085a88690f27d0930b4bfc99d
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/63635
Tested-by: Jonas Eberle <flightvision@googlemail.com>
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: Georg Ringer <georg.ringer@gmail.com>
Reviewed-by: Jonas Eberle <flightvision@googlemail.com>
Reviewed-by: Georg Ringer <georg.ringer@gmail.com>
  • Loading branch information
bmack authored and georgringer committed Apr 12, 2020
1 parent 0babc9f commit e6e40cd
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 49 deletions.
111 changes: 62 additions & 49 deletions typo3/sysext/fluid/Classes/ViewHelpers/ImageViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
/**
* Resizes a given image (if required) and renders the respective img tag.
*
* External URLs are not processed. Only a given width and height will be set on the tag.
*
* Examples
* ========
*
Expand Down Expand Up @@ -142,65 +144,76 @@ public function initializeArguments()
*/
public function render()
{
if (($this->arguments['src'] === '' && $this->arguments['image'] === null) || ($this->arguments['src'] !== '' && $this->arguments['image'] !== null)) {
$src = (string)$this->arguments['src'];
if (($src === '' && $this->arguments['image'] === null) || ($src !== '' && $this->arguments['image'] !== null)) {
throw new Exception('You must either specify a string src or a File object.', 1382284106);
}

try {
$image = $this->imageService->getImage((string)$this->arguments['src'], $this->arguments['image'], (bool)$this->arguments['treatIdAsReference']);
$cropString = $this->arguments['crop'];
if ($cropString === null && $image->hasProperty('crop') && $image->getProperty('crop')) {
$cropString = $image->getProperty('crop');
// A URL was given as src, this is kept as is, and we can only scale
if ($src !== '' && preg_match('/^(https?:)?\/\//', $src)) {
$this->tag->addAttribute('src', $src);
if (isset($this->arguments['width'])) {
$this->tag->addAttribute('width', $this->arguments['width']);
}
$cropVariantCollection = CropVariantCollection::create((string)$cropString);
$cropVariant = $this->arguments['cropVariant'] ?: 'default';
$cropArea = $cropVariantCollection->getCropArea($cropVariant);
$processingInstructions = [
'width' => $this->arguments['width'],
'height' => $this->arguments['height'],
'minWidth' => $this->arguments['minWidth'],
'minHeight' => $this->arguments['minHeight'],
'maxWidth' => $this->arguments['maxWidth'],
'maxHeight' => $this->arguments['maxHeight'],
'crop' => $cropArea->isEmpty() ? null : $cropArea->makeAbsoluteBasedOnFile($image),
];
if (!empty($this->arguments['fileExtension'] ?? '')) {
$processingInstructions['fileExtension'] = $this->arguments['fileExtension'];
if (isset($this->arguments['height'])) {
$this->tag->addAttribute('height', $this->arguments['height']);
}
$processedImage = $this->imageService->applyProcessingInstructions($image, $processingInstructions);
$imageUri = $this->imageService->getImageUri($processedImage, $this->arguments['absolute']);
} else {
try {
$image = $this->imageService->getImage($src, $this->arguments['image'], (bool)$this->arguments['treatIdAsReference']);
$cropString = $this->arguments['crop'];
if ($cropString === null && $image->hasProperty('crop') && $image->getProperty('crop')) {
$cropString = $image->getProperty('crop');
}
$cropVariantCollection = CropVariantCollection::create((string)$cropString);
$cropVariant = $this->arguments['cropVariant'] ?: 'default';
$cropArea = $cropVariantCollection->getCropArea($cropVariant);
$processingInstructions = [
'width' => $this->arguments['width'],
'height' => $this->arguments['height'],
'minWidth' => $this->arguments['minWidth'],
'minHeight' => $this->arguments['minHeight'],
'maxWidth' => $this->arguments['maxWidth'],
'maxHeight' => $this->arguments['maxHeight'],
'crop' => $cropArea->isEmpty() ? null : $cropArea->makeAbsoluteBasedOnFile($image),
];
if (!empty($this->arguments['fileExtension'] ?? '')) {
$processingInstructions['fileExtension'] = $this->arguments['fileExtension'];
}
$processedImage = $this->imageService->applyProcessingInstructions($image, $processingInstructions);
$imageUri = $this->imageService->getImageUri($processedImage, $this->arguments['absolute']);

if (!$this->tag->hasAttribute('data-focus-area')) {
$focusArea = $cropVariantCollection->getFocusArea($cropVariant);
if (!$focusArea->isEmpty()) {
$this->tag->addAttribute('data-focus-area', $focusArea->makeAbsoluteBasedOnFile($image));
if (!$this->tag->hasAttribute('data-focus-area')) {
$focusArea = $cropVariantCollection->getFocusArea($cropVariant);
if (!$focusArea->isEmpty()) {
$this->tag->addAttribute('data-focus-area', $focusArea->makeAbsoluteBasedOnFile($image));
}
}
}
$this->tag->addAttribute('src', $imageUri);
$this->tag->addAttribute('width', $processedImage->getProperty('width'));
$this->tag->addAttribute('height', $processedImage->getProperty('height'));
$this->tag->addAttribute('src', $imageUri);
$this->tag->addAttribute('width', $processedImage->getProperty('width'));
$this->tag->addAttribute('height', $processedImage->getProperty('height'));

// 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', $image->hasProperty('alternative') ? $image->getProperty('alternative') : '');
}
if (empty($this->arguments['title']) && $image->hasProperty('title')) {
$this->tag->addAttribute('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', $image->hasProperty('alternative') ? $image->getProperty('alternative') : '');
}
if (empty($this->arguments['title']) && $image->hasProperty('title')) {
$this->tag->addAttribute('title', $image->getProperty('title'));
}
} catch (ResourceDoesNotExistException $e) {
// thrown if file does not exist
throw new Exception($e->getMessage(), 1509741911, $e);
} catch (\UnexpectedValueException $e) {
// thrown if a file has been replaced with a folder
throw new Exception($e->getMessage(), 1509741912, $e);
} catch (\RuntimeException $e) {
// RuntimeException thrown if a file is outside of a storage
throw new Exception($e->getMessage(), 1509741913, $e);
} catch (\InvalidArgumentException $e) {
// thrown if file storage does not exist
throw new Exception($e->getMessage(), 1509741914, $e);
}
} catch (ResourceDoesNotExistException $e) {
// thrown if file does not exist
throw new Exception($e->getMessage(), 1509741911, $e);
} catch (\UnexpectedValueException $e) {
// thrown if a file has been replaced with a folder
throw new Exception($e->getMessage(), 1509741912, $e);
} catch (\RuntimeException $e) {
// RuntimeException thrown if a file is outside of a storage
throw new Exception($e->getMessage(), 1509741913, $e);
} catch (\InvalidArgumentException $e) {
// thrown if file storage does not exist
throw new Exception($e->getMessage(), 1509741914, $e);
}

return $this->tag->render();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
/**
* Resizes a given image (if required) and returns its relative path.
*
* External URLs are not processed and just returned as is.
*
* Examples
* ========
*
Expand Down Expand Up @@ -126,6 +128,11 @@ public static function renderStatic(array $arguments, \Closure $renderChildrenCl
throw new Exception('You must either specify a string src or a File object.', 1460976233);
}

// A URL was given as src, this is kept as is
if ($src !== '' && preg_match('/^(https?:)?\/\//', $src)) {
return $src;
}

try {
$imageService = self::getImageService();
$image = $imageService->getImage($src, $image, $treatIdAsReference);
Expand Down

0 comments on commit e6e40cd

Please sign in to comment.