Skip to content

Commit

Permalink
[BUGFIX] Make file paths absolute in GIFBUILDER
Browse files Browse the repository at this point in the history
In CLI context the full path to the temporary generated file by the
GIFBUILDER can't be resolved. Because of this inconsistency, absolute
file paths are used from now on.

Resolves: #95379
Releases: main
Change-Id: If3a8613ed8e8d11a1b8a474fa564f947ef8a5c0c
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/73932
Tested-by: core-ci <typo3@b13.com>
Tested-by: Nikita Hovratov <nikita.h@live.de>
Tested-by: Stefan Bürk <stefan@buerk.tech>
Reviewed-by: Nikita Hovratov <nikita.h@live.de>
Reviewed-by: Stefan Bürk <stefan@buerk.tech>
  • Loading branch information
simonschaufi authored and sbuerk committed Jun 6, 2022
1 parent 1088be6 commit 7a3202c
Show file tree
Hide file tree
Showing 6 changed files with 221 additions and 42 deletions.
18 changes: 9 additions & 9 deletions typo3/sysext/core/Classes/Imaging/GraphicalFunctions.php
Expand Up @@ -2069,7 +2069,7 @@ public function objPosition($conf, $workArea, $BB)
/**
* Converts $imagefile to another file in temp-dir of type $newExt (extension).
*
* @param string $imagefile The image filepath
* @param string $imagefile The absolute image filepath
* @param string $newExt New extension, eg. "gif", "png", "jpg", "tif". If $newExt is NOT set, the new imagefile will be of the original format. If newExt = 'WEB' then one of the web-formats is applied.
* @param string $w Width. $w / $h is optional. If only one is given the image is scaled proportionally. If an 'm' exists in the $w or $h and if both are present the $w and $h is regarded as the Maximum w/h and the proportions will be kept
* @param string $h Height. See $w
Expand Down Expand Up @@ -2189,8 +2189,8 @@ public function imageMagickConvert($imagefile, $newExt = '', $w = '', $h = '', $
/**
* Gets the input image dimensions.
*
* @param string $imageFile The image filepath
* @return array|null Returns an array where [0]/[1] is w/h, [2] is extension and [3] is the filename.
* @param string $imageFile The absolute image filepath
* @return array|null Returns an array where [0]/[1] is w/h, [2] is extension and [3] is the absolute filepath.
* @see imageMagickConvert()
* @see \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::getImgResource()
*/
Expand Down Expand Up @@ -2243,7 +2243,7 @@ public function cacheImageDimensions(array $identifyResult)
/**
* Fetches the cached image dimensions from the cache. Does not check if the image file exists.
*
* @param string $filePath Image file path, relative to public web path
* @param string $filePath The absolute image file path
*
* @return array|bool an array where [0]/[1] is w/h, [2] is extension and [3] is the file name,
* or FALSE for a cache miss
Expand Down Expand Up @@ -2280,19 +2280,19 @@ public function getCachedImageDimensions($filePath)
*
* This method does not check if the image file actually exists.
*
* @param string $filePath Image file path, relative to public web path
* @param string $filePath Absolute image file path
*
* @return string the hash key (an SHA1 hash), will not be empty
*/
protected function generateCacheKeyForImageFile($filePath)
{
return sha1($filePath);
return sha1(PathUtility::stripPathSitePrefix($filePath));
}

/**
* Creates the status hash to check whether a file has been changed.
*
* @param string $filePath Image file path, relative to public web path
* @param string $filePath Absolute image file path
*
* @return string the status hash (an SHA1 hash)
*/
Expand Down Expand Up @@ -2681,7 +2681,7 @@ public function gif_or_jpg($type, $w, $h)
* Used in GIFBUILDER
* Uses $this->setup['reduceColors'] for gif/png images and $this->setup['quality'] for jpg images to reduce size/quality if needed.
*
* @param string $file The filename to write to.
* @param string $file The absolute filename to write to
* @return string Returns input filename
* @see \TYPO3\CMS\Frontend\Imaging\GifBuilder::gifBuild()
*/
Expand Down Expand Up @@ -2747,7 +2747,7 @@ public function imgTag($imgInfo)
* Writes the input GDlib image pointer to file
*
* @param resource $destImg The GDlib image resource pointer
* @param string $theImage The filename to write to
* @param string $theImage The absolute file path to write to
* @param int $quality The image quality (for JPEGs)
* @return bool The output of either imageGif, imagePng or imageJpeg based on the filename to write
* @see maskImageOntoImage()
Expand Down
Expand Up @@ -3819,13 +3819,14 @@ public function getImgResource($file, $fileArray)
$imageResource = null;
if ($file === 'GIFBUILDER') {
$gifCreator = GeneralUtility::makeInstance(GifBuilder::class);
$theImage = '';
if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib']) {
$gifCreator->start($fileArray, $this->data);
$theImage = $gifCreator->gifBuild();
if ($theImage !== '') {
$imageResource = $gifCreator->getImageDimensions(Environment::getPublicPath() . '/' . $theImage);
$imageResource['origFile'] = $theImage;
}
}
$imageResource = $gifCreator->getImageDimensions($theImage);
$imageResource['origFile'] = $theImage;
} else {
if ($file instanceof File) {
$fileObject = $file;
Expand Down Expand Up @@ -3909,7 +3910,7 @@ public function getImgResource($file, $fileArray)
0 => (int)$processedFileObject->getProperty('width'),
1 => (int)$processedFileObject->getProperty('height'),
2 => $processedFileObject->getExtension(),
3 => $processedFileObject->getPublicUrl(),
3 => Environment::getPublicPath() . '/' . $processedFileObject->getPublicUrl(),
'origFile' => $fileObject->getPublicUrl(),
'origFile_mtime' => $fileObject->getModificationTime(),
// This is needed by \TYPO3\CMS\Frontend\Imaging\GifBuilder,
Expand All @@ -3929,7 +3930,7 @@ public function getImgResource($file, $fileArray)
$info = GeneralUtility::makeInstance(GifBuilder::class)->imageMagickConvert($theImage, 'WEB');
$info['origFile'] = $theImage;
// This is needed by \TYPO3\CMS\Frontend\Imaging\GifBuilder, ln 100ff in order for the setup-array to create a unique filename hash.
$info['origFile_mtime'] = @filemtime($theImage);
$info['origFile_mtime'] = @filemtime(Environment::getPublicPath() . '/' . $theImage);
$imageResource = $info;
} catch (Exception $e) {
// do nothing in case the file path is invalid
Expand Down
Expand Up @@ -20,6 +20,7 @@
use TYPO3\CMS\Core\Service\MarkerBasedTemplateService;
use TYPO3\CMS\Core\TypoScript\TypoScriptService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\PathUtility;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;

/**
Expand Down Expand Up @@ -68,10 +69,10 @@ protected function cImage($file, $conf)
}
// $info['originalFile'] will be set, when the file is processed by FAL.
// In that case the URL is final and we must not add a prefix
if (!isset($info['originalFile']) && is_file(Environment::getPublicPath() . '/' . $info['3'])) {
$source = $tsfe->absRefPrefix . str_replace('%2F', '/', rawurlencode($info['3']));
if (!isset($info['originalFile']) && is_file($info['3'])) {
$source = $tsfe->absRefPrefix . str_replace('%2F', '/', rawurlencode(PathUtility::stripPathSitePrefix($info['3'])));
} else {
$source = $info[3];
$source = PathUtility::stripPathSitePrefix($info[3]);
}
// Remove file objects for AssetCollector, as it only allows to store scalar values
$infoOriginalFile = $info['originalFile'] ?? null;
Expand Down
Expand Up @@ -15,6 +15,8 @@

namespace TYPO3\CMS\Frontend\ContentObject;

use TYPO3\CMS\Core\Utility\PathUtility;

/**
* Contains IMG_RESOURCE class object.
*/
Expand All @@ -31,7 +33,7 @@ public function render($conf = [])
{
$GLOBALS['TSFE']->lastImgResourceInfo = $this->cObj->getImgResource($conf['file'] ?? '', $conf['file.'] ?? []);
if ($GLOBALS['TSFE']->lastImgResourceInfo) {
$imageResource = $GLOBALS['TSFE']->lastImgResourceInfo[3];
$imageResource = PathUtility::stripPathSitePrefix($GLOBALS['TSFE']->lastImgResourceInfo[3]);
$theValue = isset($conf['stdWrap.']) ? $this->cObj->stdWrap($imageResource, $conf['stdWrap.']) : $imageResource;
} else {
$theValue = '';
Expand Down
31 changes: 16 additions & 15 deletions typo3/sysext/frontend/Classes/Imaging/GifBuilder.php
Expand Up @@ -329,27 +329,28 @@ public function start($conf, $data)
* Gets filename from fileName() and if file exists in typo3temp/assets/images/ dir it will - of course - not be rendered again.
* Otherwise rendering means calling ->make(), then ->output(), then ->destroy()
*
* @return string The filename for the created GIF/PNG file. The filename will be prefixed "GB_
* @return string The filename for the created GIF/PNG file.
* @see make()
* @see fileName()
*/
public function gifBuild()
{
if ($this->setup) {
// Relative to Environment::getPublicPath()
$gifFileName = $this->fileName('assets/images/');
// File exists
if (!file_exists($gifFileName)) {
// Create temporary directory if not done:
GeneralUtility::mkdir_deep(Environment::getPublicPath() . '/typo3temp/assets/images/');
// Create file:
$this->make();
$this->output($gifFileName);
$this->destroy();
}
return $gifFileName;
if (!$this->setup) {
return '';
}

// Relative to Environment::getPublicPath()
$gifFileName = $this->fileName('assets/images/');

if (!file_exists(Environment::getPublicPath() . '/' . $gifFileName)) {
// Create temporary directory if not done
GeneralUtility::mkdir_deep(Environment::getPublicPath() . '/typo3temp/assets/images/');
// Create file
$this->make();
$this->output(Environment::getPublicPath() . '/' . $gifFileName);
$this->destroy();
}
return '';
return $gifFileName;
}

/**
Expand Down

0 comments on commit 7a3202c

Please sign in to comment.