[TASK] Use ImageInfo class instead of direct PHP GD call 24/56724/7
authorMarkus Klein <markus.klein@typo3.org>
Mon, 9 Apr 2018 22:01:21 +0000 (00:01 +0200)
committerMarkus Klein <markus.klein@typo3.org>
Sat, 12 May 2018 22:08:36 +0000 (00:08 +0200)
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>
typo3/sysext/backend/Classes/Form/Utility/FormEngineUtility.php
typo3/sysext/core/Classes/Html/RteHtmlParser.php
typo3/sysext/core/Classes/Imaging/GraphicalFunctions.php
typo3/sysext/core/Classes/Resource/OnlineMedia/Processing/PreviewProcessing.php
typo3/sysext/core/Classes/Type/File/ImageInfo.php
typo3/sysext/extensionmanager/Classes/Utility/ListUtility.php
typo3/sysext/frontend/Classes/ContentObject/FileContentObject.php
typo3/sysext/frontend/Classes/ContentObject/Menu/GraphicalMenuContentObject.php
typo3/sysext/frontend/Classes/ContentObject/Menu/ImageMenuContentObject.php
typo3/sysext/indexed_search/Classes/Controller/SearchController.php

index 72ed501..17b1c3d 100644 (file)
@@ -21,7 +21,6 @@ use TYPO3\CMS\Core\Imaging\IconFactory;
 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
@@ -124,21 +123,15 @@ class FormEngineUtility
         $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>';
     }
index 6560885..bb6f949 100644 (file)
@@ -19,6 +19,7 @@ use TYPO3\CMS\Core\LinkHandling\Exception\UnknownLinkHandlerException;
 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;
 
@@ -401,7 +402,11 @@ class RteHtmlParser extends HtmlParser
                                 ];
                                 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);
                             }
@@ -456,7 +461,11 @@ class RteHtmlParser extends HtmlParser
                             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
@@ -904,7 +913,7 @@ class RteHtmlParser extends HtmlParser
      * @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)
index 48d45d1..3debed6 100644 (file)
@@ -16,6 +16,7 @@ namespace TYPO3\CMS\Core\Imaging;
 
 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;
@@ -2227,22 +2228,24 @@ class GraphicalFunctions
      */
     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;
     }
 
     /**
@@ -2762,8 +2765,7 @@ class GraphicalFunctions
                     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;
             }
         }
@@ -2869,10 +2871,10 @@ class GraphicalFunctions
                 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;
     }
 
index 233f0fa..4937691 100644 (file)
@@ -22,6 +22,7 @@ use TYPO3\CMS\Core\Resource\ProcessedFile;
 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;
@@ -91,11 +92,11 @@ class PreviewProcessing
         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()
                 ]
index 9c42960..2ea7d28 100644 (file)
@@ -54,9 +54,11 @@ class ImageInfo extends FileInfo
      */
     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();
index 36fed51..a8dec4d 100644 (file)
@@ -15,6 +15,7 @@ namespace TYPO3\CMS\Extensionmanager\Utility;
  */
 
 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;
@@ -268,14 +269,10 @@ class ListUtility implements \TYPO3\CMS\Core\SingletonInterface
     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;
index 6b98a64..303db51 100644 (file)
@@ -13,6 +13,7 @@ namespace TYPO3\CMS\Frontend\ContentObject;
  *
  * 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;
 
@@ -36,9 +37,12 @@ class FileContentObject extends AbstractContentObject
             $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);
             }
index b8c7807..d2584a6 100644 (file)
@@ -14,6 +14,7 @@ namespace TYPO3\CMS\Frontend\ContentObject\Menu;
  * 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;
@@ -242,9 +243,9 @@ class GraphicalMenuContentObject extends AbstractMenuContentObject
             // 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
index a58fe95..dab14ef 100644 (file)
@@ -14,6 +14,7 @@ namespace TYPO3\CMS\Frontend\ContentObject\Menu;
  * 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;
@@ -199,9 +200,9 @@ class ImageMenuContentObject extends AbstractMenuContentObject
                 $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();
index fe838ba..57bad61 100644 (file)
@@ -19,6 +19,7 @@ use TYPO3\CMS\Core\Database\Connection;
 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;
@@ -670,9 +671,14 @@ class SearchController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionControlle
                 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="" />'
+                            : '';
                     }
                 }
             }