Skip to content

Commit

Permalink
[TASK] Use ImageInfo class instead of direct PHP GD call
Browse files Browse the repository at this point in the history
This ensures maximum compatibility with all available graphics
commands.

Resolves: #78600
Releases: master, 8.7
Change-Id: Id7a95fbdf7782fef59e18de3dc4522d7c68b690a
Reviewed-on: https://review.typo3.org/56724
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Markus Klein <markus.klein@typo3.org>
Tested-by: Markus Klein <markus.klein@typo3.org>
  • Loading branch information
liayn committed May 12, 2018
1 parent 554b3af commit 71508fd
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 57 deletions.
17 changes: 5 additions & 12 deletions typo3/sysext/backend/Classes/Form/Utility/FormEngineUtility.php
Expand Up @@ -21,7 +21,6 @@
use TYPO3\CMS\Core\Utility\ArrayUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\PathUtility;
use TYPO3\CMS\Core\Utility\StringUtility;

/**
* This is a static, internal and intermediate helper class for various
Expand Down Expand Up @@ -124,21 +123,15 @@ public static function getIconHtml($icon, $alt = '', $title = '')
$icon = (string)$icon;
$absoluteFilePath = GeneralUtility::getFileAbsFileName($icon);
if (!empty($absoluteFilePath) && is_file($absoluteFilePath)) {
$iconInfo = StringUtility::endsWith($absoluteFilePath, '.svg')
? true
: getimagesize($absoluteFilePath);

if ($iconInfo !== false) {
return '<img'
. ' src="' . htmlspecialchars(PathUtility::getAbsoluteWebPath($absoluteFilePath)) . '"'
. ' alt="' . htmlspecialchars($alt) . '" '
. ($title ? 'title="' . htmlspecialchars($title) . '"' : '')
return '<img'
. ' src="' . htmlspecialchars(PathUtility::getAbsoluteWebPath($absoluteFilePath)) . '"'
. ' alt="' . htmlspecialchars($alt) . '" '
. ($title ? 'title="' . htmlspecialchars($title) . '"' : '')
. ' />';
}
}

$iconFactory = GeneralUtility::makeInstance(IconFactory::class);
return '<span alt="' . htmlspecialchars($alt) . '" title="' . htmlspecialchars($title) . '">'
return '<span title="' . htmlspecialchars($title) . '">'
. $iconFactory->getIcon($icon, Icon::SIZE_SMALL)->render()
. '</span>';
}
Expand Down
15 changes: 12 additions & 3 deletions typo3/sysext/core/Classes/Html/RteHtmlParser.php
Expand Up @@ -19,6 +19,7 @@
use TYPO3\CMS\Core\LinkHandling\LinkService;
use TYPO3\CMS\Core\Log\LogManager;
use TYPO3\CMS\Core\Resource;
use TYPO3\CMS\Core\Type\File\ImageInfo;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\Service\TypoLinkCodecService;

Expand Down Expand Up @@ -401,7 +402,11 @@ public function TS_images_db($value)
];
if (!$imageInfo[0] || !$imageInfo[1]) {
$filePath = $originalImageFile->getForLocalProcessing(false);
$imageInfo = @getimagesize($filePath);
$imageInfoObject = GeneralUtility::makeInstance(ImageInfo::class, $filePath);
$imageInfo = [
$imageInfoObject->getWidth(),
$imageInfoObject->getHeight()
];
}
$attribArray = $this->applyPlainImageModeSettings($imageInfo, $attribArray);
}
Expand Down Expand Up @@ -456,7 +461,11 @@ public function TS_images_db($value)
if ($this->procOptions['plainImageMode']) {
// If "plain image mode" has been configured
// Find the original dimensions of the image
$imageInfo = @getimagesize($filepath);
$imageInfoObject = GeneralUtility::makeInstance(ImageInfo::class, $filepath);
$imageInfo = [
$imageInfoObject->getWidth(),
$imageInfoObject->getHeight()
];
$attribArray = $this->applyPlainImageModeSettings($imageInfo, $attribArray);
}
// Let's try to find a file uid for this image
Expand Down Expand Up @@ -904,7 +913,7 @@ public function getKeepTags($direction = 'rte')
* @param string $value Value to process.
* @param int $count Recursion brake. Decremented on each recursion down to zero. Default is 5 (which equals the allowed nesting levels of p tags).
* @param bool $returnArray If TRUE, an array with the lines is returned, otherwise a string of the processed input value.
* @return string Processed input value.
* @return string|array Processed input value.
* @see setDivTags()
*/
public function divideIntoLines($value, $count = 5, $returnArray = false)
Expand Down
36 changes: 19 additions & 17 deletions typo3/sysext/core/Classes/Imaging/GraphicalFunctions.php
Expand Up @@ -16,6 +16,7 @@

use TYPO3\CMS\Core\Cache\CacheManager;
use TYPO3\CMS\Core\Charset\CharsetConverter;
use TYPO3\CMS\Core\Type\File\ImageInfo;
use TYPO3\CMS\Core\Utility\ArrayUtility;
use TYPO3\CMS\Core\Utility\CommandUtility;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
Expand Down Expand Up @@ -2227,22 +2228,24 @@ public function imageMagickConvert($imagefile, $newExt = '', $w = '', $h = '', $
*/
public function getImageDimensions($imageFile)
{
$returnArr = null;
preg_match('/([^\\.]*)$/', $imageFile, $reg);
if (file_exists($imageFile) && GeneralUtility::inList($this->imageFileExt, strtolower($reg[0]))) {
if ($returnArr = $this->getCachedImageDimensions($imageFile)) {
return $returnArr;
}
if ($temp = @getimagesize($imageFile)) {
$returnArr = [$temp[0], $temp[1], strtolower($reg[0]), $imageFile];
} else {
$returnArr = $this->imageMagickIdentify($imageFile);
}
if ($returnArr) {
$this->cacheImageDimensions($returnArr);
return $returnArr;
$returnArr = $this->getCachedImageDimensions($imageFile);
if (!$returnArr) {
$imageInfoObject = GeneralUtility::makeInstance(ImageInfo::class, $imageFile);
if ($imageInfoObject->getWidth()) {
$returnArr = [
$imageInfoObject->getWidth(),
$imageInfoObject->getHeight(),
strtolower($reg[0]),
$imageFile
];
$this->cacheImageDimensions($returnArr);
}
}
}
return null;
return $returnArr;
}

/**
Expand Down Expand Up @@ -2762,8 +2765,7 @@ public function output($file)
if ($this->setup['quality']) {
$quality = MathUtility::forceIntegerInRange($this->setup['quality'], 10, 100);
}
if ($this->ImageWrite($this->im, $file, $quality)) {
}
$this->ImageWrite($this->im, $file, $quality);
break;
}
}
Expand Down Expand Up @@ -2869,10 +2871,10 @@ public function imageCreateFromFile($sourceImg)
break;
}
// If non of the above:
$i = @getimagesize($sourceImg);
$im = imagecreatetruecolor($i[0], $i[1]);
$imageInfo = GeneralUtility::makeInstance(ImageInfo::class, $sourceImg);
$im = imagecreatetruecolor($imageInfo->getWidth(), $imageInfo->getHeight());
$Bcolor = imagecolorallocate($im, 128, 128, 128);
imagefilledrectangle($im, 0, 0, $i[0], $i[1], $Bcolor);
imagefilledrectangle($im, 0, 0, $imageInfo->getWidth(), $imageInfo->getHeight(), $Bcolor);
return $im;
}

Expand Down
Expand Up @@ -22,6 +22,7 @@
use TYPO3\CMS\Core\Resource\ProcessedFileRepository;
use TYPO3\CMS\Core\Resource\Processing\LocalImageProcessor;
use TYPO3\CMS\Core\Resource\Service\FileProcessingService;
use TYPO3\CMS\Core\Type\File\ImageInfo;
use TYPO3\CMS\Core\Utility\CommandUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\Imaging\GifBuilder;
Expand Down Expand Up @@ -91,11 +92,11 @@ public function processFile(FileProcessingService $fileProcessingService, Driver
GeneralUtility::unlink_tempfile($temporaryFileName);
if (is_file($temporaryFileNameForResizedThumb)) {
$processedFile->setName($this->getTargetFileName($processedFile));
list($width, $height) = getimagesize($temporaryFileNameForResizedThumb);
$imageInfo = GeneralUtility::makeInstance(ImageInfo::class, $temporaryFileNameForResizedThumb);
$processedFile->updateProperties(
[
'width' => $width,
'height' => $height,
'width' => $imageInfo->getWidth(),
'height' => $imageInfo->getHeight(),
'size' => filesize($temporaryFileNameForResizedThumb),
'checksum' => $processedFile->getTask()->getConfigurationChecksum()
]
Expand Down
8 changes: 5 additions & 3 deletions typo3/sysext/core/Classes/Type/File/ImageInfo.php
Expand Up @@ -54,9 +54,11 @@ public function getHeight()
*/
protected function getImageSizes()
{
if (is_null($this->imageSizes)) {
$this->imageSizes = getimagesize($this->getPathname());

if ($this->imageSizes === null) {
$this->imageSizes = false;
if (function_exists('getimagesize')) {
$this->imageSizes = @getimagesize($this->getPathname());
}
// Try SVG first as SVG size detection with IM/GM leads to an error output
if ($this->imageSizes === false && $this->getMimeType() === 'image/svg+xml') {
$this->imageSizes = $this->extractSvgImageSizes();
Expand Down
13 changes: 5 additions & 8 deletions typo3/sysext/extensionmanager/Classes/Utility/ListUtility.php
Expand Up @@ -15,6 +15,7 @@
*/

use TYPO3\CMS\Core\Package\PackageInterface;
use TYPO3\CMS\Core\Type\File\ImageInfo;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\VersionNumberUtility;
Expand Down Expand Up @@ -268,14 +269,10 @@ protected function getExtensionTerData($extensionKey, $version)
public function enrichExtensionsWithIconInformation(array $extensions)
{
foreach ($extensions as &$properties) {
$iInfo = @getimagesize(PATH_site . $properties['siteRelPath'] . $properties['ext_icon']);
if ($iInfo !== false) {
$properties['ext_icon_width'] = $iInfo[0];
$properties['ext_icon_height'] = $iInfo[1];
} else {
$properties['ext_icon_width'] = 0;
$properties['ext_icon_height'] = 0;
}
$extIconPath = PATH_site . $properties['siteRelPath'] . $properties['ext_icon'];
$imageInfo = GeneralUtility::makeInstance(ImageInfo::class, $extIconPath);
$properties['ext_icon_width'] = $imageInfo->getWidth();
$properties['ext_icon_height'] = $imageInfo->getHeight();
}
unset($properties);
return $extensions;
Expand Down
Expand Up @@ -13,6 +13,7 @@
*
* The TYPO3 project - inspiring people to share!
*/
use TYPO3\CMS\Core\Type\File\ImageInfo;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;

Expand All @@ -36,9 +37,12 @@ public function render($conf = [])
$fileInfo = GeneralUtility::split_fileref($file);
$extension = $fileInfo['fileext'];
if ($extension === 'jpg' || $extension === 'jpeg' || $extension === 'gif' || $extension === 'png') {
$imgInfo = @getimagesize($file);
$imageInfo = GeneralUtility::makeInstance(ImageInfo::class, $file);
$altParameters = trim($this->cObj->getAltParam($conf, false));
$theValue = '<img src="' . htmlspecialchars($this->getTypoScriptFrontendController()->absRefPrefix . $file) . '" width="' . (int)$imgInfo[0] . '" height="' . (int)$imgInfo[1] . '"' . $this->cObj->getBorderAttr(' border="0"') . ' ' . $altParameters . ' />';
$theValue = '<img src="'
. htmlspecialchars($this->getTypoScriptFrontendController()->absRefPrefix . $file)
. '" width="' . (int)$imageInfo->getWidth() . '" height="' . (int)$imageInfo->getHeight()
. '"' . $this->cObj->getBorderAttr(' border="0"') . ' ' . $altParameters . ' />';
} elseif (filesize($file) < 1024 * 1024) {
$theValue = file_get_contents($file);
}
Expand Down
Expand Up @@ -14,6 +14,7 @@
* The TYPO3 project - inspiring people to share!
*/

use TYPO3\CMS\Core\Type\File\ImageInfo;
use TYPO3\CMS\Core\TypoScript\TypoScriptService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
Expand Down Expand Up @@ -242,9 +243,9 @@ public function makeGifs($conf, $resKey)
// Generation of image file:
// File exists
if (file_exists($gifFileName)) {
$info = @getimagesize($gifFileName);
$this->result[$resKey][$key]['output_w'] = (int)$info[0];
$this->result[$resKey][$key]['output_h'] = (int)$info[1];
$imageInfo = GeneralUtility::makeInstance(ImageInfo::class, $gifFileName);
$this->result[$resKey][$key]['output_w'] = (int)$imageInfo->getWidth();
$this->result[$resKey][$key]['output_h'] = (int)$imageInfo->getHeight();
$this->result[$resKey][$key]['output_file'] = $gifFileName;
} elseif ($isGD) {
// file is generated
Expand Down
Expand Up @@ -14,6 +14,7 @@
* The TYPO3 project - inspiring people to share!
*/

use TYPO3\CMS\Core\Type\File\ImageInfo;
use TYPO3\CMS\Core\Utility\ArrayUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
Expand Down Expand Up @@ -199,9 +200,9 @@ public function makeImageMap($conf)
$imgMap = $cache->get($imgHash);
// File exists
if ($imgMap && file_exists($gifFileName)) {
$info = @getimagesize($gifFileName);
$w = $info[0];
$h = $info[1];
$imageInfo = GeneralUtility::makeInstance(ImageInfo::class, $gifFileName);
$w = $imageInfo->getWidth();
$h = $imageInfo->getHeight();
} else {
// file is generated
$gifCreator->make();
Expand Down
Expand Up @@ -19,6 +19,7 @@
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\Restriction\FrontendRestrictionContainer;
use TYPO3\CMS\Core\Html\HtmlParser;
use TYPO3\CMS\Core\Type\File\ImageInfo;
use TYPO3\CMS\Core\TypoScript\TypoScriptService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\IpAnonymizationUtility;
Expand Down Expand Up @@ -670,9 +671,14 @@ public function makeItemTypeIcon($imageType, $alt, $specRowConf)
if ($icon) {
$fullPath = GeneralUtility::getFileAbsFileName($icon);
if ($fullPath) {
$info = @getimagesize($fullPath);
$iconPath = \TYPO3\CMS\Core\Utility\PathUtility::stripPathSitePrefix($fullPath);
$this->iconFileNameCache[$imageType] = is_array($info) ? '<img src="' . $iconPath . '" ' . $info[3] . ' title="' . htmlspecialchars($alt) . '" alt="" />' : '';
$imageInfo = GeneralUtility::makeInstance(ImageInfo::class, $fullPath);
$iconPath = PathUtility::stripPathSitePrefix($fullPath);
$this->iconFileNameCache[$imageType] = $imageInfo->getWidth()
? '<img src="' . $iconPath
. '" width="' . $imageInfo->getWidth()
. '" height="' . $imageInfo->getHeight()
. '" title="' . htmlspecialchars($alt) . '" alt="" />'
: '';
}
}
}
Expand Down

0 comments on commit 71508fd

Please sign in to comment.